how to pass current HTML page DOM to the popup.js from contentscript?

80 views
Skip to first unread message

Muhammed Waleed Hafeez

unread,
Jun 21, 2021, 1:21:48 PM6/21/21
to Chromium Extensions
I can get Html-DOM in contentScript.js file though chrome.API  but when I send the DOM to popup.js....The callback function in chrome.tabs.sendMessage() event handler returns me an "object" of DOM instead of an "actual DOM" in popup.js.

I have shared my screenshot of console.log() and code below. 
If I'm doing something wrong then please correct my mistake and guide me how do i achieve this task?

Expected Requirement:
i need to get current tab/webpage's HTML DOM in my popup.js through contentScript.js


htmlDOM.PNG

Alexei Miagkov

unread,
Jun 21, 2021, 1:33:17 PM6/21/21
to Muhammed Waleed Hafeez, Chromium Extensions
You can't send the DOM using extension message passing because extension message passing serializes everything you send and the DOM is not serializable. (Try running `JSON.parse(JSON.stringify(document))` on the page; you'll see the same incomplete object that you got in your popup.)

References:

https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage

sendResponse
Function to call (at most once) when you have a response. The argument should be any JSON-ifiable object.


 All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized.

--
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/c416a7fd-2619-45e7-a0b4-775cadb8361cn%40chromium.org.

Muhammed Waleed Hafeez

unread,
Jun 22, 2021, 2:20:35 AM6/22/21
to Chromium Extensions, Alex, Chromium Extensions, Muhammed Waleed Hafeez
Thanks Alex!

but is there any way to get DOM from contentscript.js to popup.js other than message passing? 
if not! then this means i have to get actual value of specific DOM-element in contentScript.js and send it to popup.js.     
right?

Erek Speed

unread,
Jun 22, 2021, 4:42:04 AM6/22/21
to Muhammed Waleed Hafeez, Chromium Extensions, Alex
Let's not fall into the trap of the XY Problem! What are you trying to accomplish? What functionality are you trying to provide to the users of your extension?

Alexei Miagkov

unread,
Jun 22, 2021, 1:33:13 PM6/22/21
to Erek Speed, Muhammed Waleed Hafeez, Chromium Extensions
Right, generally speaking, if you want something from the DOM, you have to do that work in the content script, the place you have access to the DOM.

Muhammed Waleed Hafeez

unread,
Jun 22, 2021, 3:10:21 PM6/22/21
to Chromium Extensions, Alex, Muhammed Waleed Hafeez, Chromium Extensions, Erek Speed
Thanks Alex, and Erek i know, i'm asking a wrong question but i just wanted to confirm it that if its possible to send DOM to popup.js or not. which Alex clearly told me. 
Reply all
Reply to author
Forward
0 new messages