2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: location.href, enabled: true, updateBadge: window == window.top}, function(response) {
|
2012-04-16 01:56:12 +00:00
|
|
|
response.forEach(applyStyle);
|
|
|
|
});
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
|
2012-04-16 01:56:12 +00:00
|
|
|
switch(request.name) {
|
|
|
|
case "styleDeleted":
|
|
|
|
removeStyle(request.id);
|
|
|
|
break;
|
|
|
|
case "styleUpdated":
|
|
|
|
removeStyle(request.style.id);
|
|
|
|
//fallthrough
|
|
|
|
case "styleAdded":
|
|
|
|
if (request.style.enabled == "true") {
|
2012-08-20 01:14:33 +00:00
|
|
|
applyStyle(request.style);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function removeStyle(id) {
|
|
|
|
var e = document.getElementById("stylish-" + id);
|
|
|
|
if (e) {
|
|
|
|
e.parentNode.removeChild(e);
|
|
|
|
}
|
|
|
|
}
|
2012-06-02 04:11:32 +00:00
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function applyStyle(s) {
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyleApplies", style: s, url: location.href}, function(response) {
|
|
|
|
if (response && response.length > 0) {
|
|
|
|
applySections(s, response);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2012-06-02 04:11:32 +00:00
|
|
|
});
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function applySections(style, sections) {
|
2013-11-19 19:37:09 +00:00
|
|
|
var styleElement;
|
|
|
|
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");
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
styleElement.setAttribute("id", "stylish-" + style.id);
|
|
|
|
styleElement.setAttribute("class", "stylish");
|
|
|
|
styleElement.setAttribute("type", "text/css");
|
|
|
|
styleElement.appendChild(document.createTextNode(sections.map(function(section) {
|
|
|
|
return section.code;
|
|
|
|
}).join("\n")));
|
|
|
|
if (document.head) {
|
|
|
|
document.head.appendChild(styleElement);
|
|
|
|
} else {
|
|
|
|
document.documentElement.appendChild(styleElement);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
}
|