I’m developing a Chrome extension that simply scans the DOM for phrases.
The only thing I need help with is grabbing the DOM content with popup, I can’t find a way to return the contents of the current tab.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Tested and works correctly:
put
in your manifest.
Then, in your background.js
So essentially, we get the current tab, run some javascript on it that gets the innerHTML and passes it back to us. We interpret the response in a callback as appropriate.
Note this does very little error checking, so you might want to brush up on message passing and the tabs API.
Edit: I’ve tried this out and it does work as expected. To try it yourself easily, just create a new empty extension and give it the “tabs” permission and a background page. Then go inspect background.html from chrome://extensions and go to the console. Copy in the code below, setting the
chrome.tabs.getSelectednesting on a timeout of a few seconds. When you click enter to execute the JS, quickly click to some other tab. Wait for your timeout. Then go back to inspecting background.html. The HTML of the page you clicked to will have printed to the console.Another Edit: In general, accessing the current tab DOM as an event seems like it might be a bad model (which is probably why there isn’t a great native way to do it). Have you considered either using chrome.tabs.onUpdated or content scripts to universally do something on loading/changing the DOM?