diff --git a/.eslintrc b/.eslintrc index 077ef42c..af049f37 100644 --- a/.eslintrc +++ b/.eslintrc @@ -38,6 +38,7 @@ globals: sessionStorageHash: false download: false doTimeout: false + invokeOrPostpone: false # localization.js template: false t: false diff --git a/backup/fileSaveLoad.js b/backup/fileSaveLoad.js index e8fd7de4..d44e978f 100644 --- a/backup/fileSaveLoad.js +++ b/backup/fileSaveLoad.js @@ -287,10 +287,9 @@ function importFromString(jsonString) { const message = {method: 'styleReplaceAll', styles}; if (tab.id == ownTab.id) { applyOnMessage(message); - } else if (tab.id == activeTab.id) { - chrome.tabs.sendMessage(tab.id, message, ignoreChromeError); } else { - setTimeout(chrome.tabs.sendMessage, 0, tab.id, message, ignoreChromeError); + invokeOrPostpone(tab.id == activeTab.id, + chrome.tabs.sendMessage, tab.id, message, ignoreChromeError); } setTimeout(BG.updateIcon, 0, tab, styles); if (tab == lastTab) { diff --git a/messaging.js b/messaging.js index f7ffeea6..42f8b593 100644 --- a/messaging.js +++ b/messaging.js @@ -81,11 +81,7 @@ function notifyAllTabs(msg) { ]).then(([tabs, activeTab]) => { const activeTabId = activeTab && activeTab.id; for (const tab of tabs) { - if (tab.id === activeTabId) { - notifyTab(tab); - } else { - setTimeout(notifyTab, 0, tab); - } + invokeOrPostpone(tab.id === activeTabId, notifyTab, tab); } }); } @@ -366,3 +362,10 @@ function doTimeout(ms = 0, ...args) { ? () => new Promise(resolve => setTimeout(resolve, ms, ...args)) : new Promise(resolve => setTimeout(resolve, 0, ...args)); } + + +function invokeOrPostpone(isInvoke, fn, ...args) { + return isInvoke + ? fn(...args) + : setTimeout(invokeOrPostpone, 0, true, fn, ...args); +} diff --git a/popup.js b/popup.js index 20039c58..9c5e8e5c 100644 --- a/popup.js +++ b/popup.js @@ -246,11 +246,7 @@ function createStyleElement({ $('.disable', entry).onclick = handleEvent.toggle; $('.delete', entry).onclick = handleEvent.delete; - if (postponeDetect) { - setTimeout(detectSloppyRegexps, 0, {entry, style}); - } else { - detectSloppyRegexps({entry, style}); - } + invokeOrPostpone(!postponeDetect, detectSloppyRegexps, {entry, style}); const oldElement = $('#style-' + style.id); if (oldElement) {