Monday, May 13, 2024
57
rated 0 times [  60] [ 3]  / answers: 1 / hits: 31195  / 15 Years ago, sat, december 26, 2009, 12:00:00

I'm developing an extension for Google Chrome browser.
I could not figure out how to access the current tab DOM object from the popup.html page.
any suggestions?


More From » google-chrome

 Answers
23

By default, within popup.js/popup.html, the document object refers to the extension's popup window's document only. To get the DOM for a specific tab (for instance the currently active tab), you would need to use content scripts communications. For example we need to send a request from the extension to your content script via popup, so in the popup.html you do something like this:



chrome.tabs.getSelected(null, function(tab) {
// Send a request to the content script.
chrome.tabs.sendRequest(tab.id, {action: getDOM}, function(response) {
console.log(response.dom);
});
});


Now in the content script, we need to listen for those events coming from the extension, so in some file we named dom.js



chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.action == getDOM)
sendResponse({dom: The dom that you want to get});
else
sendResponse({}); // Send nothing..
});


Now remember to setup your manifest to include the content script and tab permission.


[#97981] Tuesday, December 22, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luzv

Total Points: 178
Total Questions: 105
Total Answers: 114

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
luzv questions
Thu, Aug 26, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Wed, Apr 29, 20, 00:00, 4 Years ago
;