-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
64 lines (57 loc) · 1.9 KB
/
utils.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
// Function to create a dynamically colored icon
function createColoredIcon(status, baseIconPath = "icon-19.png") {
return new Promise((resolve) => {
const icon = new Image();
icon.src = browser.runtime.getURL(baseIconPath);
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = 19;
canvas.height = 19;
let backgroundColor;
switch (status) {
case "active":
backgroundColor = "green";
break;
case "error":
backgroundColor = "red";
break;
default:
backgroundColor = "grey";
break;
}
icon.onload = () => {
context.fillStyle = backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
context.drawImage(icon, 0, 0, canvas.width, canvas.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
resolve(imageData);
};
});
}
// Function to update the extension's icon with dynamic coloring
async function updateIcon(status) {
const imageData = await createColoredIcon(status);
browser.browserAction.setIcon({ imageData });
}
// Function to inject script into a tab
async function injectScript(tabId, url, script) {
try {
await browser.tabs.executeScript(tabId, {
code: `(() => {
const existingScript = document.getElementById('injectedScript');
if (existingScript) existingScript.remove();
const scriptElement = document.createElement('script');
scriptElement.id = 'injectedScript';
scriptElement.textContent = ${JSON.stringify(script)};
document.body.appendChild(scriptElement);
})();`,
});
console.log(`Injected script into ${url}`);
updateIcon("active");
} catch (e) {
console.error(`Error injecting script into ${url}: ${e}`);
updateIcon("error");
}
}
// Export the functions
export { updateIcon, injectScript };