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.15k stars 415 forks source link

[BUG] A problem with the injection cache of GM Value in `UserScripts API Dynamic` mode. `MV3` #2123

Open F9y4ng opened 1 month ago

F9y4ng commented 1 month ago

Actual Behavior

Someone reported an ISSUE to me, regarding the problem that the page refreshed after the script dropdown menu operation was invalid.

After testing, I found that there was a problem with the injection cache of GM Value in UserScripts API Dynamic mode, which should be the same as the cache problem I reported #1899 in Violentmonkey nearly a year ago.

When some sites (such as: www.ithome.com) contain the following <meta> tags, GM_getValue will always get an expired cached value, which is wrong.

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">

Specifications

Script

(Please give an example of the script if applicable.)

// ==UserScript==
// @name         Test cache
// @namespace    Test cache
// @version      0.1
// @description  Test cache
// @author       You
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @run-at       document-start
// ==/UserScript==

(function () {
  "use strict";
  const val = GM_getValue("test");
  console.log("test value:", val);
  if (val === "true") {
    GM_registerMenuCommand(`SET FALSE`, () => {
      GM_setValue("test", "false");
      reload();
    });
  } else {
    GM_registerMenuCommand(`SET TRUE`, () => {
      GM_setValue("test", "true");
      reload();
    });
  }

  function reload() {
    location.reload();
  }
})();
derjanb commented 1 month ago

The problem is re-configuration of the injected userScripts takes time. So I think you can only have real @run-at document-start at the cost of this re-configuration or a slightly delayed document-start with just happened storage changes. GM.getValue will soon make this re-configuration unnecessary, at the cost of a delay when retrieving config values.

However, I'll double check if I can do anything to mitigate the problem...

F9y4ng commented 1 month ago

Before you solve this problem fundamentally, I came up with a temporary method to extend the waiting time of GM Value and then refresh the page, e.g.

  function reload() {
    setTimeout(() => location.reload(), 200);
  }

Judging from the test results, it seems to temporarily solve some problems. However, I'm not sure if it still works in other scenarios.

F9y4ng commented 1 month ago

@derjanb I found a similar problem in UserScripts API Dynamic mode. After turning off a script in the extension drop-down menu, it needs to be manually refreshed to take effect. I think this is also a cache issue.

derjanb commented 1 month ago

What needs to be refreshed manually?

Toggling a script is working fine here. Disable the script -> reload the page -> script is gone -> enable the script -> reload the page -> the script is running.

F9y4ng commented 1 month ago

I tested TM5.2.2 and TM5.3.6204 on chrome and brave respectively and both reproduce the problem as shown:

chrome_TM5.2.2

brave_TM5.3.6204

derjanb commented 1 month ago

Do you use the "Auto reload pages" option?

F9y4ng commented 1 month ago

Do you use the "Auto reload pages" option?

Yes.

And in UserScripts API mode it works fine, but in UserScripts API Dynamic mode not.

derjanb commented 1 month ago

Ah, I see. Thanks.

F9y4ng commented 1 month ago

Hi @derjanb, In UserScripts API Dynamic mode, toggling "Enabled" in the extension drop-down menu will not make the settings take effect, even if refresh the page manually.

I think it's the same caching issue.

F9y4ng commented 1 month ago

In UserScripts API Dynamic mode, toggling "Enabled" in the extension drop-down menu will not make the settings take effect, even if refresh the page manually.

Chrome 126.0.6478.183 and TM5.2.2/TM5.3.6204

But it is normal in UserScripts API mode.

derjanb commented 1 month ago

Script and extension disable issues should be fixed at 5.3.6205 (crx)

F9y4ng commented 1 month ago

Thanks for fixing the issue.