I'm developing a "page action" google chrome extension. My manifest has:
... "background": { "scripts": ["background.js"] }, ...
In my background.js file I have:
function doSomething() { alert("I was clicked!"); } chrome.pageAction.onClicked.addListener(doSomething);
This works. Now in my doSomething
function I want to read some data on the current page. It will be a lot easier for me to use jquery to read the data so I can easily target the exact data I want. How can I incorporate jquery (preferrably served from google's CDN) so that it's accessible to my doSomething
function?
You can just put jquery. js into extension folder and include it in the manifest: { "name": "My extension", ... "content_scripts": [ { "matches": ["http://www.google.com/*"], "css": ["mystyles.
Background scripts are the place to put code that needs to maintain long-term state, or perform long-term operations, independently of the lifetime of any particular web pages or browser windows.
The background script ('background. js') is a JavaScript script that runs once our extension either gets installed or the user refreshes the extension manually.
The "background"
specification in manifest.json
should specify jquery.js
so that it is loaded before background.js
:
... "background": { "scripts": ["jquery.js","background.js"] }, ...
This should do the job.
Remember the js files are loaded in the order they are specified.
test if jquery is loaded.
in background.js
if (jQuery) { // jQuery loaded } else { // jQuery not loaded }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With