-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
39 lines (34 loc) · 1.65 KB
/
background.js
File metadata and controls
39 lines (34 loc) · 1.65 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
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
try {
let isOtherDocument = false;
// check if the document head contains the specific structure for image documents
const isImageDocument = await browser.tabs.executeScript(tabId, {
file: "scripts/is-image.js",
});
// check if the document is an SVG
const isSVG = await browser.tabs.executeScript(tabId, {
file: "scripts/is-svg.js",
});
// convert the svg into a pseudo html document
if (isSVG && isSVG[0]) {
console.log("SVG detected, converting to image...");
await browser.tabs.executeScript(tabId, { file: "scripts/convert-svg.js" });
await browser.tabs.insertCSS(tabId, { file: "sharp-viewer.css" });
await browser.tabs.executeScript(tabId, { file: "scripts/sharp-viewer.js" });
}
// find reddit.com/media pages
if (tab.url.match(/www\.reddit\.com\/media/)) {
await browser.tabs.executeScript(tabId, { file: "scripts/fix-reddit.js" });
isOtherDocument = true;
}
if ((isImageDocument && isImageDocument[0]) || isOtherDocument) {
// inject viewer
await browser.tabs.insertCSS(tabId, { file: "sharp-viewer.css" });
await browser.tabs.executeScript(tabId, { file: "scripts/sharp-viewer.js" });
}
} catch (error) {
console.error("failed to inject scripts:", error);
}
}
});