{"action":"created","issue":{"url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/211","repository_url":"https://api.github.com/repos/Tampermonkey/tampermonkey","labels_url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/211/labels{/name}","comments_url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/211/comments","events_url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/211/events","html_url":"https://github.com/Tampermonkey/tampermonkey/issues/211","id":69382463,"number":211,"title":"Make run-at document-start more reliable","user":{"login":"Yonezpt","id":8498057,"avatar_url":"https://avatars.githubusercontent.com/u/8498057?v=3","gravatar_id":"","url":"https://api.github.com/users/Yonezpt","html_url":"https://github.com/Yonezpt","followers_url":"https://api.github.com/users/Yonezpt/followers","following_url":"https://api.github.com/users/Yonezpt/following{/other_user}","gists_url":"https://api.github.com/users/Yonezpt/gists{/gist_id}","starred_url":"https://api.github.com/users/Yonezpt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Yonezpt/subscriptions","organizations_url":"https://api.github.com/users/Yonezpt/orgs","repos_url":"https://api.github.com/users/Yonezpt/repos","events_url":"https://api.github.com/users/Yonezpt/events{/privacy}","received_events_url":"https://api.github.com/users/Yonezpt/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Tampermonkey/tampermonkey/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/Tampermonkey/tampermonkey/labels/external","name":"external","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2015-04-19T04:27:04Z","updated_at":"2016-09-17T14:11:01Z","closed_at":null,"body":"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.\r\n\r\nWith this simple example script for YouTube I get mixed results:\r\n\r\n````javascript\r\n// ==UserScript==\r\n// @version     1\r\n// @name        ReadyState check\r\n// @namespace   Test script\r\n// @match       https://www.youtube.com/*\r\n// @run-at      document-start\r\n// @grant       none\r\n// @noframes\r\n// ==/UserScript==\r\n(function () {\r\n    'use strict';\r\n    window.stop();\r\n    console.log(document.readyState, document.body || document.querySelector('[name=\"html5player/html5player\"]'));\r\n}());\r\n````\r\n\r\nIf 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.\r\n\r\nThe 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.\r\n\r\nIf 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.\r\n\r\nI 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.\r\n\r\nI 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.\r\n\r\nCan you update the extension to be more reliable in this aspect? Preferably it should follow Greasemonkey's setting, which I quote:\r\n\r\n>The script will run before any document begins loading, thus before any scripts run or images load.\r\n\r\nInstead of Tampermonkey's current:\r\n\r\n>The script will be injected as fast as possible.\r\n\r\nAlso I am using Tampermonkey 3.10.84 and running just the one script, no other."},"comment":{"url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/comments/247773530","html_url":"https://github.com/Tampermonkey/tampermonkey/issues/211#issuecomment-247773530","issue_url":"https://api.github.com/repos/Tampermonkey/tampermonkey/issues/211","id":247773530,"user":{"login":"tophf","id":1310400,"avatar_url":"https://avatars.githubusercontent.com/u/1310400?v=3","gravatar_id":"","url":"https://api.github.com/users/tophf","html_url":"https://github.com/tophf","followers_url":"https://api.github.com/users/tophf/followers","following_url":"https://api.github.com/users/tophf/following{/other_user}","gists_url":"https://api.github.com/users/tophf/gists{/gist_id}","starred_url":"https://api.github.com/users/tophf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tophf/subscriptions","organizations_url":"https://api.github.com/users/tophf/orgs","repos_url":"https://api.github.com/users/tophf/repos","events_url":"https://api.github.com/users/tophf/events{/privacy}","received_events_url":"https://api.github.com/users/tophf/received_events","type":"User","site_admin":false},"created_at":"2016-09-17T14:11:01Z","updated_at":"2016-09-17T14:11:01Z","body":"Also, you can try repeatedly sending (using setInterval) the list of userscripts in `webNavigation.onCommitted` - it'll fail the first time since no content script is running at this point but one of the subsequent attempts should succeed, supposedly, and the content script will receive the data much sooner."}}
{"actor":{"display_login":"tophf"}}