diff --git a/background/icon-manager.js b/background/icon-manager.js index 53360fd5..7c63b6be 100644 --- a/background/icon-manager.js +++ b/background/icon-manager.js @@ -28,13 +28,14 @@ const iconManager = (() => { }); Object.assign(API_METHODS, { - /** @param {(number|string)[]} styleIds */ - updateIconBadge(styleIds) { + /** @param {(number|string)[]} styleIds + * @param {boolean} [lazyBadge=false] preventing flicker during page load */ + updateIconBadge(styleIds, {lazyBadge} = {}) { // FIXME: in some cases, we only have to redraw the badge. is it worth a optimization? const {frameId, tab: {id: tabId}} = this.sender; const value = styleIds.length ? styleIds.map(Number) : undefined; tabManager.set(tabId, 'styleIds', frameId, value); - debounce(refreshStaleBadges, frameId ? 250 : 0); + debounce(refreshStaleBadges, frameId && lazyBadge ? 250 : 0); staleBadges.add(tabId); if (!frameId) refreshIcon(tabId, true); }, @@ -52,7 +53,7 @@ const iconManager = (() => { function onPortDisconnected({sender}) { if (tabManager.get(sender.tab.id, 'styleIds')) { - API_METHODS.updateIconBadge.call({sender}, []); + API_METHODS.updateIconBadge.call({sender}, [], {lazyBadge: true}); } } diff --git a/content/apply.js b/content/apply.js index 895e9b9a..57a630d3 100644 --- a/content/apply.js +++ b/content/apply.js @@ -19,6 +19,7 @@ self.INJECTED !== 1 && (() => { const initializing = init(); /** @type chrome.runtime.Port */ let port; + let lazyBadge = IS_FRAME; // the popup needs a check as it's not a tab but can be opened in a tab manually for whatever reason if (!IS_TAB) { @@ -166,18 +167,18 @@ self.INJECTED !== 1 && (() => { function updateCount() { if (!IS_TAB) return; - if (STYLE_VIA_API) { - API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError); - } else { - API.updateIconBadge(styleInjector.list.map(style => style.id)).catch(msg.ignoreError); - } if (IS_FRAME) { if (!port && styleInjector.list.length) { port = chrome.runtime.connect({name: 'iframe'}); } else if (port && !styleInjector.list.length) { port.disconnect(); } + if (lazyBadge && performance.now() > 1000) lazyBadge = false; } + (STYLE_VIA_API ? + API.styleViaAPI({method: 'updateCount'}) : + API.updateIconBadge(styleInjector.list.map(style => style.id), {lazyBadge}) + ).catch(msg.ignoreError); } function orphanCheck() {