From a7445011a9c7c3c84592a36eaec939c4a9b59aa0 Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 9 Sep 2019 12:40:07 +0800 Subject: [PATCH] Enhance: faster msg.sendBg (#774) --- js/msg.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/msg.js b/js/msg.js index 37d87a12..5bbef7a4 100644 --- a/js/msg.js +++ b/js/msg.js @@ -41,11 +41,19 @@ const msg = (() => { if (isBg) { return Promise.resolve(window); } - if (!chrome.runtime.getBackgroundPage) { - return Promise.resolve(); + // try using extension.getBackgroundPage because runtime.getBackgroundPage is too slow + // https://github.com/openstyles/stylus/issues/771 + if (chrome.extension.getBackgroundPage) { + const bg = chrome.extension.getBackgroundPage(); + if (bg && bg.document && bg.document.readyState !== 'loading') { + return Promise.resolve(bg); + } } - return promisify(chrome.runtime.getBackgroundPage.bind(chrome.runtime))() - .catch(() => null); + if (chrome.runtime.getBackgroundPage) { + return promisify(chrome.runtime.getBackgroundPage.bind(chrome.runtime))() + .catch(() => null); + } + return Promise.resolve(null); } function send(data, target = 'extension') {