2017-03-26 02:30:59 +00:00
|
|
|
/* eslint-disable no-tabs, indent, quotes, no-var */
|
|
|
|
'use strict';
|
|
|
|
|
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?).
|
2017-03-26 02:30:59 +00:00
|
|
|
if (code === null) {
|
2014-02-06 21:05:31 +00:00
|
|
|
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;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2014-02-06 21:05:31 +00:00
|
|
|
}
|
|
|
|
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
|
2017-03-26 02:30:59 +00:00
|
|
|
if (typeof a == "undefined") {
|
2013-05-09 03:10:08 +00:00
|
|
|
return (typeof b == "undefined") || (b.length == 0);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
if (typeof b == "undefined") {
|
2013-05-09 03:10:08 +00:00
|
|
|
return (typeof a == "undefined") || (a.length == 0);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2013-05-09 03:10:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:58:16 +00:00
|
|
|
document.addEventListener("stylishUpdateChrome", stylishUpdateChrome);
|
|
|
|
function stylishInstallChrome() {
|
|
|
|
orphanCheck();
|
2012-04-16 01:56:12 +00:00
|
|
|
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";
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.runtime.sendMessage(json, function() {
|
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
|
|
|
});
|
2017-03-19 01:58:16 +00:00
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
|
2017-03-19 01:58:16 +00:00
|
|
|
document.addEventListener("stylishInstallChrome", stylishInstallChrome);
|
|
|
|
function stylishUpdateChrome() {
|
|
|
|
orphanCheck();
|
2017-03-26 02:30:59 +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
|
|
|
});
|
2017-03-19 01:58:16 +00:00
|
|
|
}
|
2013-05-09 03:10:08 +00:00
|
|
|
|
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 {
|
2017-03-26 02:30:59 +00:00
|
|
|
callback(xhr.responseText);
|
2013-05-09 03:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
};
|
2012-12-01 23:50:43 +00:00
|
|
|
if (url.length > 2000) {
|
|
|
|
var parts = url.split("?");
|
|
|
|
xhr.open("POST", parts[0], true);
|
2017-03-26 02:30:59 +00:00
|
|
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
2012-12-01 23:50:43 +00:00
|
|
|
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 */
|
2017-03-26 02:30:59 +00:00
|
|
|
(function(es) {
|
2017-02-03 12:12:23 +00:00
|
|
|
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
|
|
|
});
|
2017-02-03 13:05:03 +00:00
|
|
|
})([
|
|
|
|
...document.querySelectorAll('div[id^="stylish-installed-style-not-installed-"]'),
|
|
|
|
...document.querySelectorAll('div[id^="stylish-installed-style-needs-update-"]')
|
|
|
|
]);
|
2017-03-19 01:58:16 +00:00
|
|
|
|
|
|
|
// orphaned content script check
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) =>
|
|
|
|
msg.method == 'ping' && sendResponse(true));
|
|
|
|
|
|
|
|
function orphanCheck() {
|
|
|
|
var port = chrome.runtime.connect();
|
|
|
|
if (port) {
|
|
|
|
port.disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we're orphaned due to an extension update
|
|
|
|
// we can detach event listeners
|
|
|
|
document.removeEventListener('stylishUpdateChrome', stylishUpdateChrome);
|
|
|
|
document.removeEventListener('stylishInstallChrome', stylishInstallChrome);
|
|
|
|
// we can't detach chrome.runtime.onMessage because it's no longer connected internally
|
|
|
|
// we can destroy global functions in this context to free up memory
|
|
|
|
[
|
|
|
|
'arraysAreEqual',
|
|
|
|
'getMeta',
|
|
|
|
'getResource',
|
|
|
|
'orphanCheck',
|
|
|
|
'sectionsAreEqual',
|
|
|
|
'sendEvent',
|
|
|
|
'stylishUpdateChrome',
|
|
|
|
'stylishInstallChrome'
|
2017-03-26 02:30:59 +00:00
|
|
|
].forEach(fn => (window[fn] = null));
|
2017-03-19 01:58:16 +00:00
|
|
|
}
|