-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscript.js
executable file
·92 lines (76 loc) · 2.52 KB
/
script.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
83
84
85
86
87
88
89
90
91
92
// toggle switch to (de)activate extension
const toggleSwitch = document.getElementById("toggleSwitch");
// getting the saved state of the toggle
chrome.storage.sync.get(["ext_on"], async function (items) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
return;
}
toggleSwitch.checked = items.ext_on != false;
toggleContent(toggleSwitch.checked);
var tab = await getCurrentTab();
await getDomElements(tab, toggleSwitch.checked);
});
const toggleContent = (isChecked) => {
const pluginWindow = document.getElementById("plugin-window");
const content = document.getElementById("content");
const welcome = document.getElementById("welcome");
const footer = document.getElementById("footer");
const inputDialog = document.getElementById("input-dialog");
if (isChecked) {
pluginWindow.style.backgroundColor = "#fafafa";
content.style.display = "block";
welcome.style.display = "none";
footer.style.color = "#000000";
inputDialog.style.display = "none";
} else {
pluginWindow.style.backgroundColor = "#004D23";
content.style.display = "none";
welcome.style.display = "flex";
footer.style.color = "#ffffff";
inputDialog.style.display = "none";
}
};
// listening to changes to the toggle switch
toggleSwitch.addEventListener("change", async () => {
const isChecked = toggleSwitch.checked;
toggleContent(isChecked);
var tab = await getCurrentTab();
await getDomElements(tab, isChecked);
chrome.storage.sync.set({
ext_on: isChecked,
function() {},
});
});
// get the current active tab to run script on it
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
try {
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
} catch (error) {
console.error(error);
}
}
// runs the content.js or revert.js file on the current tab
const getDomElements = async (tab, shouldReplace) => {
if (!tab && !tab.id) {
return;
}
try {
let fileName = shouldReplace === true ? "content.js" : "revert.js";
await chrome.scripting.executeScript({
target: { tabId: tab.id, allFrames: true },
files: [fileName],
});
} catch (error) {
console.log(error);
}
};
// Attach the function to the global object (window in a browser, global in Node.js)
if (typeof window !== "undefined") {
window.getCurrentTab = getCurrentTab;
} else if (typeof global !== "undefined") {
global.getDomElements = getDomElements;
}