I'm trying to share data between the browser_action and the content_scripts.
Currently, I'm having an issue where I CAN access the data in the browser_action page (it returns the proper values once set and saves across sessions).
HOWEVER, I can't seem to access the data in the content_script, even after being saved in the browser_action.js. The console.log always return "undefined" for the value.
The expected workflow would be:
content_script:
if syncData === undefined {
/* Do default stuff */
} else if syncData {
/* Do stuff if set to true */
} else {
/* Do stuff if set to false */
}
browser_action.js:
/* Allow user to set syncData so that it saves across page refreshes and is accessible by content_script */
manifest.json
{
"manifest_version": 2,
"name": "Name",
"description": "Description",
"version": "1.0",
"content_scripts": [
{
"js": ["content_script.js"]
}
],
"browser_action": {
"default_icon": "icon128.png",
"default_popup": "browser_action.html"
},
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"permissions": [
"storage"
]
}
content_script.js
chrome.storage.sync.get('descriptionEnabled', function (data) {
console.log(data.descriptionEnabled)
});
browser_action.js
/*jslint browser: true*/
/*global window, document, alert, chrome */
window.onload = function () {
"use strict";
chrome.storage.sync.get('descriptionEnabled', function (data) {
if (data.descriptionEnabled === true) {
document.getElementById("settingsId").value = "on";
} else if (data.descriptionEnabled === false) {
document.getElementById("settingsId").value = "off";
}
});
document.getElementById("settingsId").onchange = function () {
if (document.getElementById("settingsId").value === "on") {
chrome.storage.sync.set({'descriptionEnabled': true});
} else {
chrome.storage.sync.set({'descriptionEnabled': false});
}
};
};
browser_action.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript" src="browser_action.js"></script>
<title>Description Background Page</title>
</head>
<body>
<div>Display Saved Notifications?</div>
<div>
<select id="settingsId">
<option value="on">Yes</option>
<option value="off">No</option>
</select>
</div>
</body>
</html>