-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
75 lines (67 loc) · 2.61 KB
/
content.js
File metadata and controls
75 lines (67 loc) · 2.61 KB
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
function getCategoriesFromPage() {
const categorySet = new Set();
document.querySelectorAll("section.kur-floor").forEach((section) => {
section.querySelectorAll("a").forEach((linkElement) => {
const href = linkElement.getAttribute("href");
if (!href || !href.startsWith("https://")) return;
const match = href.match(/^https:\/\/[^/]+\/([a-zA-Z-]+)\//);
if (match && match[1]) {
const category = match[1];
categorySet.add(category);
}
});
});
const categoriesArray = Array.from(categorySet);
categoriesArray.push("minigames", "p3", "radio");
return categoriesArray;
}
function sendDetectedCategories() {
const categories = getCategoriesFromPage();
chrome.runtime.sendMessage({ action: "detectedCategories", categories: categories });
}
function applyFilters(filters) {
document.querySelectorAll("section.kur-floor .kur-room-wrapper").forEach((article) => {
const link = article.querySelector("a");
const href = link?.getAttribute("href") || "";
let shouldHide = false;
for (const category of filters) {
if (category === "minigames" && article.querySelector(".kur-floor__title-text")?.textContent.includes("DAGLIGE MINISPILL")) {
shouldHide = true;
break;
}
if (category === "p3" && href.includes("p3.no")) {
shouldHide = true;
break;
}
if (category === "radio" && href.includes("radio.nrk.no")) {
shouldHide = true;
break;
}
const categoryPattern = new RegExp(`/${category}/`, 'i');
if (categoryPattern.test(href)) {
shouldHide = true;
break;
}
}
article.style.display = shouldHide ? "none" : "block";
});
}
chrome.storage.local.get("selectedFilters", (data) => {
if (data.selectedFilters) {
applyFilters(data.selectedFilters);
}
});
sendDetectedCategories();
chrome.runtime.onMessage.addListener((message) => {
if (message.action === "applyFilters") {
const selectedFilters = message.filters;
chrome.storage.local.set({ "selectedFilters": selectedFilters });
applyFilters(selectedFilters);
} else if (message.action === "showAll") {
document.querySelectorAll("section.kur-floor .kur-room-wrapper").forEach((article) => {
article.style.display = "block";
});
} else if (message.action === "requestCategories") {
sendDetectedCategories();
}
});