-
Notifications
You must be signed in to change notification settings - Fork 511
Description
Currently when using document-start with Greasemonkey my script runs when the HTML element is created, before the Head element is loaded with new elements. However, the same isn't happening with Tampermonkey, resulting in an unreliable behavior which I was compelled to try and fix by forcing the page to reload in an attempt to get the script running when it should.
With this simple example script for YouTube I get mixed results:
// ==UserScript==
// @version 1
// @name ReadyState check
// @namespace Test script
// @match https://www.youtube.com/*
// @run-at document-start
// @grant none
// @noframes
// ==/UserScript==
(function () {
'use strict';
window.stop();
console.log(document.readyState, document.body || document.querySelector('[name="html5player/html5player"]'));
}());If I open a YouTube page for the first time it usually runs the script right at start when I need it, but if I open a video in a new tab (midle-click on link, for example) then sometimes this isn't true. On some cases the entire head is fully loaded and the body already started loading before the script could run, which can be confirmed by inspecting the page code after the script makes it stop. Also I witness often that the head already contains scripts that I need to load after my script runs, not before, which can be seen in the console.log.
The worst case is when I open a new window via window.open() which the script fails to run when it should every single time for up to 10 or 20 times in a row until it is finally able to run when it should. With the above script this isn't happening because I am not making it reload until it queues correctly, but you can see that the pages will be often complete before the script even has a chance to run.
If I pack the userscript in Chrome into a testing standalone addon, which runs-at document-start there are 0 issues like this, the script runs as soon as the page exists flawlessly, so Tampermonkey should be able too.
I can't tell if this is being caused because the Tampermonkey extension is not running as soon as it should whenever the pages open or if the userscript is not being injected as soon as it should due to some slowliness caused by Tampermonkey, but this behavior is very unreliable and is forcing me to stop considering supporting my scripts for Tampermonkey because I don't know what else I can do if the extension is not reliable enough to run the code when it should or reasonably close to it.
I have no issues with Greasemonkey nor when I convert the userscript into a Chrome extension, it's only with Tampermonkey that I am having these complications which are making the script unusuable.
Can you update the extension to be more reliable in this aspect? Preferably it should follow Greasemonkey's setting, which I quote:
The script will run before any document begins loading, thus before any scripts run or images load.
Instead of Tampermonkey's current:
The script will be injected as fast as possible.
Also I am using Tampermonkey 3.10.84 and running just the one script, no other.