2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage({method: "getStyles", url: getMeta("stylish-id-url") || location.href}, function(response) {
|
2012-04-16 01:56:12 +00:00
|
|
|
if (response.length == 0) {
|
|
|
|
sendEvent("styleCanBeInstalledChrome");
|
|
|
|
} else {
|
2013-05-09 03:10:08 +00:00
|
|
|
var installedStyle = response[0];
|
|
|
|
// maybe an update is needed
|
2014-02-06 21:05:31 +00:00
|
|
|
// use the md5 if available
|
|
|
|
var md5Url = getMeta("stylish-md5-url");
|
|
|
|
if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) {
|
|
|
|
getResource(md5Url, function(md5) {
|
|
|
|
if (md5 == installedStyle.originalMd5) {
|
|
|
|
sendEvent("styleAlreadyInstalledChrome", {updateUrl: installedStyle.updateUrl});
|
|
|
|
} else {
|
|
|
|
sendEvent("styleCanBeUpdatedChrome", {updateUrl: installedStyle.updateUrl});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
getResource(getMeta("stylish-code-chrome"), function(code) {
|
|
|
|
// this would indicate a failure (a style with settings?).
|
|
|
|
if (code == null) {
|
|
|
|
sendEvent("styleCanBeUpdatedChrome", {updateUrl: installedStyle.updateUrl});
|
|
|
|
}
|
|
|
|
var json = JSON.parse(code);
|
|
|
|
if (json.sections.length == installedStyle.sections.length) {
|
|
|
|
if (json.sections.every(function(section) {
|
|
|
|
return installedStyle.sections.some(function(installedSection) {
|
|
|
|
return sectionsAreEqual(section, installedSection);
|
|
|
|
});
|
|
|
|
})) {
|
|
|
|
// everything's the same
|
|
|
|
sendEvent("styleAlreadyInstalledChrome", {updateUrl: installedStyle.updateUrl});
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
sendEvent("styleCanBeUpdatedChrome", {updateUrl: installedStyle.updateUrl});
|
|
|
|
});
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-09 03:10:08 +00:00
|
|
|
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]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-06 21:05:31 +00:00
|
|
|
function sendEvent(type, data) {
|
|
|
|
if (typeof data == "undefined") {
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
var stylishEvent = new CustomEvent(type, {detail: data});
|
2012-04-16 01:56:12 +00:00
|
|
|
document.dispatchEvent(stylishEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("stylishInstallChrome", function() {
|
|
|
|
getResource(getMeta("stylish-description"), function(name) {
|
|
|
|
if (confirm(chrome.i18n.getMessage('styleInstall', [name]))) {
|
|
|
|
getResource(getMeta("stylish-code-chrome"), function(code) {
|
|
|
|
// check for old style json
|
|
|
|
var json = JSON.parse(code);
|
2012-08-20 01:14:33 +00:00
|
|
|
json.method = "saveStyle";
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage(json, function(response) {
|
2012-04-16 01:56:12 +00:00
|
|
|
sendEvent("styleInstalledChrome");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
getResource(getMeta("stylish-install-ping-url-chrome"));
|
2017-02-03 12:12:23 +00:00
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
|
2013-05-09 03:10:08 +00:00
|
|
|
document.addEventListener("stylishUpdateChrome", function() {
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage({method: "getStyles", url: getMeta("stylish-id-url") || location.href}, function(response) {
|
2013-05-09 03:10:08 +00:00
|
|
|
var style = response[0];
|
|
|
|
if (confirm(chrome.i18n.getMessage('styleUpdate', [style.name]))) {
|
|
|
|
getResource(getMeta("stylish-code-chrome"), function(code) {
|
|
|
|
var json = JSON.parse(code);
|
2014-02-06 21:05:31 +00:00
|
|
|
json.method = "saveStyle";
|
|
|
|
json.id = style.id;
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage(json, function() {
|
2013-05-09 03:10:08 +00:00
|
|
|
sendEvent("styleInstalledChrome");
|
|
|
|
});
|
|
|
|
});
|
2017-02-03 12:12:23 +00:00
|
|
|
}
|
2013-05-09 03:10:08 +00:00
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function getMeta(name) {
|
|
|
|
var e = document.querySelector("link[rel='" + name + "']");
|
|
|
|
return e ? e.getAttribute("href") : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResource(url, callback) {
|
|
|
|
if (url.indexOf("#") == 0) {
|
|
|
|
if (callback) {
|
|
|
|
callback(document.getElementById(url.substring(1)).innerText);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function() {
|
2013-05-09 03:10:08 +00:00
|
|
|
if (xhr.readyState == 4 && callback) {
|
|
|
|
if (xhr.status >= 400) {
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(xhr.responseText);
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2012-12-01 23:50:43 +00:00
|
|
|
if (url.length > 2000) {
|
|
|
|
var parts = url.split("?");
|
|
|
|
xhr.open("POST", parts[0], true);
|
|
|
|
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
|
|
|
|
xhr.send(parts[1]);
|
|
|
|
} else {
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.send();
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2017-02-03 12:12:23 +00:00
|
|
|
|
|
|
|
/* stylish to stylus; https://github.com/schomery/stylish-chrome/issues/12 */
|
|
|
|
(function (es) {
|
|
|
|
es.forEach(e => {
|
2017-02-03 12:56:11 +00:00
|
|
|
[...e.childNodes].filter(n => n.nodeType == 3).forEach(n => {
|
|
|
|
n.nodeValue = n.nodeValue.replace('Stylish', 'Stylus');
|
|
|
|
});
|
2017-02-03 12:12:23 +00:00
|
|
|
});
|
|
|
|
})([...document.querySelectorAll('div[id^="stylish-installed-style-not-installed-"]')]);
|
|
|
|
(function (es) {
|
|
|
|
es.forEach(e => {
|
2017-02-03 12:56:11 +00:00
|
|
|
[...e.childNodes].filter(n => n.nodeType == 3).forEach(n => {
|
|
|
|
n.nodeValue = n.nodeValue.replace('Stylish', 'Stylus');
|
|
|
|
});
|
2017-02-03 12:12:23 +00:00
|
|
|
});
|
|
|
|
})([...document.querySelectorAll('div[id^="stylish-installed-style-needs-update-"]')]);
|