-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
56 lines (50 loc) · 1.65 KB
/
background.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
// Defaults
chrome.storage.sync.get(['isActivated'], ({ isActivated }) => {
if (typeof isActivated !== 'boolean') chrome.storage.sync.set({ isActivated: true })
})
// Find all Google Docs tabs
const findTabs = () =>
new Promise(resolve =>
chrome.tabs.query(
{
status: 'complete',
url: 'https://docs.google.com/document/*',
},
resolve
)
)
// Changes the height of a window by a delta
const resizeWindow = (windowId, delta) =>
new Promise(resolve =>
chrome.windows.get(windowId, win => {
chrome.windows.update(windowId, { height: win.height + delta })
resolve()
})
)
// The most reliable I've found to trigger a re-render of the canvas element
// is to change the size of the window. Dispatching events does nothing.
// As such, we change the height by a single pixel back-and-forth
const triggerCanvasRerender = async () => {
const tabs = await findTabs()
const windowIds = tabs.reduce((acc, inc) => {
acc[inc.windowId] = true
return acc
}, {})
await Promise.all(
Object.keys(windowIds).map(async windowId => {
await resizeWindow(+windowId, -1)
await resizeWindow(+windowId, 1)
})
)
}
// Insert into all existing tabs,
// e.g. for when the user installs an extension but doesn't reload the page
findTabs().then(tabs =>
tabs.forEach(tab => {
chrome.tabs.insertCSS(tab.id, { file: 'override.css' })
chrome.tabs.executeScript(tab.id, { file: 'content.js', runAt: 'document_idle' })
chrome.tabs.executeScript(tab.id, { file: 'preload.js', runAt: 'document_start' })
triggerCanvasRerender()
})
)
chrome.storage.onChanged.addListener(triggerCanvasRerender)