Skip to content

Commit

Permalink
Fix: make sure icons are refreshed at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Oct 11, 2018
1 parent c657d7e commit 3e38810
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
40 changes: 34 additions & 6 deletions background/background.js
Expand Up @@ -142,11 +142,27 @@ prefs.subscribe([
], () => debounce(refreshIconBadgeColor));

prefs.subscribe([
'show-badge',
'show-badge'
], () => debounce(refreshIconBadgeText));

prefs.subscribe([
'disableAll',
'iconset',
], () => 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}) => {
Expand Down Expand Up @@ -348,14 +364,19 @@ function updateIconBadge(tabId, count) {
if (tabIcon.count === count) {
return;
}
const oldCount = tabIcon.count;
tabIcon.count = count;
refreshIconBadgeText(tabId, tabIcon);
if (Boolean(oldCount) !== Boolean(count)) {
refreshIcon(tabId, tabIcon);
}
}

function refreshIconBadgeText(tabId, icon) {
iconUtil.setBadgeText({
text: prefs.get('show-badge') && count ? String(count) : '',
text: prefs.get('show-badge') && icon.count ? String(icon.count) : '',
tabId
});
if (!count) {
refreshIcon(tabId, tabIcon);
}
}

function refreshIcon(tabId, icon) {
Expand Down Expand Up @@ -392,6 +413,13 @@ function refreshAllIcons() {
for (const [tabId, icon] of tabIcons) {
refreshIcon(tabId, icon);
}
refreshIcon(null, {}); // default icon
}

function refreshAllIconsBadgeText() {
for (const [tabId, icon] of tabIcons) {
refreshIconBadgeText(tabId, icon);
}
}

function onRuntimeMessage(msg, sender) {
Expand Down
11 changes: 8 additions & 3 deletions content/apply.js
Expand Up @@ -74,6 +74,7 @@ const APPLY = (() => {
};

function injectPageScript() {
// FIXME: does it work with XML?
const script = document.createElement('script');
const scriptContent = EVENT_NAME => {
document.currentScript.remove();
Expand Down Expand Up @@ -252,7 +253,7 @@ const APPLY = (() => {
method: 'invokeAPI',
name: 'updateIconBadge',
args: [count]
});
}).catch(() => {});
}

function applyStyleState({id, enabled}) {
Expand Down Expand Up @@ -291,7 +292,9 @@ const APPLY = (() => {

function applyStyles(styles, done) {
if (!styles.length) {
done();
if (done) {
done();
}
return;
}

Expand Down Expand Up @@ -326,7 +329,9 @@ const APPLY = (() => {

updateExposeIframes();
updateCount();
done();
if (done) {
done();
}
}

function applySections(id, code) {
Expand Down

0 comments on commit 3e38810

Please sign in to comment.