From d74dd209312278c1e5fc2534d71fe62208aefa45 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 23 Mar 2015 13:56:11 -0500 Subject: [PATCH] Fix pop-up when dealing with newtab #80 --- apply.js | 3 +++ popup.js | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/apply.js b/apply.js index 4a15c931..569b5303 100644 --- a/apply.js +++ b/apply.js @@ -21,6 +21,9 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { case "styleReplaceAll": replaceAll(request.styles, document); break; + case "realURL": + sendResponse(location.href); + break; } }); diff --git a/popup.js b/popup.js index 10f578bf..a1538259 100644 --- a/popup.js +++ b/popup.js @@ -8,16 +8,27 @@ if (!prefs.getPref("popup.stylesFirst")) { document.body.insertBefore(document.querySelector("body > .actions"), document.getElementById("installed")); } -chrome.tabs.getSelected(null, function(tab) { - var urlWillWork = /^(file|http|https|chrome\-extension):/.exec(tab.url); +chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + // Only one is active, I hope + var tab = tabs[0]; + // The new tab lies about what it is. + if (tab.url == "chrome://newtab/") { + chrome.tabs.sendMessage(tab.id, {method: "realURL"}, null, updatePopUp); + } else { + updatePopUp(tab.url); + } +}); + +function updatePopUp(url) { + var urlWillWork = /^(file|http|https|chrome\-extension):/.exec(url); if (!urlWillWork) { document.body.classList.add("blocked"); tE("unavailable", "stylishUnavailableForURL"); } - chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url}, showStyles); - document.querySelector("#find-styles a").href = "https://userstyles.org/styles/browse/all/" + encodeURIComponent("file" === urlWillWork[1] ? "file:" : tab.url); + chrome.extension.sendMessage({method: "getStyles", matchUrl: url}, showStyles); + document.querySelector("#find-styles a").href = "https://userstyles.org/styles/browse/all/" + encodeURIComponent("file" === urlWillWork[1] ? "file:" : url); // Write new style links var writeStyleLinks = [], @@ -26,13 +37,13 @@ chrome.tabs.getSelected(null, function(tab) { // For this URL var urlLink = writeStyleTemplate.cloneNode(true); - urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(tab.url); + urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(url); urlLink.appendChild(document.createTextNode( // switchable; default="this URL" !prefs.getPref("popup.breadcrumbs.usePath") ? t("writeStyleForURL").replace(/ /g, "\u00a0") - : /\/\/[^/]+\/(.*)/.exec(tab.url)[1] + : /\/\/[^/]+\/(.*)/.exec(url)[1] )); - urlLink.title = "url-prefix(\"$\")".replace("$", tab.url); + urlLink.title = "url-prefix(\"$\")".replace("$", url); writeStyleLinks.push(urlLink); document.querySelector("#write-style").appendChild(urlLink) if (prefs.getPref("popup.breadcrumbs")) { // switchable; default=enabled @@ -43,7 +54,7 @@ chrome.tabs.getSelected(null, function(tab) { } // For domain - var domains = getDomains(tab.url) + var domains = getDomains(url) domains.forEach(function(domain) { // Don't include TLD if (domains.length > 1 && domain.indexOf(".") == -1) { @@ -67,7 +78,7 @@ chrome.tabs.getSelected(null, function(tab) { container.appendChild(container.removeChild(container.firstChild)); } writeStyle.appendChild(container); -}); +} function showStyles(styles) { var enabledFirst = prefs.getPref("popup.enabledFirst");