-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathset-client-data.js
93 lines (90 loc) · 3.04 KB
/
set-client-data.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
import {kPopup, UCD} from '@/js/consts';
import {API} from '@/js/msg-api';
import * as prefs from '@/js/prefs';
import {FIREFOX} from '@/js/ua';
import {isDark, setSystemDark} from './color-scheme';
import {bgBusy, dataHub, isVivaldi} from './common';
import {prefsDB, stateDB} from './db';
import makePopupData from './popup-data';
import {nondefaults} from './prefs-api';
import * as styleMan from './style-manager';
import {webRequestBlocking} from './style-via-webrequest';
import * as syncMan from './sync-manager';
import * as usercssTemplate from './usercss-template';
const kEditorScrollInfo = 'editorScrollInfo';
/** @type {ResponseInit} */
const RESPONSE_INIT = {
headers: {'cache-control': 'no-cache'},
};
const ASSIGN_FUNC_STR = __.MV3 && `${function (data) {
Object.assign(this[__.CLIENT_DATA], data);
}}`;
const PROVIDERS = {
edit(url) {
const id = +url.searchParams.get('id');
const style = styleMan.get(id);
const isUC = style ? UCD in style : prefs.__values.newStyleAsUsercss;
const siKey = kEditorScrollInfo + id;
return /** @namespace StylusClientData */ {
style,
isUC,
si: style && (__.MV3 ? stateDB.get(siKey) : dataHub[siKey]),
template: !style && isUC && (usercssTemplate.value || usercssTemplate.load()),
};
},
manage(url) {
const sp = url.searchParams;
const query = sp.get('search') || undefined/*to enable client's parameter default value*/;
return /** @namespace StylusClientData */ {
badFavs: prefs.__values['manage.newUI']
&& prefs.__values['manage.newUI.favicons']
&& prefsDB.get('badFavs'),
ids: query
&& styleMan.searchDb({
query,
mode: sp.get('searchMode') || prefs.__values['manage.searchMode'],
}),
styles: styleMan.getCore({sections: true, size: true}),
sync: syncMan.getStatus(true),
};
},
options: () => {
const status = syncMan.getStatus();
const {drive} = status;
return /** @namespace StylusClientData */ {
sync: status,
syncOpts: drive ? syncMan.getDriveOptions(drive) : {},
wrb: webRequestBlocking,
};
},
popup: () => ({
[kPopup]: dataHub.pop(kPopup) || makePopupData(),
}),
};
/** @namespace API */
Object.assign(API, {
saveScroll(id, info) {
if (__.MV3) stateDB.put(info, kEditorScrollInfo + id);
else dataHub[kEditorScrollInfo + id] = info;
},
});
export default async function setClientData({
dark: pageDark,
url: pageUrl,
} = {}) {
setSystemDark(pageDark);
if (bgBusy) await bgBusy;
const url = new URL(pageUrl);
const page = url.pathname.slice(1/*"/"*/, -5/*".html"*/);
const jobs = /** @namespace StylusClientData */ Object.assign({
apply: styleMan.getSectionsByUrl(pageUrl, {init: true}),
dark: isDark,
favicon: FIREFOX || isVivaldi,
prefs: nondefaults,
}, PROVIDERS[page]?.(url));
const results = await Promise.all(Object.values(jobs));
Object.keys(jobs).forEach((id, i) => (jobs[id] = results[i]));
return __.MV3
? new Response(`(${ASSIGN_FUNC_STR})(${JSON.stringify(jobs)})`, RESPONSE_INIT)
: jobs;
}