Fix: content script may load before the background is ready
This commit is contained in:
parent
04c2d6bbf6
commit
0ea7ada48f
|
@ -279,6 +279,8 @@ if (FIREFOX && browser.commands && browser.commands.update) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg.broadcastTab({method: 'backgroundReady'});
|
||||||
|
|
||||||
function reinjectContentScripts() {
|
function reinjectContentScripts() {
|
||||||
const NTP = 'chrome://newtab/';
|
const NTP = 'chrome://newtab/';
|
||||||
const ALL_URLS = '<all_urls>';
|
const ALL_URLS = '<all_urls>';
|
||||||
|
|
|
@ -15,24 +15,8 @@ const APPLY = (() => {
|
||||||
var disabledElements = new Map();
|
var disabledElements = new Map();
|
||||||
var docRewriteObserver;
|
var docRewriteObserver;
|
||||||
var docRootObserver;
|
var docRootObserver;
|
||||||
|
const initializing = init();
|
||||||
|
|
||||||
// FIXME: styleViaAPI
|
|
||||||
// FIXME: getStylesFallback?
|
|
||||||
if (!chrome.app && document instanceof XMLDocument) {
|
|
||||||
API.styleViaAPI({action: 'styleApply'});
|
|
||||||
} else {
|
|
||||||
API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
|
||||||
.then(result => {
|
|
||||||
const styles = Object.values(result);
|
|
||||||
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
||||||
// the browsers, especially Firefox, may apply all transitions on page load
|
|
||||||
applyStyles(styles, () => {
|
|
||||||
if (styles.some(s => s.code.includes('transition'))) {
|
|
||||||
applyTransitionPatch();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
msg.onTab(applyOnMessage);
|
msg.onTab(applyOnMessage);
|
||||||
|
|
||||||
if (!isOwnPage) {
|
if (!isOwnPage) {
|
||||||
|
@ -52,6 +36,25 @@ const APPLY = (() => {
|
||||||
|
|
||||||
const setStyleContent = createSetStyleContent();
|
const setStyleContent = createSetStyleContent();
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
// FIXME: styleViaAPI
|
||||||
|
// FIXME: getStylesFallback?
|
||||||
|
if (!chrome.app && document instanceof XMLDocument) {
|
||||||
|
return API.styleViaAPI({action: 'styleApply'});
|
||||||
|
}
|
||||||
|
return API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
||||||
|
.then(result => {
|
||||||
|
const styles = Object.values(result);
|
||||||
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
||||||
|
// the browsers, especially Firefox, may apply all transitions on page load
|
||||||
|
applyStyles(styles, () => {
|
||||||
|
if (styles.some(s => s.code.includes('transition'))) {
|
||||||
|
applyTransitionPatch();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function pageObject(target) {
|
function pageObject(target) {
|
||||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
|
||||||
const obj = new window.Object();
|
const obj = new window.Object();
|
||||||
|
@ -209,6 +212,14 @@ const APPLY = (() => {
|
||||||
|
|
||||||
case 'ping':
|
case 'ping':
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case 'backgroundReady':
|
||||||
|
initializing.catch(err => {
|
||||||
|
if (msg.RX_NO_RECEIVER.test(err.message)) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
js/msg.js
10
js/msg.js
|
@ -27,6 +27,8 @@ const msg = (() => {
|
||||||
const EXTENSION_URL = chrome.runtime.getURL('');
|
const EXTENSION_URL = chrome.runtime.getURL('');
|
||||||
let handler;
|
let handler;
|
||||||
const from_ = location.href.startsWith(EXTENSION_URL) ? 'extension' : 'content';
|
const from_ = location.href.startsWith(EXTENSION_URL) ? 'extension' : 'content';
|
||||||
|
const RX_NO_RECEIVER = /Receiving end does not exist/;
|
||||||
|
const RX_PORT_CLOSED = /The message port closed before a response was received/;
|
||||||
return {
|
return {
|
||||||
send,
|
send,
|
||||||
sendTab,
|
sendTab,
|
||||||
|
@ -38,7 +40,9 @@ const msg = (() => {
|
||||||
on,
|
on,
|
||||||
onTab,
|
onTab,
|
||||||
onExtension,
|
onExtension,
|
||||||
off
|
off,
|
||||||
|
RX_NO_RECEIVER,
|
||||||
|
RX_PORT_CLOSED
|
||||||
};
|
};
|
||||||
|
|
||||||
function send(data, target = 'extension') {
|
function send(data, target = 'extension') {
|
||||||
|
@ -83,8 +87,8 @@ const msg = (() => {
|
||||||
|
|
||||||
function broadcastError(err) {
|
function broadcastError(err) {
|
||||||
if (err.message && (
|
if (err.message && (
|
||||||
/Receiving end does not exist/.test(err.message) ||
|
RX_NO_RECEIVER.test(err.message) ||
|
||||||
/The message port closed before a response was received/.test(err.message)
|
RX_PORT_CLOSED.test(err.message)
|
||||||
)) {
|
)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user