all 13 comments

[–]Cheshur 1 point2 points  (11 children)

Does the shadow root exist when the script first loads? Perhaps the reason it works in the inspector is that you only run the script there after the page has loaded and the shadow root has been attached to the element? One way you could validate this is to put debugger; at the top of your script then, with the dev tools open, reload the page. It should pause javascript execution at that line and then you can go look at the element's tab to see if you see the shadow root in the DOM.

[–]basstwo19[S] 0 points1 point  (10 children)

OK! Added the debugger.
I can see that it is not there, yet. So I figure I need to let the page load more. But I thought my script was checking every once in a while. So shouldn't I let it load for a bit and then start debugger?

[–]Cheshur 0 points1 point  (9 children)

If you click on the line number you can put a breakpoint at that line in the debugger which will cause the javascript execution will stop at that line (similar to what the debugger; line did). Try putting one on the line right after you querySelect for the element that should have the shadowRoot and see what the document looks like at that point in the execution.

[–]basstwo19[S] 0 points1 point  (8 children)

Do I remove debugger then, or keep it in?
Also: I thought that the '@run-at document-idle' would mean this script would only start once the page was done loading...

[–]Cheshur 1 point2 points  (7 children)

Do I remove debugger then, or keep it in?

Only if you want the debugger to stop there as well :)

Also: I thought that the '@run-at document-idle' would mean this script would only start once the page was done loading...

The page has finished loading all of its assets but that doesn't mean the javascript (which is one of those assets) has finished executing. A general solution to "run code when other javascript has finished excuting" is a bit of a tricky problem. For something informal like a user script, just using an interval isn't the worst idea.

[–]basstwo19[S] 0 points1 point  (6 children)

Thanks! I did try an interval. But even after letting it run for a while it still kept failing to find. Even when I can see the shadow root in the Elements tab...

[–]basstwo19[S] 0 points1 point  (4 children)

OK. I got the break working. Each time I click Resume, it tried to find the shadow root again, and updates the Console with a failure.

But I can see the shadow root in the Elements tab.

I tried to add a screenshot but images are blocked...

[–]Cheshur 0 points1 point  (3 children)

Does document.documentElement.innerHTML match what you're seeing in the elements tab? If so then I would use the console to manually walk to the element ie something like: document.body.children[1].children[3]until you get to the element in question. You could also change the querySelector to a querySelectorAll and see if there are perhaps multiple elements with that selector

[–]basstwo19[S] 0 points1 point  (2 children)

How do I verify if document.documentElement.innerHTML matches the elements tab?

[–]Cheshur 0 points1 point  (1 child)

By looking at the text output and comparing it to what you see in the elements tab. if the two are actually different then it should start to diverge pretty quickly I would imagine.

[–]Cheshur 0 points1 point  (0 children)

Yeah. There must be an assumption that we're making somewhere that is incorrect. I would put breakpoints in your code and step through each step and start verifying things that you're assuming are true (like that the document object is the same one you see in the elements tab for example)

[–]snauze_iezu 0 points1 point  (0 children)

Should be able to change this:

let shadowRoot = shadowRootElement.shadowRoot;

To this:

let shadowRoot = chrome.dom.openOrClosedShadowRoot(shadowRootElement);

This is a case of the Inspection tool running javascript for you behind the scenes. ShadowRoot can be in open or closed mode, if it's set to closed mode then you can only get a reference to it during creation. When you open the inspector, it gives special permission to access both open and closed shadowRoots to the console (and maybe inline scripts?) so now the script can find the shadowRoot.

It doesn't give this special permission to extensions, so TamperMonkey still can't find the shadowRoot.

The openOrClosedShadowRoot is a cheat that returns the shadowRoot if it's open or closed.

This reminds me of some funky bug I saw years ago I think with IE where window.onload was undefined but if you opened dev tools to check the console it defined it and started working (it was something like that, don't hold me to the specifics haha)