Просмотр исходного кода

fix: current window may be null at browser startup

tophf 1 год назад
Родитель
Сommit
17d036cff5
2 измененных файлов с 20 добавлено и 8 удалено
  1. 18 2
      src/background/utils/preinject.js
  2. 2 6
      src/background/utils/ua.js

+ 18 - 2
src/background/utils/preinject.js

@@ -1,4 +1,6 @@
-import { getActiveTab, getScriptName, getScriptPrettyUrl, getUniqId, sendTabCmd } from '@/common';
+import {
+  browserWindows, getActiveTab, getScriptName, getScriptPrettyUrl, getUniqId, sendTabCmd
+} from '@/common';
 import {
   __CODE, TL_AWAIT, UNWRAP,
   BLACKLIST, HOMEPAGE_URL, KNOWN_INJECT_INTO, META_STR, METABLOCK_RE, NEWLINE_END_RE,
@@ -22,12 +24,13 @@ import {
 import { clearStorageCache, onStorageChanged } from './storage-cache';
 import { getFrameDocId, getFrameDocIdAsObj, tabsOnRemoved } from './tabs';
 import { addValueOpener, clearValueOpener, reifyValueOpener } from './values';
-import { ua } from './ua';
+import { setBrowserName, ua } from './ua';
 
 let isApplied;
 let injectInto;
 let ffInject;
 let xhrInject = false; // must be initialized for proper comparison when toggling
+let checkedVivaldi = IS_FIREFOX;
 
 const sessionId = getUniqId();
 const API_HEADERS_RECEIVED = browser.webRequest.onHeadersReceived;
@@ -153,6 +156,7 @@ addPublicCommands({
     const frameDoc = getFrameDocId(isTop, src[kDocumentId], frameId);
     const tabId = tab.id;
     if (!url) url = src.url || tab.url;
+    if (!checkedVivaldi) checkVivaldi(tab);
     clearFrameData(tabId, frameDoc);
     let skip = skippedTabs[tabId];
     if (skip > 0) { // first time loading the tab after skipScripts was invoked
@@ -395,6 +399,7 @@ async function prepareBag(cacheKey, url, isTop, env, inject, errors) {
   const bag = { [INJECT]: inject };
   const { allIds, [MORE]: envDelayed } = env;
   const moreKey = envDelayed[IDS].length && getUniqId('more');
+  if (isObject(checkedVivaldi)) await checkedVivaldi;
   Object.assign(inject, {
     [SCRIPTS]: prepareScripts(env),
     [INJECT_INTO]: injectInto,
@@ -664,6 +669,17 @@ function clearFrameData(tabId, frameId, tabRemoved) {
   clearNotifications(tabId, frameId, tabRemoved);
 }
 
+/** Checking on demand because a tab/window definitely exists now,
+ * which is not guaranteed at browser start */
+function checkVivaldi(obj) {
+  checkedVivaldi = true;
+  if (obj.vivExtData/*new*/ || obj.extData/*old*/) {
+    setBrowserName('Vivaldi');
+  } else if ((obj = obj.windowId) != null) {
+    checkedVivaldi = browserWindows.get(obj).then(checkVivaldi);
+  }
+}
+
 function sendPopupShown(tabId, frameDoc) {
   setTimeout(sendTabCmd, 0, tabId, 'PopupShown', true, getFrameDocIdAsObj(frameDoc));
 }

+ 2 - 6
src/background/utils/ua.js

@@ -1,4 +1,3 @@
-import { browserWindows } from '@/common';
 import { addOwnCommands, init } from './init';
 
 export const {
@@ -9,6 +8,7 @@ const uaVer = navUA.match(/\s(?:Chrom(?:e|ium)|Firefox)\/(\d+[.0-9]*)|$/i)[1];
 
 /** @type {VMScriptGMInfoPlatform} */
 export const ua = {};
+export const setBrowserName = name => { ua.browserName = name; };
 /** @type {number|void} This value can be trusted because the only way to spoof it in Chrome/ium
  * is to manually open devtools for the background page in device emulation mode.
  * Using `void` for numeric comparisons like CHROME < 100 to be false in Firefox */
@@ -27,12 +27,10 @@ init.deps.push(
     browser.runtime.getPlatformInfo(),
     browser.runtime.getBrowserInfo?.(),
     navUAD?.getHighEntropyValues(['fullVersionList']),
-    !IS_FIREFOX && browserWindows.getCurrent(),
   ]).then(([
     { os, arch },
     { name, version } = {},
     uadValues,
-    wnd,
   ]) => {
     if (!version && (uadValues = uadValues?.fullVersionList) && uadValues[0]) {
       [name, version] = uadValues.map(({ brand, version: v }) => (
@@ -43,9 +41,7 @@ init.deps.push(
     }
     ua.arch = arch;
     ua.os = os;
-    ua.browserName = wnd && (wnd.vivExtData/*new*/ || wnd.extData/*old*/)
-      ? 'Vivaldi'
-      : name || 'chrome';
+    setBrowserName(name || 'chrome');
     ua.browserVersion = version || uaVer;
     if (FIREFOX) FIREFOX = parseFloat(version);
   })