-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
91 lines (79 loc) · 2.52 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
83
84
85
86
87
88
89
90
91
let twitterScriptObserver = null;
const getToggleValue = () => {
return localStorage.getItem("toggleValue");
};
const setToggleCheckedItem = () => {
localStorage.setItem("toggleValue", "checked");
};
const setToggleUncheckedItem = () => {
localStorage.setItem("toggleValue", "unchecked");
};
const removeTweetViews = () => {
const views = document.querySelectorAll('[aria-label*="estatísticas do Tweet"]');
const viewsEn = document.querySelectorAll('[aria-label*="Tweet analytics"]');
views.forEach(view => view.parentElement.remove());
viewsEn.forEach(view => view.parentElement.remove());
};
const startTwitterScript = () => {
console.log("Twitter script started");
removeTweetViews();
twitterScriptObserver = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => removeTweetViews());
});
});
twitterScriptObserver.observe(document.body, { childList: true, subtree: true });
};
const stopTwitterScript = () => {
console.log("Twitter script stopped");
if (twitterScriptObserver) {
twitterScriptObserver.disconnect();
twitterScriptObserver = null;
}
};
const toggleCheckbox = document.querySelector("#toggle-checkbox");
const message = document.querySelector("#message");
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
toggleCheckbox.addEventListener("change", async function () {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (toggleCheckbox.checked) {
message.textContent = "Bye";
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: startTwitterScript,
});
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: setToggleCheckedItem,
});
} else {
message.textContent = "Hello again";
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: stopTwitterScript,
});
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: setToggleUncheckedItem,
});
}
});
chrome.scripting
.executeScript({
target: { tabId: tabs[0].id },
function: getToggleValue,
})
.then((result) => {
const toggleValue = result[0].result;
if (toggleValue === "checked") {
toggleCheckbox.checked = true;
message.textContent = "Bye";
} else {
toggleCheckbox.checked = false;
message.textContent = "Hello again";
}
});
});