Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.21k stars 418 forks source link

GM_webRequest listener was called twice when redirect a request to itself #1813

Closed Nauxscript closed 1 year ago

Nauxscript commented 1 year ago

(Please fill out the issue template with your details)

Expected Behavior

I don’t know, maybe it can provide a parameter to decide whether or not to redirect the request? I only want to listen if a request is completed.

Actual Behavior

Every request triggers the listener twice.

Specifications

Script

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.google.com/search?q=1&sxsrf=AB5stBgvWP6U-1ixZUuq12nQGotSn2iTRQ%3A1688541174747&source=hp&ei=9helZMeoK53B1e8PkNi1yAs&iflsig=AD69kcEAAAAAZKUmBklWhECBlqZuZW3V3LP8Jtn496CF&ved=0ahUKEwjH-6CHgvf_AhWdYPUHHRBsDbkQ4dUDCAk&uact=5&oq=1&gs_lcp=Cgdnd3Mtd2l6EAMyCwgAEIAEELEDEIMBMgsIABCABBCxAxCDATILCAAQgAQQsQMQgwEyBQgAEIAEMgsILhCABBCxAxCDATILCAAQgAQQsQMQgwEyCwgAEIAEELEDEIMBMgUIABCABDIFCAAQgAQyCwgAEIAEELEDEIMBOgcIIxDqAhAnUIYDWIYDYN8PaAFwAHgAgAF9iAF9kgEDMC4xmAEAoAEBsAEK&sclient=gws-wiz
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        GM_webRequest
// @match        https://www.google.com/*
// @match        https://www.google.com/complete/search*
// ==/UserScript==

(function() {
    'use strict';
    GM_webRequest([
      { selector: 'https://www.google.com/complete/search*', action: { redirect: { from: "(.*)", to: "$1" } } },
    ], function (info, message, details) {
      console.log(info, message, details);
    });
})();

After using the above script, when I input something into google.com to trigger the listener, it outputs two logs in the dev-tool, and the network panel records three requests. However, if I disable this script, there is only one request record.

derjanb commented 1 year ago

{ redirect: { from: "(.*)", to: "$1" } }

Looks like your redirecting to the very same URL. So repeated listeners calls are to be expected, right?

Nauxscript commented 1 year ago

Looks like your redirecting to the very same URL. So repeated listeners calls are to be expected, right?

Yes, In fact, I only want to monitor the completion of these requests, not to cancel or redirect them. It is achieved based on #1086.

Nauxscript commented 1 year ago

@derjanb Is there a way to monitor the completion status of certain requests? I do not need to redirect or cancel them.

7nik commented 1 year ago

It is usually done with a hook on fetch, like:

const origFetch = fetch;
unsafeWindow.fetch = async (url, params) => {
  console.log("request started", url);
  const resp = await origFetch(url, params);
  console.log("request finished", url);
  return resp;
};

Similar can be done to XMLHttpRequest if it is used.