2018-01-01 17:02:49 +00:00
|
|
|
/* global API_METHODS usercss saveStyle getStyles chromeLocal cachedStyles */
|
2017-09-18 03:34:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
(() => {
|
|
|
|
|
|
|
|
API_METHODS.saveUsercss = save;
|
|
|
|
API_METHODS.buildUsercss = build;
|
|
|
|
API_METHODS.installUsercss = install;
|
2017-12-10 07:04:13 +00:00
|
|
|
|
|
|
|
const TEMP_CODE_PREFIX = 'tempUsercssCode';
|
|
|
|
const TEMP_CODE_CLEANUP_DELAY = 60e3;
|
|
|
|
let tempCodeLastWriteDate = 0;
|
|
|
|
if (FIREFOX) {
|
|
|
|
// the temp code is created on direct installation of usercss URLs in FF
|
|
|
|
// and can be left behind in case the install page didn't open in time before
|
|
|
|
// the extension was updated/reloaded/disabled or the browser was closed
|
|
|
|
setTimeout(function poll() {
|
|
|
|
if (Date.now() - tempCodeLastWriteDate < TEMP_CODE_CLEANUP_DELAY) {
|
|
|
|
setTimeout(poll, TEMP_CODE_CLEANUP_DELAY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.storage.local.get(null, storage => {
|
|
|
|
const leftovers = [];
|
|
|
|
for (const key in storage) {
|
|
|
|
if (key.startsWith(TEMP_CODE_PREFIX)) {
|
|
|
|
leftovers.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (leftovers.length) {
|
|
|
|
chrome.storage.local.remove(leftovers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, TEMP_CODE_CLEANUP_DELAY);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the source and find the duplication
|
2018-01-01 17:02:49 +00:00
|
|
|
function build({sourceCode, checkDup = false}) {
|
|
|
|
return buildMeta({sourceCode})
|
2017-11-01 00:17:12 +00:00
|
|
|
.then(style => Promise.all([
|
|
|
|
buildCode(style),
|
|
|
|
checkDup && findDup(style)
|
|
|
|
]))
|
2017-09-18 03:34:12 +00:00
|
|
|
.then(([style, dup]) => ({style, dup}));
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function save(style) {
|
|
|
|
if (!style.sourceCode) {
|
|
|
|
style.sourceCode = cachedStyles.byId.get(style.id).sourceCode;
|
|
|
|
}
|
|
|
|
return buildMeta(style)
|
2017-09-18 03:34:12 +00:00
|
|
|
.then(assignVars)
|
|
|
|
.then(buildCode)
|
|
|
|
.then(saveStyle);
|
|
|
|
|
|
|
|
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;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:26:31 +00:00
|
|
|
function install({url, direct, downloaded, tab}, sender) {
|
|
|
|
tab = tab !== undefined ? tab : sender.tab;
|
2018-01-01 17:02:49 +00:00
|
|
|
url = url || tab.url;
|
2017-12-06 19:35:19 +00:00
|
|
|
if (direct && !downloaded) {
|
2017-11-24 16:33:50 +00:00
|
|
|
prefetchCodeForInstallation(tab.id, url);
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
return openURL({
|
2017-11-24 16:33:50 +00:00
|
|
|
url: '/install-usercss.html' +
|
|
|
|
'?updateUrl=' + encodeURIComponent(url) +
|
2017-11-25 17:24:15 +00:00
|
|
|
'&tabId=' + tab.id +
|
|
|
|
(direct ? '&direct=yes' : ''),
|
2017-10-01 12:34:29 +00:00
|
|
|
index: tab.index + 1,
|
|
|
|
openerTabId: tab.id,
|
2017-12-31 14:58:35 +00:00
|
|
|
currentWindow: null,
|
2018-01-01 17:02:49 +00:00
|
|
|
});
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function prefetchCodeForInstallation(tabId, url) {
|
2017-12-10 07:04:13 +00:00
|
|
|
const key = TEMP_CODE_PREFIX + tabId;
|
|
|
|
tempCodeLastWriteDate = Date.now();
|
2017-11-24 16:33:50 +00:00
|
|
|
Promise.all([
|
|
|
|
download(url),
|
|
|
|
chromeLocal.setValue(key, {loading: true}),
|
|
|
|
]).then(([code]) => {
|
|
|
|
chromeLocal.setValue(key, code);
|
2017-12-10 07:04:13 +00:00
|
|
|
setTimeout(() => chromeLocal.remove(key), TEMP_CODE_CLEANUP_DELAY);
|
2017-11-24 16:33:50 +00:00
|
|
|
});
|
|
|
|
}
|
2017-09-18 03:34:12 +00:00
|
|
|
})();
|