Dim toolbar icon on unstyled tabs, regenerate icons

This commit is contained in:
tophf 2015-05-15 00:24:10 +03:00
parent a7cb548f92
commit f765934316
11 changed files with 57 additions and 30 deletions

BIN
128.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
16.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 395 B

After

Width:  |  Height:  |  Size: 369 B

BIN
19.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 450 B

After

Width:  |  Height:  |  Size: 431 B

BIN
19w.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

BIN
38.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 B

BIN
38w.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 B

BIN
48.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 834 B

After

Width:  |  Height:  |  Size: 711 B

View File

@ -14,8 +14,8 @@ function requestStyles() {
if (location.href.indexOf(chrome.extension.getURL("")) == 0) { if (location.href.indexOf(chrome.extension.getURL("")) == 0) {
var bg = chrome.extension.getBackgroundPage(); var bg = chrome.extension.getBackgroundPage();
if (bg && bg.getStyles) { if (bg && bg.getStyles) {
// apply styles immediately, then proceed with a normal request that will update the icon
bg.getStyles(request, applyStyles); bg.getStyles(request, applyStyles);
return;
} }
} }
chrome.extension.sendMessage(request, applyStyles); chrome.extension.sendMessage(request, applyStyles);

View File

@ -2,6 +2,7 @@
// why the content script also asks for this stuff. // why the content script also asks for this stuff.
chrome.webNavigation.onCommitted.addListener(webNavigationListener.bind(this, "styleApply")); chrome.webNavigation.onCommitted.addListener(webNavigationListener.bind(this, "styleApply"));
chrome.webNavigation.onHistoryStateUpdated.addListener(webNavigationListener.bind(this, "styleReplaceAll")); chrome.webNavigation.onHistoryStateUpdated.addListener(webNavigationListener.bind(this, "styleReplaceAll"));
chrome.webNavigation.onBeforeNavigate.addListener(webNavigationListener.bind(this, null));
function webNavigationListener(method, data) { function webNavigationListener(method, data) {
// Until Chrome 41, we can't target a frame with a message // Until Chrome 41, we can't target a frame with a message
// (https://developer.chrome.com/extensions/tabs#method-sendMessage) // (https://developer.chrome.com/extensions/tabs#method-sendMessage)
@ -11,19 +12,21 @@ function webNavigationListener(method, data) {
return; return;
} }
getStyles({matchUrl: data.url, enabled: true, asHash: true}, function(styleHash) { getStyles({matchUrl: data.url, enabled: true, asHash: true}, function(styleHash) {
chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash}); if (method) {
// Don't show the badge for frames chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash});
if (data.frameId == 0 && prefs.getPref("show-badge")) {
delete styleHash.disableAll;
chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(styleHash)), tabId: data.tabId});
} }
updateIcon({id: data.tabId}, styleHash)
}); });
} }
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.method) { switch (request.method) {
case "getStyles": case "getStyles":
getStyles(request, sendResponse); var styles = getStyles(request, sendResponse);
if (request.matchUrl && sender && sender.tab && sender.frameId == 0) {
// this is a main content frame, so update the icon
updateIcon(sender.tab, styles);
}
return true; return true;
case "saveStyle": case "saveStyle":
saveStyle(request, sendResponse); saveStyle(request, sendResponse);
@ -123,11 +126,11 @@ function getStyles(options, callback) {
} }
}); });
callback(styles); callback(styles);
return styles;
} }
if (cachedStyles) { if (cachedStyles) {
callCallback(); return callCallback();
return;
} }
getDatabase(function(db) { getDatabase(function(db) {

View File

@ -5,7 +5,7 @@
"homepage_url": "https://userstyles.org", "homepage_url": "https://userstyles.org",
"manifest_version": 2, "manifest_version": 2,
"icons": { "icons": {
"16": "16.png", "16": "16.png",
"48": "48.png", "48": "48.png",
"128": "128.png" "128": "128.png"
}, },
@ -43,7 +43,10 @@
], ],
"options_page": "manage.html", "options_page": "manage.html",
"browser_action": { "browser_action": {
"default_icon": "19.png", "default_icon": {
"19": "19w.png",
"38": "38w.png"
},
"default_title": "Stylish", "default_title": "Stylish",
"default_popup": "popup.html" "default_popup": "popup.html"
}, },

View File

@ -3,7 +3,7 @@ function notifyAllTabs(request) {
windows.forEach(function(win) { windows.forEach(function(win) {
win.tabs.forEach(function(tab) { win.tabs.forEach(function(tab) {
chrome.tabs.sendMessage(tab.id, request); chrome.tabs.sendMessage(tab.id, request);
updateBadgeText(tab); updateIcon(tab);
}); });
}); });
}); });
@ -16,24 +16,45 @@ function notifyAllTabs(request) {
chrome.extension.sendMessage(reqPopup); chrome.extension.sendMessage(reqPopup);
} }
function updateBadgeText(tab) { var defaultBadgeColor = "red";
if (prefs.getPref("show-badge")) { chrome.browserAction.getBadgeBackgroundColor({}, function(color) {
function stylesReceived(styles) { defaultBadgeColor = color;
var t = getBadgeText(styles); });
console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
chrome.browserAction.setBadgeText({text: t, tabId: tab.id}); function updateIcon(tab, styles) {
} if (styles) {
// if we have access to this, call directly. a page sending a message to itself doesn't seem to work right. // check for not-yet-existing tabs e.g. omnibox instant search
if (typeof getStyles != "undefined") { chrome.tabs.get(tab.id, function() {
getStyles({matchUrl: tab.url, enabled: true}, stylesReceived); if (!chrome.runtime.lastError) {
} else { // for 'styles' asHash:true fake the length by counting numeric ids manually
chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url, enabled: true}, stylesReceived); if (styles.length === undefined) {
} styles.length = 0;
for (var id in styles) {
styles.length += id.match(/^\d+$/) ? 1 : 0;
}
}
stylesReceived(styles);
}
});
return;
}
// if we have access to this, call directly. a page sending a message to itself doesn't seem to work right.
if (typeof getStyles != "undefined") {
getStyles({matchUrl: tab.url, enabled: true}, stylesReceived);
} else { } else {
chrome.browserAction.setBadgeText({text: "", tabId: tab.id}); chrome.extension.sendMessage({method: "getStyles", matchUrl: tab.url, enabled: true}, stylesReceived);
}
function stylesReceived(styles) {
var disableAll = prefs.getPref("disableAll");
var postfix = styles.length == 0 || disableAll ? "w" : "";
chrome.browserAction.setIcon({
path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"},
tabId: tab.id
});
var t = prefs.getPref("show-badge") && styles.length ? ("" + styles.length) : "";
chrome.browserAction.setBadgeText({text: t, tabId: tab.id});
chrome.browserAction.setBadgeBackgroundColor({color: disableAll ? "#aaa" : defaultBadgeColor});
//console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
} }
} }
function getBadgeText(styles) {
return styles.length == 0 ? "" : ("" + styles.length);
}