stylus/update.js

118 lines
3.7 KiB
JavaScript
Raw Normal View History

/* globals getStyles */
2017-02-08 15:15:35 +00:00
'use strict';
// TODO: refactor to make usable in manage::Updater
2017-02-08 15:15:35 +00:00
var update = {
fetch: (resource, callback) => {
let req = new XMLHttpRequest();
let [url, data] = resource.split('?');
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.onload = () => callback(req.responseText);
req.onerror = req.ontimeout = () => callback();
req.send(data);
},
2017-02-14 15:35:53 +00:00
md5Check: (style, callback, skipped) => {
2017-02-08 15:15:35 +00:00
let req = new XMLHttpRequest();
req.open('GET', style.md5Url, true);
req.onload = () => {
let md5 = req.responseText;
if (md5 && md5 !== style.originalMd5) {
callback(style);
}
else {
2017-02-14 15:35:53 +00:00
skipped(`"${style.name}" style is up-to-date`);
2017-02-08 15:15:35 +00:00
}
};
2017-02-14 15:35:53 +00:00
req.onerror = req.ontimeout = () => skipped('Error validating MD5 checksum');
2017-02-08 15:15:35 +00:00
req.send();
},
list: (callback) => {
getStyles({}, (styles) => callback(styles.filter(style => style.updateUrl)));
},
2017-02-14 15:35:53 +00:00
perform: (observe = function () {}) => {
// TODO: use sectionsAreEqual
2017-02-08 15:15:35 +00:00
// from install.js
function arraysAreEqual (a, b) {
// treat empty array and undefined as equivalent
if (typeof a === 'undefined') {
return (typeof b === 'undefined') || (b.length === 0);
}
if (typeof b === 'undefined') {
return (typeof a === 'undefined') || (a.length === 0);
}
if (a.length !== b.length) {
return false;
}
return a.every(function (entry) {
return b.indexOf(entry) !== -1;
});
}
// from install.js
function sectionsAreEqual(a, b) {
if (a.code !== b.code) {
return false;
}
return ['urls', 'urlPrefixes', 'domains', 'regexps'].every(function (attribute) {
return arraysAreEqual(a[attribute], b[attribute]);
});
}
update.list(styles => {
2017-02-14 15:35:53 +00:00
observe('count', styles.length);
2017-02-08 15:15:35 +00:00
styles.forEach(style => update.md5Check(style, style => update.fetch(style.updateUrl, response => {
if (response) {
let json = JSON.parse(response);
if (json.sections.length === style.sections.length) {
if (json.sections.every((section) => {
return style.sections.some(installedSection => sectionsAreEqual(section, installedSection));
})) {
2017-02-14 15:35:53 +00:00
return observe('single-skipped', '2'); // everything is the same
2017-02-08 15:15:35 +00:00
}
json.method = 'saveStyle';
json.id = style.id;
saveStyle(json).then(style => {
2017-02-14 15:35:53 +00:00
observe('single-updated', style.name);
2017-02-08 15:15:35 +00:00
});
}
else {
2017-02-14 15:35:53 +00:00
return observe('single-skipped', '3'); // style sections mismatch
2017-02-08 15:15:35 +00:00
}
}
2017-02-14 15:35:53 +00:00
}), () => observe('single-skipped', '1')));
2017-02-08 15:15:35 +00:00
});
}
};
2017-02-14 15:35:53 +00:00
// automatically update all user-styles if "updateInterval" pref is set
window.setTimeout(function () {
let id;
function run () {
update.perform(/*(cmd, value) => console.log(cmd, value)*/);
reset();
}
function reset () {
window.clearTimeout(id);
let interval = prefs.get('updateInterval');
// if interval === 0 => automatic update is disabled
if (interval) {
/* console.log('next update', interval); */
id = window.setTimeout(run, interval * 60 * 60 * 1000);
}
}
if (prefs.get('updateInterval')) {
run();
}
chrome.runtime.onMessage.addListener(request => {
// when user has changed the predefined time interval in the settings page
if (request.method === 'prefChanged' && 'updateInterval' in request.prefs) {
2017-02-14 15:35:53 +00:00
reset();
}
// when user just manually checked for updates
if (request.method === 'resetInterval') {
reset();
}
});
}, 10000);