Chrome extension: sharing an object between script s content and script background

I am developing a chrome extension that uses jQuery / Zepto in content script. Now the extension is designed to run on every website, which means that a custom instance of jQuery / Zepto opens on each tab.

Is there a way to share a jQuery / Zepto object between different content scripts?

I know that content scripts can communicate with the background of the script. I was hoping the background script would gain access to the jQuery object and return a link to it for each content script. But I realized that only JSON messages can be transmitted between content and background scripts.

Is there any way to achieve what I want?

+8
google-chrome-extension
source share
2 answers

Content scripts on different tabs also do not have access to JavaScript objects.

Chrome supports communication between content scripts and / or the background page through chrome.runtime.sendMessage + .onMessage . Since all messages are JSON-serialized, the JavaScript object cannot "filter" into other contexts in this way.

So: No, you cannot exchange objects, such as jQuery, with (content scripts) in other tabs.

+9
source share

Execution conditions Content scripts ensure that content scripts can communicate with each other

Example:

 "content_scripts": [ { "matches": ["<all_urls>"], "js": ["myscript.js","myscript1.js"] } ] } 

A An individual DOM environment into which content scripts are inserted ["myscript.js","myscript1.js"] provides myscript1.js access to all the contents (functions, variables) of myscript.js , but this stops the communication of the Individual DOM Environment .

Having said that "Constraint \ Requirement" you see in the Scripts the content that requires the requirement when Does it require background pages to access the DOM for subpages ?

Consult

+1
source share

All Articles