-
Notifications
You must be signed in to change notification settings - Fork 6
/
background.js
67 lines (60 loc) · 1.67 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
57
58
59
60
61
62
63
64
65
66
67
const targetPage = 'https://teams.microsoft.com/*';
const fallbackUserAgent = 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43';
let userAgent = fallbackUserAgent;
/**
* rewrite user agent header
*
* @param e
* @returns {{requestHeaders: *}}
*/
function rewriteUserAgentHeader(e) {
for (const header of e.requestHeaders) {
if (header.name.toLowerCase() === 'user-agent') {
header.value = userAgent;
}
}
return { requestHeaders: e.requestHeaders };
}
/**
* register webrequest hook
*
*/
browser.webRequest.onBeforeSendHeaders.addListener(
rewriteUserAgentHeader,
{ urls: [targetPage] },
['blocking', 'requestHeaders']
);
/**
* handle first install / updates
*/
function onInstalled(details) {
if (details.reason === 'update' || details.reason === 'install') {
// handle tab reload after installation to ensure the extension will work correctly
browser.tabs.query({ url: 'https://teams.microsoft.com/*' }).then((tabs) => {
tabs.forEach((tab) => {
if (tab.id) {
browser.tabs.reload(tab.id, { bypassCache: true });
}
})
});
}
}
// register install/update handler
browser.runtime.onInstalled.addListener(onInstalled);
/**
* watch storage for changes
*
* @param changes
* @param area
*/
function storageWatcher(changes, area) {
if (area === 'local') {
if (changes.userAgent) {
userAgent = changes.userAgent.newValue;
}
}
}
// register storage watcher
browser.storage.onChanged.addListener(storageWatcher);
// initialize user agent from storage
browser.storage.local.get({userAgent}).then((item) => userAgent = item.userAgent);