Ushnish Basu
Ushnish Basu

Reputation: 41

Using jQuery in Chrome extension: load in background, access in popup

I am making a PageAction Chrome extension with a popup and a content script.

I want to use jQuery in the popup to manipulate its DOM for presenting data (which I am scraping from the website itself using jQuery injected as a content script). I know I can load jQuery directly in popup.html and use it. However, I wanted to see if I could load jQuery only once in the background page and access it in the popup, rather than loading it each time the popup is opened.

For this, I tried the following in the popup:

var background = chrome.extension.getBackgroundPage();
var $ = background.jQuery;
$("#testID").innerHTML = "testing jQuery";

but it didn't work - $("#testID") was undefined. I checked that jQuery was loaded in the background page, and using alternate names for $, e.g. jq, jQuery etc. didn't work either.

Any suggestions on how to make this work?

Upvotes: 4

Views: 2099

Answers (1)

serg
serg

Reputation: 111265

$("#testID", document).html("testing jQuery");

I don't think there is a way to permanently change default context, so you will need to pass document as a second parameter to all your selectors.

Upvotes: 3

Related Questions