Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get background variable value in content scripts

I have a var in background page (for example var x = 23). How I can get this variable in my content script? I tried this in content.js:

chrome.extension.getBackgroundPage().x;

But it doesn't work.

like image 428
user1943804 Avatar asked Jun 11 '26 23:06

user1943804


2 Answers

Send your data with messages, and listen on the receiving side.

Messages

Since content scripts run in the context of a web page and not the extension, they often need some way of communicating with the rest of the extension. For example, an RSS reader extension might use content scripts to detect the presence of an RSS feed on a page, then notify the background page in order to display a page action icon for that page.

like image 151
epoch Avatar answered Jun 15 '26 11:06

epoch


You can not use chrome.extension.getBackgroundPage() in content scripts, it is not supported, as an alternative use epoch answer for message communication.

References:

  • Support for Content Scripts
like image 20
Sudarshan Avatar answered Jun 15 '26 11:06

Sudarshan