Fix: make sure icons are refreshed at startup

This commit is contained in:
eight 2018-10-11 18:13:24 +08:00
parent c657d7e55c
commit 3e38810a49
2 changed files with 43 additions and 10 deletions

View File

@ -142,11 +142,27 @@ prefs.subscribe([
], () => debounce(refreshIconBadgeColor)); ], () => debounce(refreshIconBadgeColor));
prefs.subscribe([ prefs.subscribe([
'show-badge', 'show-badge'
], () => debounce(refreshIconBadgeText));
prefs.subscribe([
'disableAll',
'iconset', 'iconset',
], () => debounce(refreshAllIcons)); ], () => debounce(refreshAllIcons));
prefs.initializing.then(refreshIconBadgeColor); prefs.initializing.then(() => {
refreshIconBadgeColor();
refreshAllIconsBadgeText();
refreshAllIcons();
});
navigatorUtil.onUrlChange(({tabId, frameId}, type) => {
if (type === 'committed' && !frameId) {
// it seems that the tab icon would be reset when pressing F5. We
// invalidate the cache here so it would be refreshed.
tabIcons.delete(tabId);
}
});
// ************************************************************************* // *************************************************************************
chrome.runtime.onInstalled.addListener(({reason}) => { chrome.runtime.onInstalled.addListener(({reason}) => {
@ -348,16 +364,21 @@ function updateIconBadge(tabId, count) {
if (tabIcon.count === count) { if (tabIcon.count === count) {
return; return;
} }
const oldCount = tabIcon.count;
tabIcon.count = count; tabIcon.count = count;
iconUtil.setBadgeText({ refreshIconBadgeText(tabId, tabIcon);
text: prefs.get('show-badge') && count ? String(count) : '', if (Boolean(oldCount) !== Boolean(count)) {
tabId
});
if (!count) {
refreshIcon(tabId, tabIcon); refreshIcon(tabId, tabIcon);
} }
} }
function refreshIconBadgeText(tabId, icon) {
iconUtil.setBadgeText({
text: prefs.get('show-badge') && icon.count ? String(icon.count) : '',
tabId
});
}
function refreshIcon(tabId, icon) { function refreshIcon(tabId, icon) {
const disableAll = prefs.get('disableAll'); const disableAll = prefs.get('disableAll');
const iconset = prefs.get('iconset') === 1 ? 'light/' : ''; const iconset = prefs.get('iconset') === 1 ? 'light/' : '';
@ -392,6 +413,13 @@ function refreshAllIcons() {
for (const [tabId, icon] of tabIcons) { for (const [tabId, icon] of tabIcons) {
refreshIcon(tabId, icon); refreshIcon(tabId, icon);
} }
refreshIcon(null, {}); // default icon
}
function refreshAllIconsBadgeText() {
for (const [tabId, icon] of tabIcons) {
refreshIconBadgeText(tabId, icon);
}
} }
function onRuntimeMessage(msg, sender) { function onRuntimeMessage(msg, sender) {

View File

@ -74,6 +74,7 @@ const APPLY = (() => {
}; };
function injectPageScript() { function injectPageScript() {
// FIXME: does it work with XML?
const script = document.createElement('script'); const script = document.createElement('script');
const scriptContent = EVENT_NAME => { const scriptContent = EVENT_NAME => {
document.currentScript.remove(); document.currentScript.remove();
@ -252,7 +253,7 @@ const APPLY = (() => {
method: 'invokeAPI', method: 'invokeAPI',
name: 'updateIconBadge', name: 'updateIconBadge',
args: [count] args: [count]
}); }).catch(() => {});
} }
function applyStyleState({id, enabled}) { function applyStyleState({id, enabled}) {
@ -291,7 +292,9 @@ const APPLY = (() => {
function applyStyles(styles, done) { function applyStyles(styles, done) {
if (!styles.length) { if (!styles.length) {
done(); if (done) {
done();
}
return; return;
} }
@ -326,7 +329,9 @@ const APPLY = (() => {
updateExposeIframes(); updateExposeIframes();
updateCount(); updateCount();
done(); if (done) {
done();
}
} }
function applySections(id, code) { function applySections(id, code) {