-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
91 lines (79 loc) · 3.4 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
document.addEventListener("DOMContentLoaded", function () {
const categoryContainer = document.getElementById("categoryContainer");
requestCategories();
chrome.runtime.onMessage.addListener((message) => {
if (message.action === "detectedCategories") {
const categories = message.categories;
categoryContainer.innerHTML = "";
categories.forEach((category) => {
if (!document.getElementById(category)) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = category;
checkbox.value = category;
const label = document.createElement("label");
label.htmlFor = category;
label.textContent = category;
const container = document.createElement("div");
container.classList.add("category-checkbox");
container.appendChild(checkbox);
container.appendChild(label);
categoryContainer.appendChild(container);
checkbox.addEventListener("change", applySelectedFilters);
}
});
loadSavedFilters();
}
});
function requestCategories() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: "requestCategories" }, (response) => {
if (chrome.runtime.lastError) {
injectContentScript(tabs[0].id);
}
});
}
});
}
function applySelectedFilters() {
const selectedFilters = Array.from(categoryContainer.querySelectorAll("input:checked"))
.map((checkbox) => checkbox.value);
sendMessageToContentScript({ action: "applyFilters", filters: selectedFilters });
chrome.storage.local.set({ selectedFilters });
}
function loadSavedFilters() {
chrome.storage.local.get("selectedFilters", (data) => {
if (data.selectedFilters) {
data.selectedFilters.forEach((filter) => {
const checkbox = document.getElementById(filter);
if (checkbox) checkbox.checked = true;
});
}
});
}
function sendMessageToContentScript(message) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
if (chrome.runtime.lastError) {
injectContentScript(tabs[0].id, message);
}
});
}
});
}
function injectContentScript(tabId) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ["content.js"]
});
}
document.getElementById("showAllButton")?.addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: "showAll" });
});
categoryContainer.querySelectorAll("input[type='checkbox']").forEach(checkbox => checkbox.checked = false);
chrome.storage.local.remove("selectedFilters");
});
});