Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension global variable

I've looked at other global variable questions on Chrome Extension with manifest ver 2, but found nothing.

Assume I have 2 files:

// content.js

var myVariable = myVariable(someDiv);
var myVarWithGlobe = VarWithGlobe.fromVariable(myVariable);

and

// VarWithGlobe.js

var withGlobe = withGlobe || { };

withGlobe.WithGlobe = (function(global) {
    var myLocalVar = global.myVariable;
....

WithGlobe.fromVariable = WithGlobe;

both of them are added to web_accessible_resources, content_scripts but I can't access global.myVariable in second file since it is undefined.

How can I get it if I'm not allowed to change VarWithGlobe.js?

like image 458
user2786037 Avatar asked Oct 23 '25 19:10

user2786037


1 Answers

Content scripts have own execution context (it is even different from the webpage context they are executing in). The only way is to pass messages to/from background page with variable. Check this answer for code samples

like image 196
Andrey Avatar answered Oct 26 '25 08:10

Andrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!