Get tab's DOM using content script - google-chrome-extension

I have a script in my popup.html:
</body>
<script src="popup.js"></script>
</html>
Through which I am trying to obtain the DOM of a particular tab. More specifically, I am attempting to determine the existence of a particular element on the current page in a specific tab and then use this information within popup.js. How would I go about doing this?

You can't access current page DOM from a popup. You need a content script for that. Start by setting up these three scripts:
popup script
background page script
content script
With these you can send messages between popup script and content script using background script as a proxy. Read about message passing here.
You can simplify the process a bit using executeScript instead a content script. You will still need a communication between popup script and a background page though.

Related

Chrome Extension: access separate html documents from contentscript

I have a Chrome extension that loads/injects a contentscript.js This script appends html and css to the webpage.
Currently I have the html and css written into my contentscript. What I would like is for the css itself, as well as the body of my new elements to be in separate document, mostly so it looks better than having html and css as text in a .js document.
Then in the contentscript I would do something like
node = the_html_doc.html
document.getElementsByTagName("body")[0].appendChild(node);
But how do I access such a separate document from my contentscript? It needs to be available in all tabs (I use the activeTab permission), not just the url in the manifest "matches".
A better solution would be to use a background script. What I did was to make a file called background.html that would store nothing but templates. I then had my background script (background.js) setup to communicate with my content script (content.js). The content script would send a message to the background script with a command indicating it wants a template. Leveraging jQuery, i can easily select and return a template to my content script which can then be injected into the page.
Here is the code (bits an pieces):
background.html
<div id="template-1"></div>
<div id="template-2"></div>
...
background.js
chrome.runtime.onMessage.addListener(function(cmd, sender, sendResponse){
c = JSON.parse(cmd);
if(c.cmd == "GET_TEMPLATE"){
//respond with the template referenced by c.selector
sendResponse($(c.selector).outerHTML);
}
});
content.js
var command = {cmd:"GET_TEMPLATE", selector:"#template-1"};
chrome.runtime.sendMessage(JSON.stringify(command), function(response) {
//and here you should get your template
console.log(response);
//you can start using jQuery like $(response) to alter it
});
This method has worked flawlessly for me. I not only use commands here but I use them everywhere now, it works well with message passing.
You might be able to use the web_accessible_resources manifest setting, then in your content script you can just inject a link element that points to the chrome.extension.getURL(<filename>) value for the CSS, and inject a script element of type text/html with an id, and then fetch the contents of that node and use those for your appendChild call.

ExecuteScript in a random tab that is not part of the chrome extension send result back to background javascript

I would like to check the html of an element of a
chrome.tabs.executeScript(tab_id,{code: 'sendRequestToBackground(document.getElementById('important_val').innerHTML); '},
function(){
});
I want to be able to get this value from my background script, I saw a lot of examples with chrome.extension.sendRequest and chrome.extension.onRequest.addListener because all the examples is for working from the extension popup.html to background script and vice-versa.
But I open a brand new tab, change the URL, and I want to get a value of a field that (which btw is generated via Javascript) belongs to this random tab.
Is it possible to do that?
Thanks!
If you want to inject your code to a random tab, do this:
Be sure to have permissions to "tabs" and "<all_urls>" in your manifest.json
Get all tabs using chrome.tabs.query
Pick random tab using Math.random()
Inject the code using chrome.tabs.executeScript
In the incjected code call chrome.extension.sendRequest
On the background page receive the message using chrome.extension.onRequest

chrome content script to access and modify window

I am writing a chrome extension that is a 'content script'
I want to inject a google map on to a webpage.
Problem:
It appears that i have no way to add functions on to the window object, thus i cannot define a callback function for googlemaps to call when it loads.
How do people usually go about mucking with the window?
--
someone on the interwebs suggested i do this:
You can do this easily with a JavaScript URL: window.location =
"javascript:obj.funcvar = function() {}; void(0);"
but when i did this i got an access denied error. it seems like a lot of search results about this problem are outdated.
Content scripts have a separate JavaScript execution ennvironment from the page they run on, so they cannot alter JS variables in the page itself. However, the content script shares the DOM with the page, so you can inject a <script> tag into the DOM which will be loaded and run in the actual page's execution environment.

Returning an element value from chrome.tabs to a background script

I would like to send a value from the tab html content to my chrome extension background script.
Right now I'm using a workaround, that is
chrome.tabs.executeScript() to send a code to retrieve data and then send it to my web server via Ajax, and then my background script checks to see if its there in my server. But that's obviously not the right way.
Is there a way to get element values and then send it back to the background script? so I can keep working with the value?
Thanks!
chrome.extension.sendRequest() has a counterpart chrome.extension.onRequest.addListener()
If it's not obvious you can read all about message passing here:
http://code.google.com/chrome/extensions/messaging.html

Tracking Chrome extension events in Analytics

Google Chrome Extension documentation has some good information here:
http://code.google.com/chrome/extensions/tut_analytics.html
I put the analytics tracking code in my background.html file.
However I tried putting the _gaq.push call inside a script that runs on the page and got an error saying that the variable _gaq is not defined.
So I have to put onclick events in every element on the page I want to track and from there, call a function in background.html? Is there a better way to track events?
You can do this with chrome.extension.sendRequest in the content script and chrome.extension.onRequest.addListener in the background.html page. See here for more details: http://code.google.com/chrome/extensions/messaging.html. Also be sure your manifest.json is correct; I ran into problems when I had a hyphen character instead of an underscore.

Resources