user1943804
user1943804

Reputation: 31

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.

Upvotes: 3

Views: 2688

Answers (2)

Sudarshan
Sudarshan

Reputation: 18534

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

References:

Upvotes: 1

epoch
epoch

Reputation: 16605

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.

Upvotes: 4

Related Questions