-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice-worker.js
More file actions
170 lines (154 loc) · 4.39 KB
/
service-worker.js
File metadata and controls
170 lines (154 loc) · 4.39 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import defaults from "./constants/defaults.js";
const excludeMatches = [
"*://*/*\?ajax*",
"*://*/*&ajax*",
"*://*.com/*api/*"
];
const documentUrlPatterns = [
"*://frhd.kanoapps.com/*",
"*://www.freeriderhd.com/*"
];
const moduleScript = {
excludeMatches,
id: "mod",
js: [
"game/mod.js",
"game/modules/profileSearch.js",
"game/modules/trackModeration.js"
// modules
],
matches: documentUrlPatterns,
runAt: "document_end",
world: "MAIN"
};
const contentScripts = [{
excludeMatches,
id: "connection-broker",
js: ["broker.js", "game/renderer/inject.js"],
matches: documentUrlPatterns,
runAt: "document_end"
}, {
excludeMatches,
id: "game",
js: [
"shared/Zip.js",
"game/assets/elements/ContextMenu.js",
"game/main.js"
],
matches: documentUrlPatterns,
runAt: "document_start",
world: "MAIN"
}, {
excludeMatches,
id: "ext",
js: [
"game/ThirdPartyScriptManager.js",
"game/ThirdPartyStyleManager.js",
// "game/mod.js"
],
matches: documentUrlPatterns,
runAt: "document_end",
world: "MAIN"
}, moduleScript];
chrome.runtime.onInstalled.addListener(function() {
chrome.storage.local.get(({ enabled, settings }) => {
enabled !== undefined && setState({ enabled });
chrome.storage.local.set({
enabled: enabled ?? true,
badges: true,
settings: Object.assign(defaults, settings)
})
})
});
chrome.runtime.onStartup.addListener(function() {
self.dispatchEvent(new ExtendableEvent('activate'))
});
chrome.runtime.onUpdateAvailable.addListener(async function() {
await chrome.storage.session.set({ updateAvailable: true });
await chrome.action.setBadgeBackgroundColor({ color: '#b87c14' });
chrome.action.setBadgeText({ text: '1' })
});
chrome.storage.local.onChanged.addListener(function({ enabled, settings }) {
enabled && setState({ enabled: enabled.newValue });
settings && updateModuleScript(settings.newValue)
});
self.addEventListener('activate', function() {
chrome.storage.local.get(({ enabled }) => enabled || updateIcon(enabled))
});
async function setState({ enabled = true }) {
if (enabled) {
await chrome.scripting.registerContentScripts(contentScripts);
await chrome.storage.local.get(({ settings }) => updateModuleScript(settings));
await createContextMenuOptions();
} else {
await chrome.scripting.unregisterContentScripts();
await chrome.contextMenus.removeAll();
}
await chrome.declarativeNetRequest.updateEnabledRulesets({
[(enabled ? 'en' : 'dis') + 'ableRulesetIds']: enabled ? ["frhd-assets"] : await chrome.declarativeNetRequest.getEnabledRulesets()
});
return updateIcon(enabled)
}
function updateIcon(enabled = true) {
const path = size => `/icons/${enabled ? '' : 'disabled/'}icon_${size}.png`;
return chrome.action.setIcon({
path: {
16: path(16),
48: path(48),
128: path(128)
}
})
}
chrome.contextMenus.onClicked.addListener(function(event) {
chrome.storage.local.get(({ settings }) => {
switch(event.menuItemId) {
case 'accountManager':
case 'achievementMonitor':
case 'featuredGhostsDisplay':
settings[event.menuItemId] = event.checked;
chrome.storage.local.set({ settings })
}
})
});
function createContextMenuOptions() {
return chrome.storage.local.get(async ({ settings }) => {
const options = {
accountManager: { checked: settings.accountManager, title: 'Account Manager', type: 'checkbox' },
achievementMonitor: { checked: settings.achievementMonitor, title: 'Achievement Monitor', type: 'checkbox' },
featuredGhostsDisplay: { checked: settings.featuredGhostsDisplay, title: 'Highlight Featured Races', type: 'checkbox' }
}
for (const option in options) {
await chrome.contextMenus.create(Object.assign({
contexts: ['page'],
documentUrlPatterns,
id: option,
type: 'normal'
}, options[option]))
}
})
}
async function updateModuleScript(settings) {
const scripts = moduleScript.js;
for (const module in settings) {
switch (module) {
case 'accountManager':
case 'achievementMonitor':
case 'featuredGhostsDisplay':
case 'playlists':
break;
default:
continue;
}
const enabled = settings[module];
const path = `game/modules/${module}.js`;
if (enabled) {
scripts.push(path);
} else if (scripts.includes(path)) {
scripts.splice(scripts.indexOf(path), 1);
}
}
// return chrome.scripting.updateContentScripts([moduleScript])
await chrome.scripting.unregisterContentScripts();
return chrome.scripting.registerContentScripts(contentScripts)
}
import "./externalApi.js";