-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathindex.js
158 lines (138 loc) · 4.45 KB
/
index.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
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
import './intro';
import '@/js/browser';
import {k_msgExec, kInstall, kInvokeAPI, kResolve} from '@/js/consts';
import {DNR, getRuleIds, updateDynamicRules, updateSessionRules} from '@/js/dnr';
import {_execute, onMessage} from '@/js/msg';
import {API} from '@/js/msg-api';
import * as prefs from '@/js/prefs';
import {chromeSession} from '@/js/storage-util';
import {CHROME, FIREFOX, MOBILE, WINDOWS} from '@/js/ua';
import {sleep} from '@/js/util';
import {broadcast, pingTab} from './broadcast';
import './broadcast-injector-config';
import initBrowserCommandsApi from './browser-cmd-hotkeys';
import {setSystemDark} from './color-scheme';
import {bgBusy, bgInit, bgPreInit, dataHub} from './common';
import reinjectContentScripts from './content-scripts';
import initContextMenus from './context-menus';
import {draftsDB, mirrorStorage, prefsDB, stateDB} from './db';
import download from './download';
import {refreshIconsWhenReady, updateIconBadge} from './icon-manager';
import {setPrefs} from './prefs-api';
import setClientData from './set-client-data';
import * as styleMan from './style-manager';
import * as styleCache from './style-manager/cache';
import {dataMap} from './style-manager/util';
import initStyleViaApi from './style-via-api';
import './style-via-webrequest';
import * as syncMan from './sync-manager';
import {openEditor, openManager, openURL} from './tab-util';
import * as updateMan from './update-manager';
import * as usercssMan from './usercss-manager';
import * as usoApi from './uso-api';
import * as uswApi from './usw-api';
Object.assign(API, /** @namespace API */ {
//#region API data/db/info
data: dataHub,
//#endregion
//#region API misc actions
download,
openEditor,
openManager,
openURL,
pingTab,
setPrefs,
setSystemDark,
updateIconBadge,
//#endregion
//#region API namespaced actions
styles: styleMan,
sync: syncMan,
updater: updateMan,
usercss: usercssMan,
uso: usoApi,
usw: uswApi,
//#endregion
}, !__.MV3 && /** @namespace API */ {
//#region API for MV2
setClientData,
//#endregion
}, __.BUILD !== 'chrome' && FIREFOX && initStyleViaApi());
//#region Events
chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
if (__.BUILD !== 'firefox' && CHROME) {
reinjectContentScripts();
initContextMenus();
}
if (reason === kInstall) {
if (MOBILE) prefs.set('manage.newUI', false);
if (WINDOWS) prefs.set('editor.keyMap', 'sublime');
}
if (previousVersion === '1.5.30') {
prefsDB.delete('badFavs'); // old Stylus marked all icons as bad when network was offline
}
(bgPreInit.length ? bgPreInit : bgInit).push(
styleCache.clear(),
);
if (__.MV3) {
(bgPreInit.length ? bgPreInit : bgInit).push(
stateDB.clear(),
DNR.getDynamicRules().then(rules => updateDynamicRules(undefined, getRuleIds(rules)))
.then(() => prefs.ready)
.then(() => usercssMan.toggleUrlInstaller()),
DNR.getSessionRules().then(rules => updateSessionRules(undefined, getRuleIds(rules))),
);
refreshIconsWhenReady();
}
(async () => {
if (bgBusy) await bgBusy;
mirrorStorage(dataMap);
})();
});
if (__.MV3) {
chromeSession.get('init', async ({init}) => {
__.DEBUGLOG('new session:', !init);
if (init) return;
chromeSession.set({init: true});
onStartup();
await bgBusy;
reinjectContentScripts();
});
} else {
chrome.runtime.onStartup.addListener(onStartup);
}
async function onStartup() {
await refreshIconsWhenReady();
await sleep(1000);
const minDate = Date.now() - 30 * 24 * 60e3;
for (const id of await draftsDB.getAllKeys()) {
const {date} = await draftsDB.get(id) || {};
if (date < minDate) draftsDB.delete(id);
}
}
onMessage.set((m, sender) => {
if (m.method === kInvokeAPI) {
let res = API;
for (const p of m.path.split('.')) res = res && res[p];
if (!res) throw new Error(`Unknown API.${m.path}`);
res = res.apply({msg: m, sender}, m.args);
return res ?? null;
}
}, true);
//#endregion
(async () => {
const numPreInit = bgPreInit.length;
await Promise.all(bgPreInit);
await Promise.all(bgPreInit.slice(numPreInit)); // added by an event listener on wake-up
bgPreInit.length = 0;
await Promise.all(bgInit.map(v => typeof v === 'function' ? v() : v));
bgBusy[kResolve]();
if (__.BUILD !== 'chrome' && FIREFOX) {
initBrowserCommandsApi();
initContextMenus();
}
if (!__.MV3) {
global[k_msgExec] = _execute;
broadcast({method: 'backgroundReady'});
}
})();