forked from tridactyl/tridactyl
/
background.ts
141 lines (122 loc) · 4.1 KB
/
background.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/** Background script entry point. */
import * as BackgroundController from "./controller_background"
import "./lib/browser_proxy_background"
// Add various useful modules to the window for debugging
import * as messaging from "./messaging"
import * as excmds from "./.excmds_background.generated"
import * as commandline_background from "./commandline_background"
import * as convert from "./convert"
import * as config from "./config"
import * as dom from "./dom"
import * as hinting_background from "./hinting_background"
import * as download_background from "./download_background"
import * as itertools from "./itertools"
import * as keyseq from "./keyseq"
import * as request from "./requests"
import * as native from "./native_background"
import * as msgsafe from "./msgsafe"
import state from "./state"
import * as webext from "./lib/webext"
import { AutoContain } from "./lib/autocontainers"
;(window as any).tri = Object.assign(Object.create(null), {
messaging,
excmds,
commandline_background,
convert,
config,
dom,
hinting_background,
download_background,
itertools,
native,
keyseq,
request,
msgsafe,
state,
webext,
l: prom => prom.then(console.log).catch(console.error),
})
// Send commandline to controller
commandline_background.onLine.addListener(BackgroundController.acceptExCmd)
// {{{ Clobber CSP
// This should be removed once https://bugzilla.mozilla.org/show_bug.cgi?id=1267027 is fixed
function addCSPListener() {
browser.webRequest.onHeadersReceived.addListener(
request.clobberCSP,
{ urls: ["<all_urls>"], types: ["main_frame"] },
["blocking", "responseHeaders"],
)
}
function removeCSPListener() {
browser.webRequest.onHeadersReceived.removeListener(request.clobberCSP)
}
config.getAsync("csp").then(csp => csp === "clobber" && addCSPListener())
browser.storage.onChanged.addListener((changes, areaname) => {
if ("userconfig" in changes) {
if (changes.userconfig.newValue.csp === "clobber") {
addCSPListener()
} else {
removeCSPListener()
}
}
})
// }}}
// Prevent Tridactyl from being updated while it is running in the hope of fixing #290
browser.runtime.onUpdateAvailable.addListener(_ => {})
browser.runtime.onStartup.addListener(_ => {
config.getAsync("autocmds", "TriStart").then(aucmds => {
let hosts = Object.keys(aucmds)
// If there's only one rule and it's "all", no need to check the hostname
if (hosts.length == 1 && hosts[0] == ".*") {
BackgroundController.acceptExCmd(aucmds[hosts[0]])
} else {
native.run("hostname").then(hostname => {
for (let host of hosts) {
if (hostname.content.match(host)) {
BackgroundController.acceptExCmd(aucmds[host])
}
}
})
}
})
})
let curTab = null
browser.tabs.onActivated.addListener(ev => {
let ignore = _ => _
if (curTab !== null) {
// messaging.messageTab failing can happen when leaving privileged tabs (e.g. about:addons)
messaging
.messageTab(curTab, "excmd_content", "loadaucmds", ["TabLeft"])
.catch(ignore)
}
curTab = ev.tabId
messaging
.messageTab(curTab, "excmd_content", "loadaucmds", ["TabEnter"])
.catch(ignore)
})
// {{{ AUTOCONTAINERS
let aucon = new AutoContain()
// Handle cancelled requests as a result of autocontain.
browser.webRequest.onCompleted.addListener(
details => {
if (aucon.getCancelledRequest(details.tabId)) {
aucon.clearCancelledRequests(details.tabId)
}
},
{ urls: ["<all_urls"], types: ["main_frame"] },
)
browser.webRequest.onErrorOccurred.addListener(
details => {
if (aucon.getCancelledRequest(details.tabId)) {
aucon.clearCancelledRequests(details.tabId)
}
},
{ urls: ["<all_urls>"], types: ["main_frame"] },
)
// Contain autocmd.
browser.webRequest.onBeforeRequest.addListener(
aucon.autoContain,
{ urls: ["<all_urls>"], types: ["main_frame"] },
["blocking"],
)
// }}}