forked from zmrhaljiri/text-spacing-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbackground.ts
More file actions
78 lines (66 loc) · 1.98 KB
/
background.ts
File metadata and controls
78 lines (66 loc) · 1.98 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
76
77
78
import { Storage } from "@plasmohq/storage"
import { buildCSSToInject } from "~helpers/buildCSSToInject"
const storage = new Storage({ area: "local" })
// Listen to custom key strokes
chrome.commands.onCommand.addListener((command) => {
if (command === "toggle") {
chrome.runtime.sendMessage("toggle")
}
})
// Listen to tab updates e.g. navigation within tab
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
const enabled = await storage.get("enabled")
const payload = await storage.get("styles")
const css = buildCSSToInject(payload, tabId)
if (!enabled) {
chrome.scripting.removeCSS(css)
}
if (changeInfo.status === "complete" && enabled) {
chrome.scripting.insertCSS(css)
}
})
// Listen to tab clicks
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const enabled = await storage.get("enabled")
const payload = await storage.get("styles")
const css = buildCSSToInject(payload, activeInfo.tabId)
if (!enabled) {
chrome.scripting.removeCSS(css)
}
if (enabled) {
chrome.scripting.insertCSS(css)
}
})
// Listen to window focus change
chrome.windows.onFocusChanged.addListener(async (windowId) => {
await chrome.tabs.query(
{ active: true, windowId: windowId },
async (tabs) => {
if (tabs.length > 0) {
const enabled = await storage.get("enabled")
const payload = await storage.get("styles")
const css = buildCSSToInject(payload, tabs[0].id)
if (!enabled) {
chrome.scripting.removeCSS(css)
}
if (enabled) {
chrome.scripting.insertCSS(css)
}
}
}
)
})
// Insert CSS when a new tab is created
chrome.tabs.onCreated.addListener(async (tab) => {
if (tab.id) {
const enabled = await storage.get("enabled")
const payload = await storage.get("styles")
const css = buildCSSToInject(payload, tab.id)
if (!enabled) {
chrome.scripting.removeCSS(css)
}
if (enabled) {
chrome.scripting.insertCSS(css)
}
}
})