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.28k stars 423 forks source link

In API Dynamic mode, @noframes is not working #2170

Closed jb222 closed 2 weeks ago

jb222 commented 2 months ago

First off, the new Userscripts API Dynamic mode is amazingly fast. So, to prevent errors, a DOMContentLoaded eventlistener was necessary. The userscript logs DOMContentLoaded even before the content script does! 👍
It would be great if we could set the mode per script, e.g. with something like // @dynamic_mode.

Install the script. Navigate to my test. Open the console. The web page includes a (same-origin) page in the iframe.

Expected Behavior

The userscript should only work in the parent window, not in the iframe.

Actual Behavior

Unlike Userscripts API mode, in Userscripts API Dynamic mode the <p> is appended to parent AND child window.

Specifications

Script

// ==UserScript==
// @name         Dynamic Mode Test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @noframes
// @match        *://*.torporama.com/tm/*
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    console.log("userscript started...");
    document.addEventListener("DOMContentLoaded", (event) => {
        console.log("DOMContentLoaded, userscript appending element...");
        let el = document.createElement("p");
        el.textContent = "This <p> was appended by the userscript.";
        document.body.appendChild(el);
    });
})();
derjanb commented 2 months ago

I can reproduce the issue...

derjanb commented 2 months ago

Should be fixed at 5.3.6209 (crx)

Please download the crx file linked above and drag and drop it to the extensions page chrome://extensions (after you've enabled 'Developer Mode').

For a quick fix please export your settings and scripts as zip or (JSON) file at the "Utilities" tab and import it back at the fixed BETA version.

jb222 commented 2 months ago

Thanks for the quick fix, it's working now as expected. Only in API mode, the console shows content: DOMContentLoaded and content: load twice. Is the event triggered by both parent and child windows?

Scherm­afbeelding 2024-09-06 om 10 08 32

derjanb commented 2 months ago

Is the event triggered by both parent and child windows?

Yes, the "content" script is injected into every frame at the moment. This needs further improvement.

jb222 commented 2 months ago

But apparently not in API Dynamic mode. Thanks!

derjanb commented 2 months ago

But apparently not in API Dynamic mode.

Ah, I see. Actually in both modes, but in "UserScripts API Dynamic" all script information is present at real document start and therefore all processing is stopped if no script is supposed to run. In "UserScript API" mode the content script has to wait for the background worker to answer and therefore continues processing until the script information arrived.