0

The relevant part of my script as mcve:

// ==UserScript==
// @name        mcve
// @namespace   gb
// @include     https://stackoverflow.com/questions/tagged/javascript
// @version     1
// @grant       none
// ==/UserScript==

// this is the one I'm really interested in: the tag entered into SE's tag editor field
let es = document.querySelectorAll('span.post-tag.rendered-element')
console.debug(es)

es = document.querySelectorAll('span.post-tag')
console.debug(es)

es = document.querySelectorAll('span.rendered-element')
console.debug(es)

The resulting output in FF's Web Console:

NodeList[ ]

NodeList[<span.post-tag>] ← Not the one I'm interested in

NodeList[ ]

If I enter the following in Firebug's search field:

  • span.post-tag.rendered-element → finds the one I'm interested in
  • span.post-tag → finds two, the one also found in the code above and the one I'm interested in
  • span.rendered-element → finds the one I'm interested in

which is all as expected.

What am I missing here?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Probably need to wait for javascript to load the elements. Use `waitForKeyElements` or similar. – Brock Adams Aug 16 '17 at 23:42
  • @BrockAdams That's it! Thanks! If I surround my mcve with e.g. `window.setTimeout(function () { ... }, 5000)` all are found. If you make this an answer I'm going to accept it. – Gerold Broser Aug 16 '17 at 23:49
  • 1
    You're welcome, but this Q is more of a duplicate. I'm just too lazy, anymore, to hunt down the refs to close it. ;) – Brock Adams Aug 16 '17 at 23:51
  • @BrockAdams, There are a variety of possibilities. My go-to dup-target for waiting for DOM changes is: [Is there a JavaScript/jQuery DOM change listener?](https://stackoverflow.com/q/2844565), but that covers how to detect changes, not necessarily why you need to wait. OTH, once mentioned Gerold has understood that it's necessary, so that explanation doesn't appear needed. – Makyen Aug 16 '17 at 23:55

0 Answers0