Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.head is null when @run-at document-start #2515

Closed
byzod opened this issue Jun 15, 2017 · 9 comments
Closed

document.head is null when @run-at document-start #2515

byzod opened this issue Jun 15, 2017 · 9 comments

Comments

@byzod
Copy link

byzod commented Jun 15, 2017

Environment

Key Value
System Platform AMD64, Windows-10-10.0.14393
Browser Firefox Developer Edition (55.0 Beta 1, 64 bit)
User Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0
Build Config https://hg.mozilla.org/releases/mozilla-beta/rev/6872377277a618b2b9e0d2b4c2b9e51765ac199e
Grease Monkey 3.11

Test user script:

// ==UserScript==
// @name        HeadTest
// @namespace   HeadTest
// @include     *
// @run-at      document-start
// @version     1
// @grant       none
// ==/UserScript==
AddGlobalStyle('body{background-color:red;}');

function AddGlobalStyle(css) {
	var head, style;
	head = document.getElementsByTagName("head")[0];
	console.log("[Test]: Head: %o", head); //DEBUG
	console.log("[Test]: document.head: %o", document.head); //DEBUG
}

Expect Result:

[Test]: Head:  <head> 
[Test]: document.head:  <head>

Actual Result:

[Test]: Head:  undefined
[Test]: document.head:  null

This issue also applies to GM_addStyle

@janekptacijarabaci
Copy link
Contributor

janekptacijarabaci commented Jun 15, 2017

Last good: Firefox 55.0a1 (2017-03-17)
Build from https://hg.mozilla.org/mozilla-central/rev/39607304b774591fa6e32c4b06158d869483c312

First bad: Firefox 55.0a1 (2017-03-17)
Build from https://hg.mozilla.org/mozilla-central/rev/23a4b7430dd7e83a2809bf3dc41471f154301eda

Pushlog:
https://hg.mozilla.org/mozilla-central/pushloghtml?fromchange=39607304b774591fa6e32c4b06158d869483c312&tochange=23a4b7430dd7e83a2809bf3dc41471f154301eda

Bug 1333990 is the suspect.

See also #1849

@byzod
Copy link
Author

byzod commented Jun 16, 2017

@janekptacijarabaci Found a workaround:

setTimeout(()=>{
    AddGlobalStyle('body{background-color:red;}');
});

It looks unstable and unreliable, but that's the simplest way I can found currently that make things work.

@janekptacijarabaci
Copy link
Contributor

janekptacijarabaci commented Jun 16, 2017

@the8472
Copy link
Contributor

the8472 commented Jun 23, 2017

I don't think this is a bug. document-start happens very early. This allows userscripts to manipulate the page before any content javascript has executed.

If you want to execute later just use document-end or use mutation observers to wait for a particular element to be inserted.

@byzod
Copy link
Author

byzod commented Jun 23, 2017

@the8472
Maybe you're right.
According to MDN:

document-element-inserted
Sent immediately after the root element of a document has been created, but before executing any script on it.

document-start (based on document-element-inserted) is fired immediately after <html></html> is created, at this time, <head> is null.

But this breaks many scripts that try to add style before the page is rendered (if you use GM_addstyle with document-end you will noticed that the page is blinking) as it works before

I'm not sure this is intentionally

@the8472
Copy link
Contributor

the8472 commented Jun 23, 2017

GM_addStyle is silly anyway, it just inserts a <style> tag into <head>, you can do that yourself, with a mutation observer if necessary.

But if that is the actual concern it could be patched to check for the presence of <head> use observers if it is not.

@byzod
Copy link
Author

byzod commented Jun 23, 2017

@the8472 If the behavior of current document-start is correct and intentionally, then we need to using an observer every time you need to add style
Something like

function addGlobalStyle(css){
  let headHunter = new MutationObserver(
    records => {
    ; check mutation records
    ; is added node's tag name 'head'?
    ; create a style node then throw it in
    ; then disconnect us
   }
  );
  headhunter.observer(document, {childlist : true});
}

is needed for every script that add a style before document-end. That's boring.

I hope GM_addstyle could do that for me, or write some useful warning in the wiki ("GM_addstyle is not available for scripts with @run-at document-start,don't' use it, find a solution yourself", etc)

@the8472
Copy link
Contributor

the8472 commented Jun 23, 2017

Yes, gm_addstyle should be fixed.

But everything else a script might do also has to jump through the same hoops at document-start too, adding styles not special in that regard. Nothing of the DOM is available, so you have to wait for whatever you need. I.e. you need to use observers anyway if you run things at document-start.

@arantius
Copy link
Collaborator

Thanks for the detailed report, but this is WAI, and the 3.x branch is ~dead anyway. (I, for one, welcome our new web extension overlords.) Things like GM_addStyle will be fixed by never existing in 4.x.

Scripts like https://arantius.com/misc/greasemonkey/amazon-url-cleaner.user.js exist which don't need a DOM at all, and should run ASAP.

Repository owner deleted a comment from Swyter Aug 31, 2017
chocolateboy added a commit to chocolateboy/userscripts that referenced this issue Sep 9, 2017
1) a recent change in Firefox has broken the pattern of styling
a page before it's displayed by calling GM_addStyle at
document-start. [1] a workaround hasn't been (and won't be)
implemented in Greasemonkey

work around this by using a mutation observer to detect the
creation of the head element before adding the style

2) fix FOSC on /r/firefox (the blue background) by hiding <html>
rather than <body>

[1] greasemonkey/greasemonkey#2515
alexwh added a commit to alexwh/Bandcamp-Volume-Bar that referenced this issue Jul 15, 2019
due to greasemonkey/greasemonkey#2515 this statement breaks the head injections on line 15 and 19. the script does not appear to require running at document start, and works fine with the default (document-end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants