in manifest.json
add storage permission
"permissions": ["storage"]
in contentscript.js
save your data in the local storage Google Chrome : Chrome Storage API
var data = ''; //set your data here
chrome.storage.local.set({
'myVariable': data
});
use sendMessage to call the background page : Chrome messaging API
chrome.runtime.sendMessage({
greeting: "myAction"
});
in background.js
get the message from contentscript.js
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.greeting == "myAction") {
collectData();
}
});
define the collectData() function
function collectData() {
chrome.storage.local.get('myVariable', function (items) {
console.log(items); //your data is on items.myVariable
});
}
to call popup.js function from background.js use the messaging API