2018-10-11 12:00:25 +00:00
|
|
|
/* global backgroundWorker */
|
2018-10-10 15:05:20 +00:00
|
|
|
/* exported usercss */
|
2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-10 15:05:20 +00:00
|
|
|
const 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;
|
2018-09-26 02:33:02 +00:00
|
|
|
const ERR_ARGS_IS_LIST = new Set(['missingMandatory', 'missingChar']);
|
2018-09-25 13:14:46 +00:00
|
|
|
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-08-05 16:49:25 +00:00
|
|
|
|
2018-09-25 13:14:46 +00:00
|
|
|
return backgroundWorker.parseUsercssMeta(match[0], match.index)
|
2018-09-26 02:33:02 +00:00
|
|
|
.catch(err => {
|
|
|
|
if (err.code) {
|
2018-09-26 03:37:50 +00:00
|
|
|
const args = ERR_ARGS_IS_LIST.has(err.code) ? drawList(err.args) : err.args;
|
2018-09-26 02:33:02 +00:00
|
|
|
const message = chrome.i18n.getMessage(`meta_${err.code}`, args);
|
|
|
|
if (message) {
|
|
|
|
err.message = message;
|
2017-11-09 07:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 02:33:02 +00:00
|
|
|
throw err;
|
|
|
|
})
|
2018-09-25 13:14:46 +00:00
|
|
|
.then(({metadata}) => {
|
|
|
|
style.usercssData = metadata;
|
|
|
|
for (const [key, value = key] of Object.entries(GLOBAL_METAS)) {
|
|
|
|
style[value] = metadata[key];
|
2017-11-09 07:53:09 +00:00
|
|
|
}
|
2018-09-25 13:14:46 +00:00
|
|
|
return style;
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 03:40:04 +00:00
|
|
|
function drawList(items) {
|
|
|
|
return items.map(i => i.length === 1 ? JSON.stringify(i) : i).join(', ');
|
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
|
|
|
}
|
|
|
|
})();
|