-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcommon.js
240 lines (213 loc) · 6.9 KB
/
common.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import {Settings, SiteSettings} from "./utils.js";
export const DEFAULT_SCHEME = "delumine-smart";
const DEFAULT_FILTER = DEFAULT_SCHEME.split("-").slice(1).join("-");
const storeCache = {};
let settings = new Settings(DEFAULT_FILTER);
let migrationTask;
async function migrateFromLocalStorage() {
const {migrationComplete} = await chrome.storage.local.get(['migrationComplete']);
if (migrationComplete >= 2) {
return;
}
if (migrationTask) {
await migrationTask;
return;
}
migrationTask = (async () => {
try {
await chrome.offscreen.createDocument({
url: 'migrate.html',
reasons: ['LOCAL_STORAGE'],
justification: 'migrating local storage to cloud sync storage',
});
} catch {
// Already created. That's fine, just send the message.
}
const [{value: remoteSettings}, {value: {localStorage}}] = await Promise
.allSettled([
chrome.storage.sync.get(),
chrome.runtime.sendMessage({target: 'offscreen', action: 'migrate'}),
]);
if (remoteSettings) {
try {
Object.assign(storeCache, remoteSettings);
settings = Settings.import(storeCache?.sites, DEFAULT_FILTER);
} catch (err) {
console.log(`Error loading remote settings: ${JSON.stringify(err)}`);
}
}
if (localStorage) {
const {sites, ...otherSettings} = migrateV1toV2(localStorage);
// Allow local settings to override remote settings.
Object.assign(storeCache, otherSettings);
// Merge local site settings with any existing ones.
settings.import(sites);
storeCache.sites = settings.export();
// Publish the merged site settings.
await storeSet("sites", storeCache.sites);
chrome.storage.local.set({migrationComplete: 2});
}
})();
await migrationTask;
migrationTask = null;
}
function parseSiteMods(sitemods) {
// Perplexingly, I seem to have implemented settingsV1 sitemodifiers in two
// different ways: either a whitespace-delimited string or an object with
// key: true pairs.
try {
return sitemods.split(" ");
} catch { /* Not a string. */ }
try {
return Object.keys(sitemods);
} catch { /* Not an object. */ }
return [];
}
function migrateV1toV2(v1) {
const toBool = (str) => str !== "false" && Boolean(str);
const v2 = {version: 2, enabled: toBool(v1?.enabled ?? true)};
const defaultFilter = (v1?.scheme ?? DEFAULT_SCHEME)
.split("-").slice(1).join("-") || "normal"
;
const schemeToFilter = (scheme) =>
(scheme ?? `filter-${defaultFilter}`)
.split("-").slice(1).join("-") || "normal"
;
const defaultMods = [];
if (toBool(v1?.low_contrast)) {
defaultMods.push("low_contrast");
}
if (toBool(v1?.kill_background)) {
defaultMods.push("killbg");
}
if (toBool(v1?.force_text)) {
defaultMods.push("forceinput");
}
defaultMods.push("dynamic");
const settings = new Settings(defaultFilter, defaultMods);
const siteModifiers = JSON.parse(v1?.sitemodifiers ?? "{}");
const siteSchemes = JSON.parse(v1?.siteschemes ?? "{}");
const domains = new Set([
...Object.keys(siteModifiers),
...Object.keys(siteSchemes),
]);
for (const domain of domains) {
const siteSettings = new SiteSettings(
schemeToFilter(siteSchemes[domain]),
parseSiteMods(siteModifiers[domain]).map(mod => ({
"low-contrast": "low_contrast",
"kill_background": "killbg",
"force_text": "forceinput",
})[mod]).filter(Boolean),
);
settings.save(domain, siteSettings);
}
v2.sites = settings.export();
console.log(`Settings V2: ${JSON.stringify(v2)}`);
return v2;
}
export async function syncStore() {
await migrateFromLocalStorage();
return await refreshStore();
}
export async function refreshStore() {
const items = await chrome.storage.sync.get();
Object.assign(storeCache, items);
settings = Settings.import(storeCache?.sites, DEFAULT_FILTER);
settings.import((await chrome.storage.local.get("sites"))["sites"] ?? []);
return settings;
}
export function storeSet(key, value) {
storeCache[key] = value;
return chrome.storage.sync.set({[key]: value});
}
export function $(id) {
return document.getElementById(id);
}
export function getEnabled() {
return storeCache?.enabled ?? true;
}
export function setEnabled(enabled) {
return storeSet('enabled', enabled);
}
export function getSiteSettings(site) {
const siteSettings = settings.load(site);
if (!siteSettings) {
throw new Error(`Could not load settings for ${site}`);
}
return siteSettings;
}
export function setSiteSettings(site, siteSettings) {
settings.save(site, siteSettings);
storeCache.sites = settings.export();
chrome.storage.local.set({sites: settings.exportLocal()});
return storeSet("sites", storeCache.sites);
}
export async function delSiteSettings(site) {
const {sites} = await chrome.storage.sync.get("sites");
return await storeSet("sites", sites.filter(siteRow => siteRow[0] !== site));
}
export async function resetSiteSchemes() {
await chrome.storage.sync.remove(
Object.keys(await chrome.storage.sync.get())
);
for (const key of Object.keys(storeCache)) {
delete storeCache[key];
}
}
export function siteFromUrl(url) {
return new URL(url).hostname;
}
export function getGlobalSettings() {
return storeCache['settings'] ?? {};
}
export function setGlobalSetting(key, value) {
const globalSettings = getGlobalSettings();
globalSettings[key] = value;
return storeSet('settings', globalSettings);
}
export function getMatchingSite(site) {
return settings.match(site);
}
export function setSiteScheme(site, scheme) {
const siteSettings = settings.load(site);
return setSiteSettings(site, new SiteSettings(scheme, siteSettings.mods));
}
export function setDefaultModifiers(modifiers) {
const defaultSettings = settings.site_default();
return setSiteSettings("", new SiteSettings(defaultSettings.filter, modifiers));
}
export function addSiteModifier(site, modifier) {
const siteSettings = settings.load(site);
const mods = siteSettings.mods.union(new Set([modifier]));
const newSettings = new SiteSettings(siteSettings.filter, mods);
return setSiteSettings(site, newSettings);
}
export function delSiteModifier(site, modifier) {
const siteSettings = settings.load(site);
const mods = siteSettings.mods.difference(new Set([modifier]));
const newSettings = new SiteSettings(siteSettings.filter, mods);
return setSiteSettings(site, newSettings);
}
export function changedFromDefault(site) {
return !settings.site_default.equals(settings.load(site));
}
export function isFileUrl(url) {
try {
return (new URL(url)).origin === "file://";
} catch {
return false;
}
}
export function isDisallowedUrl(url) {
if (url.indexOf('about') == 0) {
return true;
} else if (url.indexOf('chrome') == 0) {
// Special case the "newtab" page, which this extension affects.
if (siteFromUrl(url) == 'newtab')
return false;
else
return true;
}
return false;
}