|
30 | 30 | let matchUrl = isFrameNoUrl |
31 | 31 | ? parent.location.href.split('#')[0] |
32 | 32 | : location.href; |
33 | | - let isOrphaned; |
| 33 | + let isOrphaned, orphanCleanup; |
34 | 34 | let offscreen; |
35 | 35 | // firefox doesn't orphanize content scripts so the old elements stay |
36 | 36 | if (!CHROME) styleInjector.clearOrphans(); |
|
77 | 77 | addEventListener('pageshow', onBFCache); |
78 | 78 |
|
79 | 79 | if (!chrome.tabs) { |
80 | | - dispatchEvent(new Event(chrome.runtime.id)); |
| 80 | + dispatchEvent(new CustomEvent(chrome.runtime.id, {detail: orphanCleanup = Math.random()})); |
81 | 81 | addEventListener(chrome.runtime.id, orphanCheck, true); |
82 | 82 | } |
83 | 83 |
|
|
95 | 95 | else data = !isFrameSameOrigin && !isXml && !chrome.tabs && tryCatch(getStylesViaXhr); |
96 | 96 | // XML in Chrome will be auto-converted to html later, so we can't style it via XHR now |
97 | 97 | await applyStyles(data); |
| 98 | + if (orphanCleanup) { |
| 99 | + dispatchEvent(new Event(orphanCleanup)); |
| 100 | + orphanCleanup = false; |
| 101 | + } |
98 | 102 | } |
99 | 103 |
|
100 | 104 | async function applyStyles(data) { |
|
104 | 108 | Object.assign(own, window[Symbol.for(SYM_ID)] = data); |
105 | 109 | if (!isFrame && own.cfg.top === '') own.cfg.top = location.origin; // used by child frames via parentStyles |
106 | 110 | if (!isFrame && own.cfg.dark !== mqDark.matches) mqDark.onchange(mqDark); |
107 | | - if (styleInjector.list.length) await styleInjector.replace(own); |
108 | | - else if (!own.cfg.off) await styleInjector.apply(own); |
| 111 | + if (styleInjector.list.length) styleInjector.replace(own); |
| 112 | + else if (!own.cfg.off) styleInjector.apply(own); |
109 | 113 | styleInjector.toggle(!own.cfg.off); |
110 | 114 | } |
111 | 115 |
|
|
258 | 262 | if (offscreen) for (const fn of offscreen) fn(); |
259 | 263 | offscreen = null; |
260 | 264 | isOrphaned = true; |
261 | | - setTimeout(styleInjector.clear, 1000); // avoiding FOUC |
| 265 | + styleInjector.shutdown(evt.detail); |
262 | 266 | tryCatch(msg.off, applyOnMessage); |
263 | 267 | } |
264 | 268 | })(); |
0 commit comments