stylus/install.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

chrome.extension.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 {
sendEvent("styleAlreadyInstalledChrome");
}
});
function sendEvent(type) {
var stylishEvent = document.createEvent("Events");
stylishEvent.initEvent(type, false, false, document.defaultView, null);
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);
json.method = "saveStyle";
chrome.extension.sendMessage(json, function(response) {
2012-04-16 01:56:12 +00:00
sendEvent("styleInstalledChrome");
});
});
getResource(getMeta("stylish-install-ping-url-chrome"));
}
});
}, false);
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.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && callback) {
callback(xhr.responseText);
}
}
xhr.send();
}