You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
3 years ago
|
// Sync the selected categories in the popup
|
||
|
|
||
|
chrome.storage.sync.get(["categories", "selected"], (result) => {
|
||
|
let categories = result.categories;
|
||
|
let selected = result.selected;
|
||
|
listCategories(categories, selected);
|
||
|
});
|
||
|
|
||
|
const listCategories = function (categories, selected) {
|
||
|
let selectedList = document.getElementById("selected-list");
|
||
|
for (const category of selected) {
|
||
|
item = document.createElement("li");
|
||
|
item.innerHTML = categories[category].title;
|
||
|
selectedList.appendChild(item);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const activate = document.getElementById("activate");
|
||
|
activate.addEventListener("click", async () => {
|
||
|
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||
|
|
||
|
chrome.scripting.executeScript({
|
||
|
target: { tabId: tab.id },
|
||
|
function: highlightSelected,
|
||
|
});
|
||
|
});
|
||
|
|
||
|
function highlightSelected() {
|
||
|
chrome.storage.sync.get(["categories", "selected"], (result) => {
|
||
|
for (category of result.selected) {
|
||
|
result.categories[category].keywords.forEach((keyword) => {
|
||
|
let findings = contains("*", keyword);
|
||
|
if (findings.length) {
|
||
|
console.log(findings);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
function contains(selector, text) {
|
||
|
var elements = document.querySelectorAll(selector);
|
||
|
return Array.prototype.filter.call(elements, function (element) {
|
||
|
return RegExp(text).test(element.textContent);
|
||
|
});
|
||
|
}
|
||
|
}
|