Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Extensions: How to include jQuery in programmatically injected content script?

I'm injecting my content script from the background page when the user clicks the browser action button, like so:

chrome.browserAction.onClicked.addListener(function (tab) {     chrome.tabs.executeScript(null, { file: "content.js" }); }); 

So how can I access jQuery from inside my content.js? I don't see a way to inject that simultaneously.

like image 966
mpen Avatar asked Jan 15 '11 05:01

mpen


People also ask

What is inject JS in Chrome?

This extension allows you to write, save, and execute javascript into web pages. You can save scripts to be run globally (on any page), on a specific domain, on a specific page, or via a "URL contains" string.

What is content JS in Chrome extension?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.


2 Answers

What about:

chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {     chrome.tabs.executeScript(null, { file: "content.js" }); }); 

You can download "jquery.js" from here: http://jquery.com/download/

like image 135
serg Avatar answered Sep 18 '22 22:09

serg


How about adding this to your manifest file

"content_scripts": [     {         "js": [                 "jquery.js",                  "contentscript.js"             ]     } ], 
like image 41
Apoorv Saini Avatar answered Sep 20 '22 22:09

Apoorv Saini