New Tab Page: fixup for icon, badge, popup style list
This commit is contained in:
parent
f765934316
commit
c60a21cf12
3
apply.js
3
apply.js
|
@ -41,9 +41,6 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
|
||||||
case "styleReplaceAll":
|
case "styleReplaceAll":
|
||||||
replaceAll(request.styles, document);
|
replaceAll(request.styles, document);
|
||||||
break;
|
break;
|
||||||
case "realURL":
|
|
||||||
sendResponse(location.href);
|
|
||||||
break;
|
|
||||||
case "styleDisableAll":
|
case "styleDisableAll":
|
||||||
disableAll(request.disableAll);
|
disableAll(request.disableAll);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -15,7 +15,7 @@ function webNavigationListener(method, data) {
|
||||||
if (method) {
|
if (method) {
|
||||||
chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash});
|
chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash});
|
||||||
}
|
}
|
||||||
updateIcon({id: data.tabId}, styleHash)
|
updateIcon({id: data.tabId, url: data.url}, styleHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +23,8 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
|
||||||
switch (request.method) {
|
switch (request.method) {
|
||||||
case "getStyles":
|
case "getStyles":
|
||||||
var styles = getStyles(request, sendResponse);
|
var styles = getStyles(request, sendResponse);
|
||||||
if (request.matchUrl && sender && sender.tab && sender.frameId == 0) {
|
// check if this is a main content frame style enumeration
|
||||||
// this is a main content frame, so update the icon
|
if (request.matchUrl && !request.id && sender && sender.tab && sender.frameId == 0) {
|
||||||
updateIcon(sender.tab, styles);
|
updateIcon(sender.tab, styles);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -65,15 +65,15 @@ chrome.commands.onCommand.addListener(function(command) {
|
||||||
|
|
||||||
// contextMenus API is present in ancient Chrome but it throws an exception
|
// contextMenus API is present in ancient Chrome but it throws an exception
|
||||||
// upon encountering the unsupported parameter value "browser_action", so we have to catch it.
|
// upon encountering the unsupported parameter value "browser_action", so we have to catch it.
|
||||||
try {
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
|
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
|
||||||
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge")
|
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge")
|
||||||
});
|
}, function() { var clearError = chrome.runtime.lastError; });
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
|
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
|
||||||
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll")
|
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll")
|
||||||
});
|
}, function() { var clearError = chrome.runtime.lastError; });
|
||||||
chrome.contextMenus.onClicked.addListener(function(info, tab) {
|
chrome.contextMenus.onClicked.addListener(function(info, tab) {
|
||||||
if (info.menuItemId == "disableAll") {
|
if (info.menuItemId == "disableAll") {
|
||||||
disableAllStylesToggle(info.checked);
|
disableAllStylesToggle(info.checked);
|
||||||
|
@ -81,7 +81,6 @@ chrome.contextMenus.onClicked.addListener(function(info, tab) {
|
||||||
prefs.setPref(info.menuItemId, info.checked);
|
prefs.setPref(info.menuItemId, info.checked);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch(e) {console.error(e)}
|
|
||||||
|
|
||||||
function disableAllStylesToggle(newState) {
|
function disableAllStylesToggle(newState) {
|
||||||
if (newState === undefined || newState === null) {
|
if (newState === undefined || newState === null) {
|
||||||
|
@ -384,13 +383,9 @@ function openURL(options) {
|
||||||
chrome.tabs.highlight({windowId: tabs[0].windowId, tabs: tabs[0].index}, function (window) {});
|
chrome.tabs.highlight({windowId: tabs[0].windowId, tabs: tabs[0].index}, function (window) {});
|
||||||
} else {
|
} else {
|
||||||
delete options.method;
|
delete options.method;
|
||||||
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
|
getActiveTab(function(tab) {
|
||||||
// re-use an active new tab page
|
// re-use an active new tab page
|
||||||
if (tabs.length && tabs[0].url.match(/^chrome:\/\/newtab\/?$/)) {
|
chrome.tabs[tab.url == "chrome://newtab/" ? "update" : "create"](options);
|
||||||
chrome.tabs.update(options);
|
|
||||||
} else {
|
|
||||||
chrome.tabs.create(options);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
48
messaging.js
48
messaging.js
|
@ -22,6 +22,11 @@ chrome.browserAction.getBadgeBackgroundColor({}, function(color) {
|
||||||
});
|
});
|
||||||
|
|
||||||
function updateIcon(tab, styles) {
|
function updateIcon(tab, styles) {
|
||||||
|
// while NTP is still loading only process the request for its main frame with a real url
|
||||||
|
// (but when it's loaded we should process style toggle requests from popups, for example)
|
||||||
|
if (tab.url == "chrome://newtab/" && tab.status != "complete") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (styles) {
|
if (styles) {
|
||||||
// check for not-yet-existing tabs e.g. omnibox instant search
|
// check for not-yet-existing tabs e.g. omnibox instant search
|
||||||
chrome.tabs.get(tab.id, function() {
|
chrome.tabs.get(tab.id, function() {
|
||||||
|
@ -38,15 +43,17 @@ function updateIcon(tab, styles) {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if we have access to this, call directly. a page sending a message to itself doesn't seem to work right.
|
getTabRealURL(tab, function(url) {
|
||||||
if (typeof getStyles != "undefined") {
|
// if we have access to this, call directly. a page sending a message to itself doesn't seem to work right.
|
||||||
getStyles({matchUrl: tab.url, enabled: true}, stylesReceived);
|
if (typeof getStyles != "undefined") {
|
||||||
} else {
|
getStyles({matchUrl: url, enabled: true}, stylesReceived);
|
||||||
chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url, enabled: true}, stylesReceived);
|
} else {
|
||||||
}
|
chrome.extension.sendMessage({method: "getStyles", matchUrl: url, enabled: true}, stylesReceived);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function stylesReceived(styles) {
|
function stylesReceived(styles) {
|
||||||
var disableAll = prefs.getPref("disableAll");
|
var disableAll = "disableAll" in styles ? styles.disableAll : prefs.getPref("disableAll");
|
||||||
var postfix = styles.length == 0 || disableAll ? "w" : "";
|
var postfix = styles.length == 0 || disableAll ? "w" : "";
|
||||||
chrome.browserAction.setIcon({
|
chrome.browserAction.setIcon({
|
||||||
path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"},
|
path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"},
|
||||||
|
@ -58,3 +65,30 @@ function updateIcon(tab, styles) {
|
||||||
//console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
|
//console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getActiveTab(callback) {
|
||||||
|
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
|
||||||
|
callback(tabs[0]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActiveTabRealURL(callback) {
|
||||||
|
getActiveTab(function(tab) {
|
||||||
|
getTabRealURL(tab, callback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTabRealURL(tab, callback) {
|
||||||
|
if (tab.url != "chrome://newtab/") {
|
||||||
|
callback(tab.url);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
chrome.webNavigation.getAllFrames({tabId: tab.id}, function(frames) {
|
||||||
|
frames.some(function(frame) {
|
||||||
|
if (frame.parentFrameId == -1) { // parentless frame is the main frame
|
||||||
|
callback(frame.url);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
16
popup.js
16
popup.js
|
@ -24,17 +24,7 @@ if (!prefs.getPref("popup.stylesFirst")) {
|
||||||
document.body.insertBefore(document.querySelector("body > .actions"), installed);
|
document.body.insertBefore(document.querySelector("body > .actions"), installed);
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
getActiveTabRealURL(updatePopUp);
|
||||||
// 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) {
|
function updatePopUp(url) {
|
||||||
var urlWillWork = /^(file|http|https|chrome\-extension):/.exec(url);
|
var urlWillWork = /^(file|http|https|chrome\-extension):/.exec(url);
|
||||||
|
@ -195,8 +185,8 @@ function handleUpdate(style) {
|
||||||
if (styleElement) {
|
if (styleElement) {
|
||||||
installed.replaceChild(createStyleElement(style), styleElement);
|
installed.replaceChild(createStyleElement(style), styleElement);
|
||||||
} else {
|
} else {
|
||||||
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
|
getActiveTabRealURL(function(url) {
|
||||||
if (tabs.length && chrome.extension.getBackgroundPage().getApplicableSections(style, tabs[0].url).length) {
|
if (chrome.extension.getBackgroundPage().getApplicableSections(style, url).length) {
|
||||||
// a new style for the current url is installed
|
// a new style for the current url is installed
|
||||||
document.getElementById("unavailable").style.display = "none";
|
document.getElementById("unavailable").style.display = "none";
|
||||||
installed.appendChild(createStyleElement(style));
|
installed.appendChild(createStyleElement(style));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user