1

I am currently trying to create a chrome extension that alters a website (an interface for a ip camera that I bought, thats also why the site is a local site like 192.168.178.70 for example). I just want to add some buttons and stuff like that to add some functionality.

I used

document.body.onload

to wait until everything has loaded so I can grab a table or a div or whatever and create and add my button to it. But document.getElementById("ipcam_wrapper") does not seem to work properly, it returns null.

Well I tried to do the same thing in google chrome developer mode but it does also return null. I actually managed to get the element this way once but I am not able to recreate it because I did not do anything different.

There is only one element with that specific ID.

Only one element with that ID

Here i try to get the element:

Trying to get the element

So what am I doing wrong?

best regards

asked Aug 23, 2018 at 13:07
14
  • 1
    Do you have any iframe on the page? Also, can you create a minimal reproducible example? Commented Aug 23, 2018 at 13:09
  • 1
    @TPRammus If it is an iframe, that explains the issue. Can you check the source of the page (through right click, view page source) and see if there are any IFrame elements? Commented Aug 23, 2018 at 13:24
  • 1
    @TPRammus You might need to select the execution context in the developer tools while executing document.getElementById. See section "Selecting Execution Context" in this article: developers.google.com/web/tools/chrome-devtools/console Commented Aug 23, 2018 at 13:31
  • 1
    @TPRammus IIRC, when there is an iframe of the same domain on the page, the scripts of extension run once for each execution context. So in one of the execution context it might be failing, but in the other one it might be working. Can you check that? Frankly, I haven't worked with extensions as much to remember the exact details. Commented Aug 23, 2018 at 13:48
  • 1
    @TPRammus It would be great if you can post an answer explaining the issue and the solution, in case someone else stumbles on a similar issue. Commented Aug 24, 2018 at 11:30

2 Answers 2

3

Thanks to Nisarg Shah, I was able to resolve my issue. Thank you very much for your help!

The issue was that the element I was trying to access was inside an iframe. Also thank you RaYell for providing the necessary code, with which I was able to resolve my problem:

window.frames['mainframe'].document.getElementById('example')
answered Aug 24, 2018 at 11:48
Sign up to request clarification or add additional context in comments.

1 Comment

@nimeresam Well... maybe you do not have window.frames['mainframe']?
3

I think it's because the document itself won't be loaded yet.

I fixed it by using window.onload instead of document.body.onload, like:

window.addEventListener("load", ev => {
 // it should work
 document.getElementById("ipcam_wrapper")
});
answered Apr 21, 2020 at 12:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.