index.js unable to access body of index.html

75 views
Skip to first unread message

Vince Scafaria

unread,
Feb 16, 2023, 9:49:15 AM2/16/23
to Chromium Extensions
If I add this line at the end of index.js
console.log(document.body);
starting from this example
document.body is null

I'm trying to enhance the index.html page to include buttons that run javascript on-click to query on the data in the sqlite db etc. When I try to access elements on the page (not just the button, but even 'body'), those elements are inaccessible. Looking at the rest of index.js, which does do dom manipulation, I feel like I'm missing something basic. Thanks for any help!

Oliver Dunk

unread,
Feb 16, 2023, 9:56:04 AM2/16/23
to Vince Scafaria, Chromium Extensions
Could you try moving the script tag to the body of the HTML, or waiting to run your code until the DOMContentLoaded (https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event) event fires?

Scripts execute synchronously as the page loads, and your console.log is likely running before the browser has got down to the body tag and added that to the document :)
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/9e5a68a2-936f-48ba-b881-75b485a1e491n%40chromium.org.

Vince Scafaria

unread,
Feb 16, 2023, 2:21:28 PM2/16/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Vince Scafaria
Now from index.js I call a worker and I'm trying to have that worker access the chrome.storage.local data that the chrome extension has been populating. (I'm launching this index.html file to transfer data from local storage to SQLite.) However, I get the message in the worker that 'chrome is not defined' when I try the below (this code is in the worker.js called from index.js). Thoughts? Thanks!

const xferCacheToDb = function() {
  chrome.storage.local.get(all => {
    // .. do stuff
  });

Vince Scafaria

unread,
Feb 16, 2023, 7:34:19 PM2/16/23
to Chromium Extensions, Vince Scafaria, Oliver Dunk, Chromium Extensions
Nevermind, I believe I can solve my problem by accessing storage from index.js and passing that data to worker.js.
Reply all
Reply to author
Forward
0 new messages