2018-11-07 06:09:29 +00:00
|
|
|
/* global styleSectionsEqual prefs download tryJSONparse ignoreChromeError
|
|
|
|
calcStyleDigest getStyleWithNoCode debounce chromeLocal
|
|
|
|
usercss semverCompare
|
|
|
|
API_METHODS styleManager */
|
2017-02-08 15:15:35 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-16 15:05:25 +00:00
|
|
|
(() => {
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
const STATES = {
|
|
|
|
UPDATED: 'updated',
|
|
|
|
SKIPPED: 'skipped',
|
|
|
|
|
|
|
|
// details for SKIPPED status
|
|
|
|
EDITED: 'locally edited',
|
|
|
|
MAYBE_EDITED: 'may be locally edited',
|
|
|
|
SAME_MD5: 'up-to-date: MD5 is unchanged',
|
|
|
|
SAME_CODE: 'up-to-date: code sections are unchanged',
|
|
|
|
SAME_VERSION: 'up-to-date: version is unchanged',
|
|
|
|
ERROR_MD5: 'error: MD5 is invalid',
|
|
|
|
ERROR_JSON: 'error: JSON is invalid',
|
|
|
|
ERROR_VERSION: 'error: version is older than installed style',
|
|
|
|
};
|
|
|
|
|
2018-05-14 05:46:22 +00:00
|
|
|
const ALARM_NAME = 'scheduledUpdate';
|
|
|
|
const MIN_INTERVAL_MS = 60e3;
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
let lastUpdateTime = parseInt(localStorage.lastUpdateTime) || Date.now();
|
|
|
|
let checkingAll = false;
|
|
|
|
let logQueue = [];
|
|
|
|
let logLastWriteTime = 0;
|
|
|
|
|
2018-04-30 17:05:52 +00:00
|
|
|
const retrying = new Set();
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
API_METHODS.updateCheckAll = checkAllStyles;
|
|
|
|
API_METHODS.updateCheck = checkStyle;
|
2018-02-16 15:05:25 +00:00
|
|
|
API_METHODS.getUpdaterStates = () => STATES;
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
prefs.subscribe(['updateInterval'], schedule);
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
return {checkAllStyles, checkStyle, STATES};
|
|
|
|
|
|
|
|
function checkAllStyles({
|
|
|
|
save = true,
|
|
|
|
ignoreDigest,
|
|
|
|
observe,
|
|
|
|
} = {}) {
|
|
|
|
resetInterval();
|
|
|
|
checkingAll = true;
|
2018-04-30 17:05:52 +00:00
|
|
|
retrying.clear();
|
2018-01-01 17:02:49 +00:00
|
|
|
const port = observe && chrome.runtime.connect({name: 'updater'});
|
2018-11-07 06:09:29 +00:00
|
|
|
return styleManager.getAllStyles().then(styles => {
|
2017-04-25 21:48:27 +00:00
|
|
|
styles = styles.filter(style => style.updateUrl);
|
2018-01-01 17:02:49 +00:00
|
|
|
if (port) port.postMessage({count: styles.length});
|
|
|
|
log('');
|
|
|
|
log(`${save ? 'Scheduled' : 'Manual'} update check for ${styles.length} styles`);
|
2017-04-25 21:48:27 +00:00
|
|
|
return Promise.all(
|
|
|
|
styles.map(style =>
|
2018-01-01 17:02:49 +00:00
|
|
|
checkStyle({style, port, save, ignoreDigest})));
|
2017-04-25 21:48:27 +00:00
|
|
|
}).then(() => {
|
2018-01-01 17:02:49 +00:00
|
|
|
if (port) port.postMessage({done: true});
|
|
|
|
if (port) port.disconnect();
|
|
|
|
log('');
|
|
|
|
checkingAll = false;
|
2018-04-30 17:05:52 +00:00
|
|
|
retrying.clear();
|
2017-02-08 15:15:35 +00:00
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkStyle({
|
|
|
|
id,
|
2018-11-07 06:09:29 +00:00
|
|
|
style,
|
2018-01-01 17:02:49 +00:00
|
|
|
port,
|
|
|
|
save = true,
|
|
|
|
ignoreDigest,
|
|
|
|
}) {
|
2017-04-24 13:29:48 +00:00
|
|
|
/*
|
|
|
|
Original style digests are calculated in these cases:
|
|
|
|
* style is installed or updated from server
|
|
|
|
* style is checked for an update and its code is equal to the server code
|
|
|
|
|
|
|
|
Update check proceeds in these cases:
|
|
|
|
* style has the original digest and it's equal to the current digest
|
|
|
|
* [ignoreDigest: true] style doesn't yet have the original digest but we ignore it
|
|
|
|
* [ignoreDigest: none/false] style doesn't yet have the original digest
|
|
|
|
so we compare the code to the server code and if it's the same we save the digest,
|
|
|
|
otherwise we skip the style and report MAYBE_EDITED status
|
|
|
|
|
|
|
|
'ignoreDigest' option is set on the second manual individual update check on the manage page.
|
|
|
|
*/
|
2018-11-07 06:09:29 +00:00
|
|
|
return fetchStyle()
|
|
|
|
.then(() => {
|
|
|
|
if (!ignoreDigest) {
|
|
|
|
return calcStyleDigest(style)
|
|
|
|
.then(checkIfEdited);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (style.usercssData) {
|
|
|
|
return maybeUpdateUsercss();
|
|
|
|
}
|
|
|
|
return maybeUpdateUSO();
|
|
|
|
})
|
2017-04-28 21:04:01 +00:00
|
|
|
.then(maybeSave)
|
2017-12-04 06:55:11 +00:00
|
|
|
.then(reportSuccess)
|
|
|
|
.catch(reportFailure);
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function fetchStyle() {
|
|
|
|
if (style) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return styleManager.get(id)
|
|
|
|
.then(style_ => {
|
|
|
|
style = style_;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:55:11 +00:00
|
|
|
function reportSuccess(saved) {
|
2018-01-01 17:02:49 +00:00
|
|
|
log(STATES.UPDATED + ` #${style.id} ${style.name}`);
|
|
|
|
const info = {updated: true, style: saved};
|
|
|
|
if (port) port.postMessage(info);
|
|
|
|
return info;
|
2017-12-04 06:55:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function reportFailure(error) {
|
2018-07-02 14:20:22 +00:00
|
|
|
if ((
|
|
|
|
error === 503 || // Service Unavailable
|
|
|
|
error === 429 // Too Many Requests
|
|
|
|
) && !retrying.has(id)) {
|
2018-04-30 17:05:52 +00:00
|
|
|
retrying.add(id);
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
2018-05-09 15:11:44 +00:00
|
|
|
resolve(checkStyle({id, style, port, save, ignoreDigest}));
|
2018-04-30 17:05:52 +00:00
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
error = error === 0 ? 'server unreachable' : error;
|
2018-11-29 00:07:05 +00:00
|
|
|
// UserCSS metadata error returns an object; e.g. "Invalid @var color..."
|
|
|
|
if (typeof error === 'object' && error.message) {
|
|
|
|
error = error.message;
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
log(STATES.SKIPPED + ` (${error}) #${style.id} ${style.name}`);
|
|
|
|
const info = {error, STATES, style: getStyleWithNoCode(style)};
|
|
|
|
if (port) port.postMessage(info);
|
|
|
|
return info;
|
2017-12-04 06:55:11 +00:00
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
function checkIfEdited(digest) {
|
2017-09-16 01:24:50 +00:00
|
|
|
if (style.originalDigest && style.originalDigest !== digest) {
|
2018-01-01 17:02:49 +00:00
|
|
|
return Promise.reject(STATES.EDITED);
|
2017-04-23 12:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
function maybeUpdateUSO() {
|
|
|
|
return download(style.md5Url).then(md5 => {
|
|
|
|
if (!md5 || md5.length !== 32) {
|
2018-01-01 17:02:49 +00:00
|
|
|
return Promise.reject(STATES.ERROR_MD5);
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
|
|
|
if (md5 === style.originalMd5 && style.originalDigest && !ignoreDigest) {
|
2018-01-01 17:02:49 +00:00
|
|
|
return Promise.reject(STATES.SAME_MD5);
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2018-03-13 14:24:10 +00:00
|
|
|
// USO can't handle POST requests for style json
|
|
|
|
return download(style.updateUrl, {body: null})
|
2018-11-18 13:30:47 +00:00
|
|
|
.then(text => {
|
|
|
|
const style = tryJSONparse(text);
|
|
|
|
if (style) {
|
|
|
|
// USO may not provide a correctly updated originalMd5 (#555)
|
|
|
|
style.originalMd5 = md5;
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeUpdateUsercss() {
|
2017-12-04 06:55:11 +00:00
|
|
|
// TODO: when sourceCode is > 100kB use http range request(s) for version check
|
2018-11-07 06:09:29 +00:00
|
|
|
return download(style.updateUrl).then(text =>
|
|
|
|
usercss.buildMeta(text).then(json => {
|
|
|
|
const {usercssData: {version}} = style;
|
|
|
|
const {usercssData: {version: newVersion}} = json;
|
|
|
|
switch (Math.sign(semverCompare(version, newVersion))) {
|
|
|
|
case 0:
|
|
|
|
// re-install is invalid in a soft upgrade
|
|
|
|
if (!ignoreDigest) {
|
|
|
|
const sameCode = text === style.sourceCode;
|
|
|
|
return Promise.reject(sameCode ? STATES.SAME_CODE : STATES.SAME_VERSION);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// downgrade is always invalid
|
|
|
|
return Promise.reject(STATES.ERROR_VERSION);
|
|
|
|
}
|
|
|
|
return usercss.buildCode(json);
|
|
|
|
})
|
|
|
|
);
|
2017-04-23 12:19:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 06:55:11 +00:00
|
|
|
function maybeSave(json = {}) {
|
|
|
|
// usercss is already validated while building
|
|
|
|
if (!json.usercssData && !styleJSONseemsValid(json)) {
|
2018-01-01 17:02:49 +00:00
|
|
|
return Promise.reject(STATES.ERROR_JSON);
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2017-10-04 08:19:20 +00:00
|
|
|
json.id = style.id;
|
2017-11-26 17:47:23 +00:00
|
|
|
json.updateDate = Date.now();
|
2017-12-13 06:12:54 +00:00
|
|
|
|
2017-12-04 06:55:11 +00:00
|
|
|
// keep current state
|
|
|
|
delete json.enabled;
|
2017-12-13 06:12:54 +00:00
|
|
|
|
2017-12-04 06:55:11 +00:00
|
|
|
// keep local name customizations
|
2017-12-13 06:12:54 +00:00
|
|
|
if (style.originalName !== style.name && style.name !== json.name) {
|
|
|
|
delete json.name;
|
|
|
|
} else {
|
|
|
|
json.originalName = json.name;
|
|
|
|
}
|
2017-12-04 06:55:11 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
const newStyle = Object.assign({}, style, json);
|
2018-08-27 17:42:17 +00:00
|
|
|
if (styleSectionsEqual(json, style, {checkSource: true})) {
|
2017-12-04 06:55:11 +00:00
|
|
|
// update digest even if save === false as there might be just a space added etc.
|
2018-11-07 06:09:29 +00:00
|
|
|
return styleManager.installStyle(newStyle)
|
2018-08-27 17:17:36 +00:00
|
|
|
.then(saved => {
|
|
|
|
style.originalDigest = saved.originalDigest;
|
|
|
|
return Promise.reject(STATES.SAME_CODE);
|
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!style.originalDigest && !ignoreDigest) {
|
|
|
|
return Promise.reject(STATES.MAYBE_EDITED);
|
2017-09-18 03:34:12 +00:00
|
|
|
}
|
2017-12-04 06:55:11 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
return save ?
|
2018-11-07 06:09:29 +00:00
|
|
|
API_METHODS[json.usercssData ? 'installUsercss' : 'installStyle'](newStyle) :
|
|
|
|
newStyle;
|
2017-04-23 12:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function styleJSONseemsValid(json) {
|
|
|
|
return json
|
|
|
|
&& json.sections
|
|
|
|
&& json.sections.length
|
2017-07-16 18:02:00 +00:00
|
|
|
&& typeof json.sections.every === 'function'
|
|
|
|
&& typeof json.sections[0].code === 'string';
|
2017-04-23 12:19:18 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function schedule() {
|
2017-04-20 18:27:10 +00:00
|
|
|
const interval = prefs.get('updateInterval') * 60 * 60 * 1000;
|
2018-05-14 05:46:22 +00:00
|
|
|
if (interval > 0) {
|
2018-01-01 17:02:49 +00:00
|
|
|
const elapsed = Math.max(0, Date.now() - lastUpdateTime);
|
2018-05-14 05:46:22 +00:00
|
|
|
chrome.alarms.create(ALARM_NAME, {
|
|
|
|
when: Date.now() + Math.max(MIN_INTERVAL_MS, interval - elapsed),
|
|
|
|
});
|
|
|
|
chrome.alarms.onAlarm.addListener(onAlarm);
|
2017-04-22 18:02:49 +00:00
|
|
|
} else {
|
2018-05-14 05:46:22 +00:00
|
|
|
chrome.alarms.clear(ALARM_NAME, ignoreChromeError);
|
|
|
|
chrome.alarms.onAlarm.removeListener(onAlarm);
|
2017-02-14 15:35:53 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 05:46:22 +00:00
|
|
|
function onAlarm({name}) {
|
|
|
|
if (name === ALARM_NAME) checkAllStyles();
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function resetInterval() {
|
|
|
|
localStorage.lastUpdateTime = lastUpdateTime = Date.now();
|
|
|
|
schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(text) {
|
|
|
|
logQueue.push({text, time: new Date().toLocaleString()});
|
|
|
|
debounce(flushQueue, text && checkingAll ? 1000 : 0);
|
|
|
|
}
|
|
|
|
|
2018-05-09 15:33:13 +00:00
|
|
|
function flushQueue(lines) {
|
|
|
|
if (!lines) {
|
|
|
|
chromeLocal.getValue('updateLog', []).then(flushQueue);
|
2018-01-01 17:02:49 +00:00
|
|
|
return;
|
2017-04-26 23:06:16 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
const time = Date.now() - logLastWriteTime > 11e3 ?
|
|
|
|
logQueue[0].time + ' ' :
|
|
|
|
'';
|
2018-05-24 18:09:00 +00:00
|
|
|
if (logQueue[0] && !logQueue[0].text) {
|
2018-01-01 17:02:49 +00:00
|
|
|
logQueue.shift();
|
|
|
|
if (lines[lines.length - 1]) lines.push('');
|
|
|
|
}
|
|
|
|
lines.splice(0, lines.length - 1000);
|
|
|
|
lines.push(time + (logQueue[0] && logQueue[0].text || ''));
|
|
|
|
lines.push(...logQueue.slice(1).map(item => item.text));
|
|
|
|
|
2018-05-09 15:33:13 +00:00
|
|
|
chromeLocal.setValue('updateLog', lines);
|
2018-01-01 17:02:49 +00:00
|
|
|
logLastWriteTime = Date.now();
|
|
|
|
logQueue = [];
|
|
|
|
}
|
|
|
|
})();
|