2015-01-30 16:36:46 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: location.href, enabled: true, asHash: true}, applyStyles);
|
2015-01-30 03:32:14 +00:00
|
|
|
|
|
|
|
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
|
|
|
|
switch(request.name) {
|
|
|
|
case "styleDeleted":
|
|
|
|
removeStyle(request.id);
|
|
|
|
break;
|
|
|
|
case "styleUpdated":
|
|
|
|
removeStyle(request.style.id);
|
|
|
|
//fallthrough
|
|
|
|
case "styleAdded":
|
|
|
|
if (request.style.enabled == "true") {
|
2015-01-30 16:36:46 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: location.href, enabled: true, id: request.style.id, asHash: true}, applyStyles);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "styleApply":
|
|
|
|
for (var styleId in request.styles) {
|
|
|
|
applySections(styleId, request.styles[styleId]);
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function removeStyle(id) {
|
|
|
|
var e = document.getElementById("stylish-" + id);
|
|
|
|
if (e) {
|
|
|
|
e.parentNode.removeChild(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 16:36:46 +00:00
|
|
|
function applyStyles(styleHash) {
|
|
|
|
for (var styleId in styleHash) {
|
|
|
|
applySections(styleId, styleHash[styleId]);
|
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 16:36:46 +00:00
|
|
|
function applySections(styleId, sections) {
|
|
|
|
var styleElement = document.getElementById("stylish-" + styleId);
|
|
|
|
// Already there.
|
|
|
|
if (styleElement) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
if (document.documentElement instanceof SVGSVGElement) {
|
|
|
|
// SVG document, make an SVG style element.
|
|
|
|
styleElement = document.createElementNS("http://www.w3.org/2000/svg", "style");
|
|
|
|
} else {
|
|
|
|
// This will make an HTML style element. If there's SVG embedded in an HTML document, this works on the SVG too.
|
|
|
|
styleElement = document.createElement("style");
|
|
|
|
}
|
2015-01-30 16:36:46 +00:00
|
|
|
styleElement.setAttribute("id", "stylish-" + styleId);
|
2015-01-30 03:32:14 +00:00
|
|
|
styleElement.setAttribute("class", "stylish");
|
|
|
|
styleElement.setAttribute("type", "text/css");
|
|
|
|
styleElement.appendChild(document.createTextNode(sections.map(function(section) {
|
|
|
|
return section.code;
|
|
|
|
}).join("\n")));
|
2015-02-01 00:07:23 +00:00
|
|
|
document.documentElement.appendChild(styleElement);
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|