2017-11-24 16:33:50 +00:00
|
|
|
/* global usercss saveStyle getStyles chromeLocal */
|
2017-09-18 03:34:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-var
|
2017-10-29 17:22:10 +00:00
|
|
|
var usercssHelper = (() => {
|
2017-09-18 03:34:12 +00:00
|
|
|
function buildMeta(style) {
|
|
|
|
if (style.usercssData) {
|
|
|
|
return Promise.resolve(style);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const {sourceCode} = style;
|
|
|
|
// allow sourceCode to be normalized
|
|
|
|
delete style.sourceCode;
|
|
|
|
return Promise.resolve(Object.assign(usercss.buildMeta(sourceCode), style));
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildCode(style) {
|
|
|
|
return usercss.buildCode(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapReject(pending) {
|
2017-11-22 10:19:27 +00:00
|
|
|
return pending
|
2017-11-24 15:26:20 +00:00
|
|
|
.catch(err => new Error(Array.isArray(err) ? err.join('\n') : err.message || String(err)));
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the source and find the duplication
|
2017-11-01 00:17:12 +00:00
|
|
|
function build({sourceCode, checkDup = false}, noReject) {
|
|
|
|
const pending = buildMeta({sourceCode})
|
|
|
|
.then(style => Promise.all([
|
|
|
|
buildCode(style),
|
|
|
|
checkDup && findDup(style)
|
|
|
|
]))
|
2017-09-18 03:34:12 +00:00
|
|
|
.then(([style, dup]) => ({style, dup}));
|
|
|
|
|
2017-11-08 21:51:58 +00:00
|
|
|
return noReject ? wrapReject(pending) : pending;
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function save(style, noReject) {
|
|
|
|
const pending = buildMeta(style)
|
|
|
|
.then(assignVars)
|
|
|
|
.then(buildCode)
|
|
|
|
.then(saveStyle);
|
|
|
|
|
2017-11-08 21:51:58 +00:00
|
|
|
return noReject ? wrapReject(pending) : pending;
|
2017-09-18 03:34:12 +00:00
|
|
|
|
|
|
|
function assignVars(style) {
|
|
|
|
if (style.reason === 'config' && style.id) {
|
|
|
|
return style;
|
|
|
|
}
|
2017-11-08 21:52:59 +00:00
|
|
|
return findDup(style).then(dup => {
|
|
|
|
if (dup) {
|
|
|
|
style.id = dup.id;
|
|
|
|
if (style.reason !== 'config') {
|
|
|
|
// preserve style.vars during update
|
|
|
|
usercss.assignVars(style, dup);
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
2017-11-08 21:52:59 +00:00
|
|
|
}
|
|
|
|
return style;
|
|
|
|
});
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findDup(style) {
|
|
|
|
if (style.id) {
|
|
|
|
return getStyles({id: style.id}).then(s => s[0]);
|
|
|
|
}
|
|
|
|
return getStyles().then(styles =>
|
|
|
|
styles.find(target => {
|
|
|
|
if (!target.usercssData) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return target.usercssData.name === style.usercssData.name &&
|
|
|
|
target.usercssData.namespace === style.usercssData.namespace;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function openInstallPage(tab, {url = tab.url, direct} = {}) {
|
|
|
|
if (direct) {
|
|
|
|
prefetchCodeForInstallation(tab.id, url);
|
|
|
|
}
|
2017-10-01 12:34:29 +00:00
|
|
|
return wrapReject(openURL({
|
2017-11-24 16:33:50 +00:00
|
|
|
url: '/install-usercss.html' +
|
|
|
|
'?updateUrl=' + encodeURIComponent(url) +
|
|
|
|
'&tabId=' + (direct ? -tab.id : tab.id),
|
2017-10-01 12:34:29 +00:00
|
|
|
index: tab.index + 1,
|
|
|
|
openerTabId: tab.id,
|
|
|
|
}));
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function prefetchCodeForInstallation(tabId, url) {
|
|
|
|
const key = 'tempUsercssCode' + tabId;
|
|
|
|
Promise.all([
|
|
|
|
download(url),
|
|
|
|
chromeLocal.setValue(key, {loading: true}),
|
|
|
|
]).then(([code]) => {
|
|
|
|
chromeLocal.setValue(key, code);
|
|
|
|
setTimeout(() => chromeLocal.remove(key), 60e3);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-24 03:39:04 +00:00
|
|
|
return {build, save, findDup, openInstallPage};
|
2017-09-18 03:34:12 +00:00
|
|
|
})();
|