Prechádzať zdrojové kódy

fix: abort GM_xmlhttpRequest on page redirect

Gerald 5 rokov pred
rodič
commit
bb5ba2e1b2
1 zmenil súbory, kde vykonal 33 pridanie a 1 odobranie
  1. 33 1
      src/background/utils/requests.js

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

@@ -9,17 +9,28 @@ import { commands } from './message';
 const VM_VERIFY = 'VM-Verify';
 const VM_VERIFY = 'VM-Verify';
 const requests = {};
 const requests = {};
 const verify = {};
 const verify = {};
+const tabRequests = {};
 
 
 Object.assign(commands, {
 Object.assign(commands, {
   ConfirmInstall: confirmInstall,
   ConfirmInstall: confirmInstall,
   /** @return {string} */
   /** @return {string} */
-  GetRequestId(eventsToNotify = []) {
+  GetRequestId(eventsToNotify = [], src) {
     const id = getUniqId();
     const id = getUniqId();
+    const tabId = src.tab?.id;
     requests[id] = {
     requests[id] = {
       id,
       id,
+      tabId,
       eventsToNotify,
       eventsToNotify,
       xhr: new XMLHttpRequest(),
       xhr: new XMLHttpRequest(),
     };
     };
+    if (tabId) {
+      let set = tabRequests[tabId];
+      if (!set) {
+        set = new Set();
+        tabRequests[tabId] = set;
+      }
+      set.add(id);
+    }
     return id;
     return id;
   },
   },
   /** @return {void} */
   /** @return {void} */
@@ -300,6 +311,7 @@ function clearRequest(req) {
   if (req.coreId) delete verify[req.coreId];
   if (req.coreId) delete verify[req.coreId];
   delete requests[req.id];
   delete requests[req.id];
   HeaderInjector.del(req.id);
   HeaderInjector.del(req.id);
+  tabRequests[req.tabId]?.delete(req.id);
 }
 }
 
 
 function decodeBody(obj) {
 function decodeBody(obj) {
@@ -462,3 +474,23 @@ async function maybeInstallUserJs(tabId, url) {
     if (tabId >= 0) browser.tabs.update(tabId, { url });
     if (tabId >= 0) browser.tabs.update(tabId, { url });
   }
   }
 }
 }
+
+browser.tabs.onUpdated.addListener((tabId, changes) => {
+  if (changes.status === 'loading') {
+    clearRequestsByTabId(tabId);
+  }
+});
+
+browser.tabs.onRemoved.addListener((tabId) => {
+  clearRequestsByTabId(tabId);
+});
+
+function clearRequestsByTabId(tabId) {
+  const set = tabRequests[tabId];
+  if (set) {
+    delete tabRequests[tabId];
+    for (const id of set) {
+      commands.AbortRequest(id);
+    }
+  }
+}