2018-09-25 13:14:46 +00:00
|
|
|
/* global loadScript semverCompare colorConverter styleCodeEmpty backgroundWorker */
|
2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-var
|
2017-10-29 17:22:10 +00:00
|
|
|
var usercss = (() => {
|
2018-09-25 13:14:46 +00:00
|
|
|
const GLOBAL_METAS = {
|
|
|
|
author: undefined,
|
|
|
|
description: undefined,
|
|
|
|
homepageURL: 'url',
|
|
|
|
// updateURL: 'updateUrl',
|
|
|
|
name: undefined,
|
2017-08-05 16:49:25 +00:00
|
|
|
};
|
2018-09-25 13:14:46 +00:00
|
|
|
const RX_META = /\/\*\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i;
|
|
|
|
return {buildMeta, buildCode, assignVars};
|
2017-11-27 09:44:16 +00:00
|
|
|
|
2017-11-09 00:49:10 +00:00
|
|
|
function buildMeta(sourceCode) {
|
2017-09-18 00:16:11 +00:00
|
|
|
sourceCode = sourceCode.replace(/\r\n?/g, '\n');
|
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
const style = {
|
2017-12-06 08:03:43 +00:00
|
|
|
reason: 'install',
|
2017-08-05 16:49:25 +00:00
|
|
|
enabled: true,
|
2017-09-16 01:24:50 +00:00
|
|
|
sourceCode,
|
2018-09-25 13:14:46 +00:00
|
|
|
sections: []
|
2017-08-05 16:49:25 +00:00
|
|
|
};
|
|
|
|
|
2018-09-25 13:14:46 +00:00
|
|
|
const match = sourceCode.match(RX_META);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error('can not find metadata');
|
2017-09-05 00:16:08 +00:00
|
|
|
}
|
2017-11-09 00:49:10 +00:00
|
|
|
|
2018-09-25 13:14:46 +00:00
|
|
|
return backgroundWorker.parseUsercssMeta(match[0], match.index)
|
|
|
|
.then(({metadata}) => {
|
|
|
|
style.usercssData = metadata;
|
|
|
|
for (const [key, value = key] of Object.entries(GLOBAL_METAS)) {
|
|
|
|
style[value] = metadata[key];
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
});
|
2017-10-06 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 13:36:30 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} style
|
|
|
|
* @param {Boolean} [allowErrors=false]
|
|
|
|
* @returns {(Style | {style: Style, errors: (false|String[])})} - style object
|
|
|
|
* when allowErrors is falsy or {style, errors} object when allowErrors is truthy
|
|
|
|
*/
|
|
|
|
function buildCode(style, allowErrors) {
|
2018-09-25 13:14:46 +00:00
|
|
|
const match = style.sourceCode.match(RX_META);
|
|
|
|
return backgroundWorker.compileUsercss(
|
|
|
|
style.usercssData.preprocessor,
|
|
|
|
style.sourceCode.slice(0, match.index) + style.sourceCode.slice(match.index + match[0].length),
|
|
|
|
style.usercssData.vars
|
|
|
|
)
|
2018-01-07 13:36:30 +00:00
|
|
|
.then(({sections, errors}) => {
|
|
|
|
if (!errors.length) errors = false;
|
|
|
|
if (!sections.length || errors && !allowErrors) {
|
2018-09-25 13:14:46 +00:00
|
|
|
throw errors;
|
2018-01-07 13:36:30 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
style.sections = sections;
|
2018-01-07 13:36:30 +00:00
|
|
|
return allowErrors ? {style, errors} : style;
|
2017-09-15 05:40:04 +00:00
|
|
|
});
|
2017-09-09 11:29:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:47:15 +00:00
|
|
|
function assignVars(style, oldStyle) {
|
2017-09-16 01:24:50 +00:00
|
|
|
const {usercssData: {vars}} = style;
|
2017-11-01 02:47:15 +00:00
|
|
|
const {usercssData: {vars: oldVars}} = oldStyle;
|
2018-09-25 13:38:40 +00:00
|
|
|
if (!vars || !oldVars) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-09-09 11:29:35 +00:00
|
|
|
// The type of var might be changed during the update. Set value to null if the value is invalid.
|
2017-09-16 01:24:50 +00:00
|
|
|
for (const key of Object.keys(vars)) {
|
|
|
|
if (oldVars[key] && oldVars[key].value) {
|
|
|
|
vars[key].value = oldVars[key].value;
|
2017-09-09 11:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 13:14:46 +00:00
|
|
|
return backgroundWorker.nullifyInvalidVars(vars)
|
|
|
|
.then(vars => {
|
|
|
|
style.usercssData.vars = vars;
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
|
|
|
})();
|