diff --git a/_locales/en/messages.json b/_locales/en/messages.json index f5890926..2cf4d1d7 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -227,6 +227,10 @@ } } }, + "stylishUnavailableForURL": { + "message": "(Stylish does not work on pages like this.)", + "description": "Note in the toolbar pop-up when on a URL Stylish can't affect" + }, "updateCheckFailBadResponseCode": { "message": "Update failed - server responded with code $code$.", "description": "Text that displays when an update check failed because the response code indicates an error", @@ -247,5 +251,13 @@ "updateCompleted": { "message": "Update completed.", "description": "Text that displays when an update completed" + }, + "writeStyleFor": { + "message": "Write style for: ", + "description": "Label for toolbar pop-up that precedes the links to write a new style" + }, + "writeStyleForURL": { + "message": "this URL", + "description": "Text for link in toolbar pop-up to write a new style for the current URL" } } diff --git a/background.js b/background.js index 637d07db..27d0f35e 100644 --- a/background.js +++ b/background.js @@ -295,18 +295,5 @@ function saveFromJSONStyleReloaded(updateType, style, callback) { } } -function getDomains(url) { - if (url.indexOf("file:") == 0) { - return []; - } - var d = /.*?:\/*([^\/]+)/.exec(url)[1]; - var domains = [d]; - while (d.indexOf(".") != -1) { - d = d.substring(d.indexOf(".") + 1); - domains.push(d); - } - return domains; -} - // Get the DB so that any first run actions will be performed immediately when the background page loads. getDatabase(function() {}, reportError); diff --git a/edit.js b/edit.js index 259bce7e..7b94add5 100644 --- a/edit.js +++ b/edit.js @@ -125,10 +125,16 @@ window.addEventListener("load", init, false); function init() { tE("sections-help", "helpAlt", "alt"); - var idMatch = /[&\?]id=([0-9]+)/.exec(location.href) - if (idMatch == null || idMatch.length != 2) { // match should be 2 - one for the whole thing, one for the parentheses + var params = getParams(); + if (!params.id) { // match should be 2 - one for the whole thing, one for the parentheses // This is an add - addSection(); + var section = {code: ""} + if (params.domain) { + section.domains = [params.domain]; + } else if (params.url) { + section.urls = [params.url]; + } + addSection(section); // default to enabled document.getElementById("enabled").checked = true document.title = t("addStyleTitle"); @@ -136,8 +142,7 @@ function init() { return; } // This is an edit - var id = idMatch[1]; - chrome.extension.sendMessage({method: "getStyles", id: id}, function(styles) { + chrome.extension.sendMessage({method: "getStyles", id: params.id}, function(styles) { var style = styles[0]; styleId = style.id; initWithStyle(style); @@ -304,6 +309,19 @@ function showHelp(text) { alert(text); } +function getParams() { + var params = {}; + var urlParts = location.href.split("?", 2); + if (urlParts.length == 1) { + return params; + } + urlParts[1].split("&").forEach(function(keyValue) { + var splitKeyValue = keyValue.split("=", 2); + params[decodeURIComponent(splitKeyValue[0])] = decodeURIComponent(splitKeyValue[1]); + }); + return params; +} + chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { var installed = document.getElementById("installed"); switch(request.name) { diff --git a/manifest.json b/manifest.json index 7eef12fb..1badcda6 100644 --- a/manifest.json +++ b/manifest.json @@ -11,6 +11,7 @@ }, "permissions": [ "tabs", + "webNavigation", "http://userstyles.org/", "https://userstyles.org/" ], diff --git a/popup.html b/popup.html index 6bd79b0a..ebac3416 100644 --- a/popup.html +++ b/popup.html @@ -26,11 +26,14 @@ margin-bottom: 0.5em; border-bottom: 1px solid black; } - #find-styles, #manage-styles { + #find-styles, #write-style, #manage-styles { font-size: smaller; } - #find-styles { - margin-bottom: 0.5em; + #find-styles, #write-style, #unavailable { + margin-bottom: 0.75em; + } + #unavailable { + display: none; } @@ -41,9 +44,12 @@ +
+
+
diff --git a/popup.js b/popup.js index ed120de8..71cd47c5 100644 --- a/popup.js +++ b/popup.js @@ -1,9 +1,54 @@ var styleTemplate = document.createElement("div"); styleTemplate.innerHTML = "
" + t('editStyleLabel') + " " + t('deleteStyleLabel') + "
"; +var writeStyleTemplate = document.createElement("a"); +writeStyleTemplate.className = "write-style-link"; + chrome.tabs.getSelected(null, function(tab) { + var urlWillWork = /^(file|http|https):.*/.test(tab.url); + + if (!urlWillWork) { + ["installed", "find-styles", "write-style"].forEach(function(id) { + document.getElementById(id).style.display = "none"; + }); + document.getElementById("unavailable").style.display = "block"; + return; + } + chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url}, showStyles); - document.querySelector("#find-styles a").href = "http://userstyles.org/styles/browse/all/" + encodeURIComponent(tab.url); + document.querySelector("#find-styles a").href = "https://userstyles.org/styles/browse/all/" + encodeURIComponent(tab.url); + + // Write new style links + var writeStyleLinks = [] + + // For this URL + var urlLink = writeStyleTemplate.cloneNode(true); + urlLink.href = "edit.html?url=" + encodeURIComponent(tab.url); + urlLink.appendChild(document.createTextNode(t("writeStyleForURL"))); + writeStyleLinks.push(urlLink); + document.querySelector("#write-style").appendChild(urlLink) + + // 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)); + writeStyleLinks.push(domainLink); + }); + + var writeStyle = document.querySelector("#write-style"); + writeStyleLinks.forEach(function(link, index) { + if (index > 0) { + writeStyle.appendChild(document.createTextNode(" ")); + } + link.addEventListener("click", openLink, false); + writeStyle.appendChild(link); + }); }); function showStyles(styles) { @@ -89,7 +134,10 @@ function handleDelete(id) { } tE("open-manage-link", "openManage"); +tE("write-style-for", "writeStyleFor"); tE("find-styles-link", "findStylesForSite"); +tE("unavailable", "stylishUnavailableForURL"); -document.getElementById("find-styles-link").addEventListener("click", openLink, false); -document.getElementById("open-manage-link").addEventListener("click", openLink, false); +["find-styles-link", "open-manage-link"].forEach(function(id) { + document.getElementById(id).addEventListener("click", openLink, false); +});