Using jQuery in a Chrome extension: loading in the background, pop-up access

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

I want to use jQuery in a popup to manipulate its DOM to represent the data (which I slipped from the site itself using jQuery entered as the content of the script). I know that I can load jQuery directly in popup.html and use it. However, I wanted to see if I could only load jQuery once on the background image and access it in a popup, rather than loading it every time the popup opens.

To do this, I tried the following in a popup window:

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

but it didnโ€™t work - $("#testID") was undefined. I checked that jQuery is loaded on the wallpaper and uses alternative names for $, for example. jq, jQuery etc. also did not work.

Any suggestions on how to make this work?

+4
source share
1 answer
 $("#testID", document).html("testing jQuery"); 

I donโ€™t think there is a way to constantly change the default context, so you need to pass document as the second parameter to all your selectors.

+3
source

Source: https://habr.com/ru/post/1348191/


All Articles