-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
83 lines (66 loc) · 2.56 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
document.addEventListener("DOMContentLoaded", async () => {
const editorElement = document.getElementById("scriptEditor");
const lineNumbersElement = document.getElementById("line-numbers");
// Load the utility functions from utils.js
const { updateIcon, injectScript } = await import("./utils.js");
// Function to update line numbers
const updateLineNumbers = () => {
const lines = editorElement.value.split("\n").length;
lineNumbersElement.innerHTML = "";
for (let i = 1; i <= lines; i++) {
const lineNumber = document.createElement("span");
lineNumber.textContent = i;
lineNumbersElement.appendChild(lineNumber);
}
};
// Update line numbers on input
editorElement.addEventListener("input", updateLineNumbers);
// Sync scroll position between textarea and line numbers
editorElement.addEventListener("scroll", () => {
lineNumbersElement.scrollTop = editorElement.scrollTop;
});
// Get the current active tab
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const activeTab = tabs[0];
const url = activeTab.url;
console.log("Active tab URL:", url);
// Load the script for the current active tab
const result = await browser.storage.local.get(url);
const script = result[url] || "";
editorElement.value = script;
console.log("Loaded script from localStorage:", script);
// Initial update of line numbers
updateLineNumbers();
// Initial update of the icon based on whether a script exists
updateIcon(!!script ? "active" : "no-script");
// Focus the textarea input
editorElement.focus();
// Save the script, inject it, and update the icon when the textarea loses focus
editorElement.addEventListener("blur", async (event) => {
const newScript = editorElement.value;
console.log("Script on blur:", newScript);
await browser.storage.local.set({ [url]: newScript });
// Inject the updated script into the current page
if (newScript) {
injectScript(activeTab.id, url, newScript);
} else {
updateIcon("no-script");
}
});
// Apply theme-based styling
const applyTheme = () => {
const isDarkMode = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
if (isDarkMode) {
document.body.classList.add("dark-mode");
editorElement.classList.add("dark-mode");
} else {
document.body.classList.remove("dark-mode");
editorElement.classList.remove("dark-mode");
}
};
// Listen for theme changes
window.matchMedia("(prefers-color-scheme: dark)").addListener(applyTheme);
applyTheme();
});