-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
82 lines (73 loc) · 2.43 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
document.addEventListener("DOMContentLoaded", () => {
const toggleButton = document.getElementById("toggleButton");
// Function to update button text
function updateButtonText(isEnabled) {
toggleButton.textContent = isEnabled
? "Disable Difficulty Hider"
: "Enable Difficulty Hider";
}
// Function to save settings to chrome storage
function saveSettings(isEnabled) {
chrome.storage.local.set({ isEnabled });
}
// Function to load settings from chrome storage
function loadSettings(callback) {
chrome.storage.local.get("isEnabled", (data) => {
callback(data.isEnabled !== undefined ? data.isEnabled : true);
});
}
// Set the initial state based on stored value or default to enabled
loadSettings((isEnabled) => {
updateButtonText(isEnabled);
// Apply initial state to the current page
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: updateDifficultyVisibility,
args: [isEnabled],
});
});
});
// Toggle the state when the button is clicked
toggleButton.addEventListener("click", () => {
loadSettings((isEnabled) => {
const newIsEnabled = !isEnabled;
saveSettings(newIsEnabled);
updateButtonText(newIsEnabled);
// Update the current tab immediately
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: updateDifficultyVisibility,
args: [newIsEnabled],
});
});
});
});
});
// This function will run in the context of the current page
function updateDifficultyVisibility(isEnabled) {
const problemSetDifficultyElements = document.querySelectorAll(
"div[role='cell'] span[class*='text-']"
);
const problemPageDifficultyElements = document.querySelectorAll(
"div[class*='text-difficulty-']"
);
if (isEnabled) {
// Hide difficulties
problemSetDifficultyElements.forEach((element) => {
element.style.display = "none";
});
problemPageDifficultyElements.forEach((element) => {
element.style.display = "none";
});
} else {
// Show difficulties
problemSetDifficultyElements.forEach((element) => {
element.style.display = "";
});
problemPageDifficultyElements.forEach((element) => {
element.style.display = "";
});
}
}