2012-08-20 01:14:33 +00:00
|
|
|
var styleTemplate = document.createElement("div");
|
2014-10-01 19:51:25 +00:00
|
|
|
styleTemplate.innerHTML = "<input class='checker' type='checkbox'></input><div class='style-name'></div><div class='actions'><a class='style-edit-link' href='edit.html?id='>" + t('editStyleLabel') + "</a> <a href='#' class='delete'>" + t('deleteStyleLabel') + "</a></div>";
|
2012-08-20 01:14:33 +00:00
|
|
|
|
|
|
|
chrome.tabs.getSelected(null, function(tab) {
|
|
|
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url}, showStyles);
|
|
|
|
document.querySelector("#find-styles a").href = "http://userstyles.org/styles/browse/all/" + encodeURIComponent(tab.url);
|
|
|
|
});
|
|
|
|
|
|
|
|
function showStyles(styles) {
|
|
|
|
var installed = document.getElementById("installed");
|
|
|
|
if (styles.length == 0) {
|
|
|
|
installed.innerHTML = "<div class='entry' id='no-styles'>" + t('noStylesForSite') + "</div>";
|
|
|
|
}
|
|
|
|
styles.map(createStyleElement).forEach(function(e) {
|
|
|
|
installed.appendChild(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createStyleElement(style) {
|
|
|
|
var e = styleTemplate.cloneNode(true);
|
2014-10-01 19:51:25 +00:00
|
|
|
var checkbox = e.querySelector(".checker");
|
|
|
|
checkbox.setAttribute("type","checkbox");
|
|
|
|
checkbox.setAttribute("class","checker")
|
|
|
|
if (style.enabled == "true") {
|
|
|
|
checkbox.checked = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkbox.checked = false;
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
e.setAttribute("class", "entry " + (style.enabled == "true" ? "enabled" : "disabled"));
|
|
|
|
e.setAttribute("style-id", style.id);
|
|
|
|
var styleName = e.querySelector(".style-name");
|
|
|
|
styleName.appendChild(document.createTextNode(style.name));
|
|
|
|
var editLink = e.querySelector(".style-edit-link");
|
|
|
|
editLink.setAttribute("href", editLink.getAttribute("href") + style.id);
|
|
|
|
editLink.addEventListener("click", openLink, false);
|
2014-10-01 19:51:25 +00:00
|
|
|
|
|
|
|
if (checkbox.checked == true) {
|
|
|
|
(checkbox).addEventListener("click", function() { enable(event, false); }, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(checkbox).addEventListener("click", function() { enable(event, true); }, false);
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
e.querySelector(".delete").addEventListener("click", function() { doDelete(event, false); }, false);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
function enable(event, enabled) {
|
|
|
|
var id = getId(event);
|
|
|
|
enableStyle(id, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doDelete() {
|
2013-07-03 16:43:42 +00:00
|
|
|
// Opera can't do confirms in popups
|
|
|
|
if (getBrowser() != "Opera") {
|
|
|
|
if (!confirm(t('deleteStyleConfirm'))) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
var id = getId(event);
|
|
|
|
deleteStyle(id);
|
|
|
|
}
|
|
|
|
|
2013-07-03 16:43:42 +00:00
|
|
|
function getBrowser() {
|
2013-07-03 17:33:31 +00:00
|
|
|
if (navigator.userAgent.indexOf("OPR") > -1) {
|
2013-07-03 16:43:42 +00:00
|
|
|
return "Opera";
|
|
|
|
}
|
|
|
|
return "Chrome";
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function getId(event) {
|
|
|
|
var e = event.target;
|
|
|
|
while (e) {
|
|
|
|
if (e.hasAttribute("style-id")) {
|
|
|
|
return e.getAttribute("style-id");
|
|
|
|
}
|
|
|
|
e = e.parentNode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function openLink(event) {
|
2013-07-04 22:12:16 +00:00
|
|
|
event.preventDefault();
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.tabs.create({url: event.target.href});
|
2013-07-04 22:12:16 +00:00
|
|
|
//return false;
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleUpdate(style) {
|
|
|
|
var installed = document.getElementById("installed");
|
|
|
|
installed.replaceChild(createStyleElement(style), installed.querySelector("[style-id='" + style.id + "']"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDelete(id) {
|
|
|
|
var installed = document.getElementById("installed");
|
|
|
|
installed.removeChild(installed.querySelector("[style-id='" + id + "']"));
|
|
|
|
}
|
|
|
|
|
|
|
|
tE("open-manage-link", "openManage");
|
|
|
|
tE("find-styles-link", "findStylesForSite");
|
|
|
|
|
|
|
|
document.getElementById("find-styles-link").addEventListener("click", openLink, false);
|
2013-07-04 22:12:16 +00:00
|
|
|
document.getElementById("open-manage-link").addEventListener("click", openLink, false);
|
2012-08-20 01:14:33 +00:00
|
|
|
|