2012-08-20 01:14:33 +00:00
|
|
|
var styleTemplate = document.createElement("div");
|
2015-02-17 19:01:15 +00:00
|
|
|
styleTemplate.innerHTML = "<input class='checker' type='checkbox'><div class='style-name'></div><div class='actions'><a href='#' class='enable'>" + t('enableStyleLabel') + "</a> <a href='#' class='disable'>" + t('disableStyleLabel') + "</a> <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
|
|
|
|
2015-01-30 17:28:05 +00:00
|
|
|
var writeStyleTemplate = document.createElement("a");
|
|
|
|
writeStyleTemplate.className = "write-style-link";
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.tabs.getSelected(null, function(tab) {
|
2015-02-18 21:55:40 +00:00
|
|
|
var urlWillWork = /^(file|http|https|chrome\-extension):.*/.exec(tab.url);
|
2015-01-30 17:28:05 +00:00
|
|
|
|
|
|
|
if (!urlWillWork) {
|
|
|
|
["installed", "find-styles", "write-style"].forEach(function(id) {
|
|
|
|
document.getElementById(id).style.display = "none";
|
|
|
|
});
|
|
|
|
document.getElementById("unavailable").style.display = "block";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url}, showStyles);
|
2015-02-18 21:55:40 +00:00
|
|
|
document.querySelector("#find-styles a").href = "https://userstyles.org/styles/browse/all/" + encodeURIComponent("file" === urlWillWork[1] ? "file:" : tab.url);
|
2015-01-30 17:28:05 +00:00
|
|
|
|
|
|
|
// Write new style links
|
2015-02-18 18:56:39 +00:00
|
|
|
var writeStyleLinks = [],
|
|
|
|
container = document.createElement('span');
|
|
|
|
container.id = "match";
|
2015-01-30 17:28:05 +00:00
|
|
|
|
|
|
|
// For this URL
|
|
|
|
var urlLink = writeStyleTemplate.cloneNode(true);
|
|
|
|
urlLink.href = "edit.html?url=" + encodeURIComponent(tab.url);
|
2015-03-02 04:23:03 +00:00
|
|
|
urlLink.appendChild(document.createTextNode( // switchable; default="this URL"
|
|
|
|
localStorage["popup.breadcrumbs.usePath"] !== "true"
|
|
|
|
? t("writeStyleForURL").replace(/ /g, "\u00a0")
|
|
|
|
: /\/\/[^/]+\/(.*)/.exec(tab.url)[1]
|
|
|
|
));
|
|
|
|
urlLink.title = "url(\"$\")".replace("$", tab.url);
|
2015-01-30 17:28:05 +00:00
|
|
|
writeStyleLinks.push(urlLink);
|
|
|
|
document.querySelector("#write-style").appendChild(urlLink)
|
2015-03-02 04:23:03 +00:00
|
|
|
if (localStorage["popup.breadcrumbs"] !== "false") { // switchable; default=enabled
|
|
|
|
urlLink.addEventListener("mouseenter", function(event) { this.parentNode.classList.add("url()") }, false);
|
|
|
|
urlLink.addEventListener("focus", function(event) { this.parentNode.classList.add("url()") }, false);
|
|
|
|
urlLink.addEventListener("mouseleave", function(event) { this.parentNode.classList.remove("url()") }, false);
|
|
|
|
urlLink.addEventListener("blur", function(event) { this.parentNode.classList.remove("url()") }, false);
|
|
|
|
}
|
2015-01-30 17:28:05 +00:00
|
|
|
|
|
|
|
// For domain
|
|
|
|
var domains = getDomains(tab.url)
|
|
|
|
domains.forEach(function(domain) {
|
|
|
|
// Don't include TLD
|
|
|
|
if (domains.length > 1 && domain.indexOf(".") == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var domainLink = writeStyleTemplate.cloneNode(true);
|
|
|
|
domainLink.href = "edit.html?domain=" + encodeURIComponent(domain);
|
|
|
|
domainLink.appendChild(document.createTextNode(domain));
|
2015-03-03 19:31:32 +00:00
|
|
|
domainLink.title = "domain(\"$\")".replace("$", domain);
|
2015-02-18 18:56:39 +00:00
|
|
|
domainLink.setAttribute("subdomain", domain.substring(0, domain.indexOf(".")));
|
2015-01-30 17:28:05 +00:00
|
|
|
writeStyleLinks.push(domainLink);
|
|
|
|
});
|
|
|
|
|
|
|
|
var writeStyle = document.querySelector("#write-style");
|
|
|
|
writeStyleLinks.forEach(function(link, index) {
|
2015-01-30 18:35:37 +00:00
|
|
|
link.addEventListener("click", openLinkInTabOrWindow, false);
|
2015-02-18 18:56:39 +00:00
|
|
|
container.appendChild(link);
|
2015-01-30 17:28:05 +00:00
|
|
|
});
|
2015-03-02 04:23:03 +00:00
|
|
|
if (localStorage["popup.breadcrumbs"] !== "false") {
|
|
|
|
container.classList.add("breadcrumbs");
|
|
|
|
container.appendChild(container.removeChild(container.firstChild));
|
|
|
|
}
|
2015-02-18 18:56:39 +00:00
|
|
|
writeStyle.appendChild(container);
|
2012-08-20 01:14:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function showStyles(styles) {
|
2015-03-03 21:49:23 +00:00
|
|
|
var enabledFirst = localStorage["popup.enabledFirst"] !== "false";
|
|
|
|
styles.sort(function(a, b) {
|
|
|
|
if (enabledFirst && a.enabled !== b.enabled) return !(a.enabled < b.enabled) ? -1 : 1;
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2012-08-20 01:14:33 +00:00
|
|
|
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");
|
2014-10-06 16:13:05 +00:00
|
|
|
checkbox.checked = style.enabled == "true";
|
2014-10-01 19:51:25 +00:00
|
|
|
|
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);
|
2015-01-30 18:38:17 +00:00
|
|
|
editLink.addEventListener("click", openLinkInTabOrWindow, false);
|
2014-10-01 19:51:25 +00:00
|
|
|
|
2014-10-06 16:13:05 +00:00
|
|
|
// the checkbox will not toggle itself after clicking the name, but calling enable will regenerate it
|
|
|
|
styleName.addEventListener("click", function() { enable(event, !event.target.previousSibling.checked); }, false);
|
|
|
|
// clicking the checkbox will toggle it, and this will run after that happens
|
|
|
|
checkbox.addEventListener("click", function() { enable(event, event.target.checked); }, false);
|
2015-02-17 19:01:15 +00:00
|
|
|
e.querySelector(".enable").addEventListener("click", function() { enable(event, true); }, false);
|
|
|
|
e.querySelector(".disable").addEventListener("click", function() { enable(event, false); }, false);
|
2014-10-06 16:13:05 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:35:37 +00:00
|
|
|
function openLinkInTabOrWindow(event) {
|
|
|
|
event.preventDefault();
|
2015-03-03 23:04:27 +00:00
|
|
|
if (prefs.getPref('openEditInWindow', false)) {
|
2015-01-30 18:35:37 +00:00
|
|
|
chrome.windows.create({url: event.target.href});
|
|
|
|
} else {
|
|
|
|
chrome.tabs.create({url: event.target.href});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2015-01-30 17:28:05 +00:00
|
|
|
tE("write-style-for", "writeStyleFor");
|
2012-08-20 01:14:33 +00:00
|
|
|
tE("find-styles-link", "findStylesForSite");
|
2015-01-30 17:28:05 +00:00
|
|
|
tE("unavailable", "stylishUnavailableForURL");
|
2012-08-20 01:14:33 +00:00
|
|
|
|
2015-01-30 17:28:05 +00:00
|
|
|
["find-styles-link", "open-manage-link"].forEach(function(id) {
|
|
|
|
document.getElementById(id).addEventListener("click", openLink, false);
|
|
|
|
});
|