javascript - How to get access to a background script variable from content script -


i'm creating extension own use, have problem. want assign value variable background.js content.js. variable background.js must exist despite refreshing of content page. how this?

manifest.json

{    "manifest_version": 2,   "name": "slownik",   "version": "1.0",    "description": "slownik",    "background": {   "scripts": ["background.js"]   },    "content_scripts": [     {       "matches": ["*://*.sjp.pl/*"],       "js": ["content.js"]     }   ] } 

background.js:

var test = "test"; 

content.js:

test = "testa"; 

exactly desire not possible. background script(s) , content scripts execute in different contexts and, in cases, different processes. not possible directly share variable between 2 contexts. however, can share information.

.storage.local exists able store information within extension in way accessible of scripts.1 data stored in .storage.local persists through browser being restarted. can set value, using .storage.local.set(), within background.js , value, using .storage.local.get() within content.js.

there examples of using .storage.local on mdn pages linked above. there number of stack overflow questions/answers provide examples.


1. except scripts insert page context. these not content scripts, insert them using content scripts. use gain access variables , functions exist in page scripts run on webpage.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -