ソースを参照

fix: allow reloading installer's tab in FF68+

tophf 4 年 前
コミット
4c51762686
2 ファイル変更16 行追加3 行削除
  1. 7 1
      src/background/utils/requests.js
  2. 9 2
      src/injected/index.js

+ 7 - 1
src/background/utils/requests.js

@@ -10,6 +10,7 @@ import { extensionRoot } from './init';
 import { commands } from './message';
 
 const VM_VERIFY = 'VM-Verify';
+const CONFIRM_URL_BASE = `${extensionRoot}confirm/index.html#`;
 /** @type {Object<string,VMHttpRequest>} */
 const requests = {};
 const verify = {};
@@ -18,6 +19,11 @@ let encoder;
 
 Object.assign(commands, {
   ConfirmInstall: confirmInstall,
+  async CheckInstallerTab(tabId, src) {
+    const tab = IS_FIREFOX && (src.url || '').startsWith('file:')
+      && await browser.tabs.get(tabId).catch(noop);
+    return tab && (tab.pendingUrl || tab.url || '').startsWith(CONFIRM_URL_BASE);
+  },
   /** @return {void} */
   HttpRequest(opts, src) {
     const { tab: { id: tabId }, frameId } = src;
@@ -454,7 +460,7 @@ async function confirmInstall({ code, from, url }, { tab = {} }) {
     || /^(chrome:\/\/(newtab|startpage)\/|about:(home|newtab))$/.test(from));
   /** @namespace VMConfirmCache */
   cache.put(`confirm-${confirmKey}`, { incognito, url, from, tabId, ff: ua.firefox });
-  const confirmUrl = `/confirm/index.html#${confirmKey}`;
+  const confirmUrl = CONFIRM_URL_BASE + confirmKey;
   const { windowId } = canReplaceCurTab
     ? await browser.tabs.update(tabId, { url: confirmUrl })
     : await commands.TabOpen({ url: confirmUrl, active: !!active }, { tab });

+ 9 - 2
src/injected/index.js

@@ -20,6 +20,7 @@ if (url
     if (!/javascript|^text\/plain|^$/::regexpTest(response.headers.get('content-type') || '')) {
       return;
     }
+    let oldCode;
     let code = await response::getText();
     if (!/==userscript==/i::regexpTest(code)) {
       return;
@@ -28,9 +29,9 @@ if (url
     // FF68+ doesn't allow extension pages to get file: URLs anymore so we need to track it here
     // (detecting FF68 by a feature because we can't use getBrowserInfo here and UA may be altered)
     if (browser.storage.managed) {
+      /** @param {chrome.runtime.Port} */
       browser.runtime.onConnect.addListener(port => {
         if (port.name !== 'FetchSelf') return;
-        let oldCode;
         port.onMessage.addListener(async () => {
           code = await (await fetch(url, fetchOpts))::getText();
           if (code === oldCode) {
@@ -40,7 +41,13 @@ if (url
           }
           port.postMessage(code);
         });
-        port.onDisconnect.addListener(closeSelf);
+        port.onDisconnect.addListener(async () => {
+          oldCode = null;
+          // The user may have reloaded the Confirm page so let's check
+          if (!await sendCmd('CheckInstallerTab', port.sender.tab.id)) {
+            closeSelf();
+          }
+        });
       });
     } else {
       closeSelf();