2021-01-01 14:27:58 +00:00
|
|
|
/* global API msg */// msg.js
|
2021-05-27 11:18:28 +00:00
|
|
|
/* global CHROME URLS stringAsRegExp tryRegExp tryURL */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global bgReady compareRevision */// common.js
|
|
|
|
/* global calcStyleDigest styleCodeEmpty styleSectionGlobal */// sections-util.js
|
|
|
|
/* global db */
|
|
|
|
/* global prefs */
|
|
|
|
/* global tabMan */
|
2021-03-05 14:25:05 +00:00
|
|
|
/* global usercssMan */
|
2018-11-07 06:09:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
This style manager is a layer between content script and the DB. When a style
|
|
|
|
is added/updated, it broadcast a message to content script and the content
|
|
|
|
script would try to fetch the new code.
|
|
|
|
|
|
|
|
The live preview feature relies on `runtime.connect` and `port.onDisconnect`
|
2021-01-01 14:27:58 +00:00
|
|
|
to cleanup the temporary code. See livePreview in /edit.
|
2018-11-07 06:09:29 +00:00
|
|
|
*/
|
2020-11-18 11:17:15 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
const styleMan = (() => {
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#region Declarations
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @typedef {{
|
|
|
|
style: StyleObj
|
|
|
|
preview?: StyleObj
|
|
|
|
appliesTo: Set<string>
|
|
|
|
}} StyleMapData */
|
|
|
|
/** @type {Map<number,StyleMapData>} */
|
|
|
|
const dataMap = new Map();
|
|
|
|
const uuidIndex = new Map();
|
|
|
|
/** @typedef {Object<styleId,{id: number, code: string[]}>} StyleSectionsToApply */
|
|
|
|
/** @type {Map<string,{maybeMatch: Set<styleId>, sections: StyleSectionsToApply}>} */
|
2018-11-07 06:09:29 +00:00
|
|
|
const cachedStyleForUrl = createCache({
|
2021-01-01 14:27:58 +00:00
|
|
|
onDeleted(url, cache) {
|
2018-11-07 06:09:29 +00:00
|
|
|
for (const section of Object.values(cache.sections)) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const data = id2data(section.id);
|
|
|
|
if (data) data.appliesTo.delete(url);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
const BAD_MATCHER = {test: () => false};
|
|
|
|
const compileRe = createCompiler(text => `^(${text})$`);
|
|
|
|
const compileSloppyRe = createCompiler(text => `^${text}$`);
|
2019-06-11 14:44:32 +00:00
|
|
|
const compileExclusion = createCompiler(buildExclusion);
|
2021-01-01 14:27:58 +00:00
|
|
|
const MISSING_PROPS = {
|
|
|
|
name: style => `ID: ${style.id}`,
|
|
|
|
_id: () => uuidv4(),
|
|
|
|
_rev: () => Date.now(),
|
2019-06-11 14:44:32 +00:00
|
|
|
};
|
2021-01-26 13:33:17 +00:00
|
|
|
const DELETE_IF_NULL = ['id', 'customName', 'md5Url', 'originalMd5'];
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
|
|
|
|
let ready = init();
|
2020-10-11 15:12:06 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
chrome.runtime.onConnect.addListener(handleLivePreview);
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#region Exports
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
/** @returns {Promise<number>} style id */
|
|
|
|
async delete(id, reason) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const data = id2data(id);
|
|
|
|
await db.exec('delete', id);
|
|
|
|
if (reason !== 'sync') {
|
|
|
|
API.sync.delete(data.style._id, Date.now());
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const url of data.appliesTo) {
|
|
|
|
const cache = cachedStyleForUrl.get(url);
|
|
|
|
if (cache) delete cache.sections[id];
|
|
|
|
}
|
|
|
|
dataMap.delete(id);
|
|
|
|
uuidIndex.delete(data.style._id);
|
|
|
|
await msg.broadcast({
|
|
|
|
method: 'styleDeleted',
|
|
|
|
style: {id},
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
return id;
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<number>} style id */
|
|
|
|
async deleteByUUID(_id, rev) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const id = uuidIndex.get(_id);
|
|
|
|
const oldDoc = id && id2style(id);
|
|
|
|
if (oldDoc && compareRevision(oldDoc._rev, rev) <= 0) {
|
|
|
|
// FIXME: does it make sense to set reason to 'sync' in deleteByUUID?
|
|
|
|
return styleMan.delete(id, 'sync');
|
|
|
|
}
|
|
|
|
},
|
2019-03-03 22:54:37 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
async editSave(style) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
style = mergeWithMapped(style);
|
|
|
|
style.updateDate = Date.now();
|
|
|
|
return handleSave(await saveStyle(style), 'editSave');
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<?StyleObj>} */
|
|
|
|
async find(filter) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const filterEntries = Object.entries(filter);
|
|
|
|
for (const {style} of dataMap.values()) {
|
|
|
|
if (filterEntries.every(([key, val]) => style[key] === val)) {
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj[]>} */
|
|
|
|
async getAll() {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
return Array.from(dataMap.values(), data2style);
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
async getByUUID(uuid) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
return id2style(uuidIndex.get(uuid));
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleSectionsToApply>} */
|
|
|
|
async getSectionsByUrl(url, id, isInitialApply) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
if (isInitialApply && prefs.get('disableAll')) {
|
|
|
|
return {disableAll: true};
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-02-28 15:01:26 +00:00
|
|
|
if (CHROME && this && this.sender) {
|
|
|
|
/* Chrome hides text frament from location.href of the page e.g. #:~:text=foo
|
|
|
|
so we'll use the real URL reported by webNavigation API.
|
|
|
|
TODO: if FF will do the same, this won't work as is: FF reports onCommitted too late */
|
|
|
|
const {tab, frameId} = this.sender;
|
|
|
|
url = tab && tabMan.get(tab.id, 'url', frameId) || url;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
let cache = cachedStyleForUrl.get(url);
|
|
|
|
if (!cache) {
|
|
|
|
cache = {
|
|
|
|
sections: {},
|
|
|
|
maybeMatch: new Set(),
|
|
|
|
};
|
|
|
|
buildCache(cache, url, dataMap.values());
|
|
|
|
cachedStyleForUrl.set(url, cache);
|
|
|
|
} else if (cache.maybeMatch.size) {
|
|
|
|
buildCache(cache, url, Array.from(cache.maybeMatch, id2data).filter(Boolean));
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
? cache.sections[id] ? {[id]: cache.sections[id]} : {}
|
|
|
|
: cache.sections;
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
async get(id) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
return id2style(id);
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StylesByUrlResult[]>} */
|
|
|
|
async getByUrl(url, id = null) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
// FIXME: do we want to cache this? Who would like to open popup rapidly
|
|
|
|
// or search the DB with the same URL?
|
|
|
|
const result = [];
|
|
|
|
const styles = id
|
|
|
|
? [id2style(id)].filter(Boolean)
|
|
|
|
: Array.from(dataMap.values(), data2style);
|
|
|
|
const query = createMatchQuery(url);
|
|
|
|
for (const style of styles) {
|
|
|
|
let excluded = false;
|
|
|
|
let sloppy = false;
|
|
|
|
let sectionMatched = false;
|
|
|
|
const match = urlMatchStyle(query, style);
|
|
|
|
// TODO: enable this when the function starts returning false
|
|
|
|
// if (match === false) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
if (match === 'excluded') {
|
|
|
|
excluded = true;
|
|
|
|
}
|
|
|
|
for (const section of style.sections) {
|
|
|
|
if (styleSectionGlobal(section) && styleCodeEmpty(section.code)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const match = urlMatchSection(query, section);
|
|
|
|
if (match) {
|
|
|
|
if (match === 'sloppy') {
|
|
|
|
sloppy = true;
|
|
|
|
}
|
|
|
|
sectionMatched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sectionMatched) {
|
|
|
|
result.push(/** @namespace StylesByUrlResult */ {style, excluded, sloppy});
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
return result;
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj[]>} */
|
|
|
|
async importMany(items) {
|
|
|
|
if (ready.then) await ready;
|
2021-03-05 14:25:05 +00:00
|
|
|
for (const style of items) {
|
|
|
|
beforeSave(style);
|
|
|
|
if (style.sourceCode && style.usercssData) {
|
|
|
|
await usercssMan.buildCode(style);
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const events = await db.exec('putMany', items);
|
|
|
|
return Promise.all(items.map((item, i) => {
|
|
|
|
afterSave(item, events[i]);
|
|
|
|
return handleSave(item, 'import');
|
|
|
|
}));
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
async install(style, reason = null) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
reason = reason || dataMap.has(style.id) ? 'update' : 'install';
|
|
|
|
style = mergeWithMapped(style);
|
|
|
|
const url = !style.url && style.updateUrl && (
|
|
|
|
URLS.extractUsoArchiveInstallUrl(style.updateUrl) ||
|
2021-04-20 17:40:04 +00:00
|
|
|
URLS.extractGreasyForkInstallUrl(style.updateUrl) ||
|
|
|
|
URLS.extractUSwInstallUrl(style.updateUrl)
|
2021-01-01 14:27:58 +00:00
|
|
|
);
|
|
|
|
if (url) style.url = style.installationUrl = url;
|
|
|
|
style.originalDigest = await calcStyleDigest(style);
|
|
|
|
// FIXME: update updateDate? what about usercss config?
|
|
|
|
return handleSave(await saveStyle(style), reason);
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<?StyleObj>} */
|
|
|
|
async putByUUID(doc) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const id = uuidIndex.get(doc._id);
|
|
|
|
if (id) {
|
|
|
|
doc.id = id;
|
|
|
|
} else {
|
|
|
|
delete doc.id;
|
|
|
|
}
|
|
|
|
const oldDoc = id && id2style(id);
|
|
|
|
let diff = -1;
|
|
|
|
if (oldDoc) {
|
|
|
|
diff = compareRevision(oldDoc._rev, doc._rev);
|
|
|
|
if (diff > 0) {
|
|
|
|
API.sync.put(oldDoc._id, oldDoc._rev);
|
|
|
|
return;
|
2018-11-11 06:04:22 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
if (diff < 0) {
|
|
|
|
doc.id = await db.exec('put', doc);
|
|
|
|
uuidIndex.set(doc._id, doc.id);
|
|
|
|
return handleSave(doc, 'sync');
|
|
|
|
}
|
|
|
|
},
|
2018-11-11 06:04:22 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<number>} style id */
|
|
|
|
async toggle(id, enabled) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const style = Object.assign({}, id2style(id), {enabled});
|
|
|
|
handleSave(await saveStyle(style), 'toggle', false);
|
|
|
|
return id;
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
// using bind() to skip step-into when debugging
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
addExclusion: addIncludeExclude.bind(null, 'exclusions'),
|
|
|
|
/** @returns {Promise<StyleObj>} */
|
|
|
|
addInclusion: addIncludeExclude.bind(null, 'inclusions'),
|
|
|
|
/** @returns {Promise<?StyleObj>} */
|
|
|
|
removeExclusion: removeIncludeExclude.bind(null, 'exclusions'),
|
|
|
|
/** @returns {Promise<?StyleObj>} */
|
|
|
|
removeInclusion: removeIncludeExclude.bind(null, 'inclusions'),
|
|
|
|
};
|
2019-03-03 22:54:37 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Implementation
|
|
|
|
|
|
|
|
/** @returns {StyleMapData} */
|
|
|
|
function id2data(id) {
|
|
|
|
return dataMap.get(id);
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {?StyleObj} */
|
|
|
|
function id2style(id) {
|
|
|
|
return (dataMap.get(id) || {}).style;
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {?StyleObj} */
|
|
|
|
function data2style(data) {
|
|
|
|
return data && data.style;
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {StyleObj} */
|
|
|
|
function createNewStyle() {
|
|
|
|
return /** @namespace StyleObj */ {
|
|
|
|
enabled: true,
|
|
|
|
updateUrl: null,
|
|
|
|
md5Url: null,
|
|
|
|
url: null,
|
|
|
|
originalMd5: null,
|
|
|
|
installDate: Date.now(),
|
|
|
|
};
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {void} */
|
|
|
|
function storeInMap(style) {
|
|
|
|
dataMap.set(style.id, {
|
|
|
|
style,
|
|
|
|
appliesTo: new Set(),
|
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @returns {StyleObj} */
|
|
|
|
function mergeWithMapped(style) {
|
|
|
|
return Object.assign({},
|
|
|
|
id2style(style.id) || createNewStyle(),
|
|
|
|
style);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function handleLivePreview(port) {
|
|
|
|
if (port.name !== 'livePreview') {
|
|
|
|
return;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
let id;
|
|
|
|
port.onMessage.addListener(style => {
|
|
|
|
if (!id) id = style.id;
|
|
|
|
const data = id2data(id);
|
|
|
|
data.preview = style;
|
|
|
|
broadcastStyleUpdated(style, 'editPreview');
|
|
|
|
});
|
|
|
|
port.onDisconnect.addListener(() => {
|
|
|
|
port = null;
|
|
|
|
if (id) {
|
|
|
|
const data = id2data(id);
|
|
|
|
if (data) {
|
|
|
|
data.preview = null;
|
|
|
|
broadcastStyleUpdated(data.style, 'editPreviewEnd');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function addIncludeExclude(type, id, rule) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const style = Object.assign({}, id2style(id));
|
|
|
|
const list = style[type] || (style[type] = []);
|
|
|
|
if (list.includes(rule)) {
|
|
|
|
throw new Error('The rule already exists');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
style[type] = list.concat([rule]);
|
|
|
|
return handleSave(await saveStyle(style), 'styleSettings');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function removeIncludeExclude(type, id, rule) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
const style = Object.assign({}, id2style(id));
|
|
|
|
const list = style[type];
|
|
|
|
if (!list || !list.includes(rule)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
style[type] = list.filter(r => r !== rule);
|
|
|
|
return handleSave(await saveStyle(style), 'styleSettings');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function broadcastStyleUpdated(style, reason, method = 'styleUpdated', codeIsUpdated = true) {
|
|
|
|
const {id} = style;
|
|
|
|
const data = id2data(id);
|
2018-11-07 06:09:29 +00:00
|
|
|
const excluded = new Set();
|
|
|
|
const updated = new Set();
|
|
|
|
for (const [url, cache] of cachedStyleForUrl.entries()) {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!data.appliesTo.has(url)) {
|
|
|
|
cache.maybeMatch.add(id);
|
2018-11-07 06:09:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const code = getAppliedCode(createMatchQuery(url), style);
|
|
|
|
if (code) {
|
2018-11-07 06:09:29 +00:00
|
|
|
updated.add(url);
|
2021-01-01 14:27:58 +00:00
|
|
|
cache.sections[id] = {id, code};
|
|
|
|
} else {
|
|
|
|
excluded.add(url);
|
|
|
|
delete cache.sections[id];
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
data.appliesTo = updated;
|
2018-11-07 06:09:29 +00:00
|
|
|
return msg.broadcast({
|
|
|
|
method,
|
|
|
|
reason,
|
2020-11-18 11:17:15 +00:00
|
|
|
codeIsUpdated,
|
2021-01-01 14:27:58 +00:00
|
|
|
style: {
|
|
|
|
id,
|
|
|
|
md5Url: style.md5Url,
|
|
|
|
enabled: style.enabled,
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
function beforeSave(style) {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!style.name) {
|
2021-01-01 14:27:58 +00:00
|
|
|
throw new Error('Style name is empty');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-10-11 15:12:06 +00:00
|
|
|
for (const key of DELETE_IF_NULL) {
|
|
|
|
if (style[key] == null) {
|
|
|
|
delete style[key];
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
if (!style._id) {
|
2020-02-20 12:17:15 +00:00
|
|
|
style._id = uuidv4();
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
style._rev = Date.now();
|
2018-11-07 06:09:29 +00:00
|
|
|
fixUsoMd5Issue(style);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function afterSave(style, newId) {
|
|
|
|
if (style.id == null) {
|
|
|
|
style.id = newId;
|
|
|
|
}
|
|
|
|
uuidIndex.set(style._id, style.id);
|
2021-01-01 14:27:58 +00:00
|
|
|
API.sync.put(style._id, style._rev);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function saveStyle(style) {
|
2019-11-05 19:30:45 +00:00
|
|
|
beforeSave(style);
|
2021-01-01 14:27:58 +00:00
|
|
|
const newId = await db.exec('put', style);
|
|
|
|
afterSave(style, newId);
|
|
|
|
return style;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function handleSave(style, reason, codeIsUpdated) {
|
|
|
|
const data = id2data(style.id);
|
|
|
|
const method = data ? 'styleUpdated' : 'styleAdded';
|
|
|
|
if (!data) {
|
|
|
|
storeInMap(style);
|
2018-11-07 06:09:29 +00:00
|
|
|
} else {
|
2021-01-01 14:27:58 +00:00
|
|
|
data.style = style;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
broadcastStyleUpdated(style, reason, method, codeIsUpdated);
|
|
|
|
return style;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get styles matching a URL, including sloppy regexps and excluded items.
|
2019-06-11 14:44:32 +00:00
|
|
|
function getAppliedCode(query, data) {
|
|
|
|
if (urlMatchStyle(query, data) !== true) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const code = [];
|
|
|
|
for (const section of data.sections) {
|
2019-06-11 14:44:32 +00:00
|
|
|
if (urlMatchSection(query, section) === true && !styleCodeEmpty(section.code)) {
|
2018-11-07 06:09:29 +00:00
|
|
|
code.push(section.code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return code.length && code;
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function init() {
|
|
|
|
const styles = await db.exec('getAll') || [];
|
|
|
|
const updated = styles.filter(style =>
|
|
|
|
addMissingProps(style) +
|
|
|
|
addCustomName(style));
|
|
|
|
if (updated.length) {
|
|
|
|
await db.exec('putMany', updated);
|
|
|
|
}
|
|
|
|
for (const style of styles) {
|
|
|
|
fixUsoMd5Issue(style);
|
|
|
|
storeInMap(style);
|
|
|
|
uuidIndex.set(style._id, style.id);
|
|
|
|
}
|
|
|
|
ready = true;
|
|
|
|
bgReady._resolveStyles();
|
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function addMissingProps(style) {
|
|
|
|
let res = 0;
|
|
|
|
for (const key in MISSING_PROPS) {
|
|
|
|
if (!style[key]) {
|
|
|
|
style[key] = MISSING_PROPS[key](style);
|
|
|
|
res = 1;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Upgrades the old way of customizing local names */
|
|
|
|
function addCustomName(style) {
|
|
|
|
let res = 0;
|
|
|
|
const {originalName} = style;
|
|
|
|
if (originalName) {
|
|
|
|
res = 1;
|
|
|
|
if (originalName !== style.name) {
|
|
|
|
style.customName = style.name;
|
|
|
|
style.name = originalName;
|
2020-10-11 15:12:06 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
delete style.originalName;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
return res;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:44:32 +00:00
|
|
|
function urlMatchStyle(query, style) {
|
|
|
|
if (
|
|
|
|
style.exclusions &&
|
|
|
|
style.exclusions.some(e => compileExclusion(e).test(query.urlWithoutParams))
|
|
|
|
) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return 'excluded';
|
|
|
|
}
|
|
|
|
if (!style.enabled) {
|
|
|
|
return 'disabled';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:44:32 +00:00
|
|
|
function urlMatchSection(query, section) {
|
|
|
|
if (
|
|
|
|
section.domains &&
|
|
|
|
section.domains.some(d => d === query.domain || query.domain.endsWith(`.${d}`))
|
|
|
|
) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-10-03 17:17:59 +00:00
|
|
|
if (section.urlPrefixes && section.urlPrefixes.some(p => p && query.url.startsWith(p))) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// as per spec the fragment portion is ignored in @-moz-document:
|
|
|
|
// https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#url-of-doc
|
|
|
|
// but the spec is outdated and doesn't account for SPA sites
|
|
|
|
// so we only respect it for `url()` function
|
|
|
|
if (section.urls && (
|
2019-06-11 14:44:32 +00:00
|
|
|
section.urls.includes(query.url) ||
|
|
|
|
section.urls.includes(query.urlWithoutHash)
|
2018-11-07 06:09:29 +00:00
|
|
|
)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-11 14:44:32 +00:00
|
|
|
if (section.regexps && section.regexps.some(r => compileRe(r).test(query.url))) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
According to CSS4 @document specification the entire URL must match.
|
|
|
|
Stylish-for-Chrome implemented it incorrectly since the very beginning.
|
|
|
|
We'll detect styles that abuse the bug by finding the sections that
|
|
|
|
would have been applied by Stylish but not by us as we follow the spec.
|
|
|
|
*/
|
2019-06-11 14:44:32 +00:00
|
|
|
if (section.regexps && section.regexps.some(r => compileSloppyRe(r).test(query.url))) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return 'sloppy';
|
|
|
|
}
|
|
|
|
// TODO: check for invalid regexps?
|
2020-10-04 10:42:06 +00:00
|
|
|
return styleSectionGlobal(section);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createCompiler(compile) {
|
|
|
|
// FIXME: FIFO cache doesn't work well here, if we want to match many
|
|
|
|
// regexps more than the cache size, we will never hit the cache because
|
|
|
|
// the first cache is deleted. So we use a simple map but it leaks memory.
|
|
|
|
const cache = new Map();
|
|
|
|
return text => {
|
|
|
|
let re = cache.get(text);
|
|
|
|
if (!re) {
|
|
|
|
re = tryRegExp(compile(text));
|
|
|
|
if (!re) {
|
|
|
|
re = BAD_MATCHER;
|
|
|
|
}
|
|
|
|
cache.set(text, re);
|
|
|
|
}
|
|
|
|
return re;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:44:32 +00:00
|
|
|
function compileGlob(text) {
|
2021-01-01 14:27:58 +00:00
|
|
|
return stringAsRegExp(text, '', true)
|
|
|
|
.replace(/\\\\\\\*|\\\*/g, m => m.length > 2 ? m : '.*');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:44:32 +00:00
|
|
|
function buildExclusion(text) {
|
|
|
|
// match pattern
|
|
|
|
const match = text.match(/^(\*|[\w-]+):\/\/(\*\.)?([\w.]+\/.*)/);
|
|
|
|
if (!match) {
|
|
|
|
return '^' + compileGlob(text) + '$';
|
|
|
|
}
|
|
|
|
return '^' +
|
|
|
|
(match[1] === '*' ? '[\\w-]+' : match[1]) +
|
|
|
|
'://' +
|
|
|
|
(match[2] ? '(?:[\\w.]+\\.)?' : '') +
|
|
|
|
compileGlob(match[3]) +
|
|
|
|
'$';
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The md5Url provided by USO includes a duplicate "update" subdomain (see #523),
|
|
|
|
// This fixes any already installed styles containing this error
|
|
|
|
function fixUsoMd5Issue(style) {
|
|
|
|
if (style && style.md5Url && style.md5Url.includes('update.update.userstyles')) {
|
|
|
|
style.md5Url = style.md5Url.replace('update.update.userstyles', 'update.userstyles');
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 14:44:32 +00:00
|
|
|
|
|
|
|
function createMatchQuery(url) {
|
|
|
|
let urlWithoutHash;
|
|
|
|
let urlWithoutParams;
|
|
|
|
let domain;
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
get urlWithoutHash() {
|
|
|
|
if (!urlWithoutHash) {
|
|
|
|
urlWithoutHash = url.split('#')[0];
|
|
|
|
}
|
|
|
|
return urlWithoutHash;
|
|
|
|
},
|
|
|
|
get urlWithoutParams() {
|
|
|
|
if (!urlWithoutParams) {
|
2021-05-27 11:18:28 +00:00
|
|
|
const u = tryURL(url);
|
2019-06-11 14:44:32 +00:00
|
|
|
urlWithoutParams = u.origin + u.pathname;
|
|
|
|
}
|
|
|
|
return urlWithoutParams;
|
|
|
|
},
|
|
|
|
get domain() {
|
|
|
|
if (!domain) {
|
2021-05-27 11:18:28 +00:00
|
|
|
const u = tryURL(url);
|
2019-06-11 14:44:32 +00:00
|
|
|
domain = u.hostname;
|
|
|
|
}
|
|
|
|
return domain;
|
2020-11-18 11:17:15 +00:00
|
|
|
},
|
2019-06-11 14:44:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function buildCache(cache, url, styleList) {
|
|
|
|
const query = createMatchQuery(url);
|
|
|
|
for (const {style, appliesTo, preview} of styleList) {
|
|
|
|
const code = getAppliedCode(query, preview || style);
|
|
|
|
if (code) {
|
|
|
|
const id = style.id;
|
|
|
|
cache.sections[id] = {id, code};
|
|
|
|
appliesTo.add(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:27:29 +00:00
|
|
|
function uuidv4() {
|
|
|
|
const seeds = crypto.getRandomValues(new Uint16Array(8));
|
|
|
|
// 00001111-2222-M333-N444-555566667777
|
|
|
|
seeds[3] = seeds[3] & 0x0FFF | 0x4000; // UUID version 4, M = 4
|
|
|
|
seeds[4] = seeds[4] & 0x3FFF | 0x8000; // UUID variant 1, N = 8..0xB
|
|
|
|
return Array.from(seeds, hex4dashed).join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** uuidv4 helper: converts to a 4-digit hex string and adds "-" at required positions */
|
|
|
|
function hex4dashed(num, i) {
|
|
|
|
return (num + 0x10000).toString(16).slice(-4) + (i >= 1 && i <= 4 ? '-' : '');
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
//#endregion
|
2018-11-07 06:09:29 +00:00
|
|
|
})();
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
/** Creates a FIFO limit-size map. */
|
|
|
|
function createCache({size = 1000, onDeleted} = {}) {
|
|
|
|
const map = new Map();
|
|
|
|
const buffer = Array(size);
|
|
|
|
let index = 0;
|
|
|
|
let lastIndex = 0;
|
|
|
|
return {
|
|
|
|
get(id) {
|
|
|
|
const item = map.get(id);
|
|
|
|
return item && item.data;
|
|
|
|
},
|
|
|
|
set(id, data) {
|
|
|
|
if (map.size === size) {
|
|
|
|
// full
|
|
|
|
map.delete(buffer[lastIndex].id);
|
|
|
|
if (onDeleted) {
|
|
|
|
onDeleted(buffer[lastIndex].id, buffer[lastIndex].data);
|
|
|
|
}
|
|
|
|
lastIndex = (lastIndex + 1) % size;
|
|
|
|
}
|
|
|
|
const item = {id, data, index};
|
|
|
|
map.set(id, item);
|
|
|
|
buffer[index] = item;
|
|
|
|
index = (index + 1) % size;
|
|
|
|
},
|
|
|
|
delete(id) {
|
|
|
|
const item = map.get(id);
|
|
|
|
if (!item) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
map.delete(item.id);
|
|
|
|
const lastItem = buffer[lastIndex];
|
|
|
|
lastItem.index = item.index;
|
|
|
|
buffer[item.index] = lastItem;
|
|
|
|
lastIndex = (lastIndex + 1) % size;
|
|
|
|
if (onDeleted) {
|
|
|
|
onDeleted(item.id, item.data);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
clear() {
|
|
|
|
map.clear();
|
|
|
|
index = lastIndex = 0;
|
|
|
|
},
|
|
|
|
has: id => map.has(id),
|
|
|
|
*entries() {
|
|
|
|
for (const [id, item] of map) {
|
|
|
|
yield [id, item.data];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
*values() {
|
|
|
|
for (const item of map.values()) {
|
|
|
|
yield item.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
get size() {
|
|
|
|
return map.size;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|