Communicate between content script and options page
- by Gaurang Tandon
I have seen many questions already and all are about background page to content script.
Summary
My extension has an options page, and a content script. The content script handles the storage functionality (chrome.storage manipulation).
Whenever, a user changes a setting in the options page, I want to send a message to the content script to store the new data.
My code:
options.js
var data = "abcd"; // let data
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, "storeData:" + data, function(response){
console.log(response); // gives undefined :(
});
});
content script js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// not working
});
My question:
Why isn't the approach not working?
Is there any other (better) approach for this procedure.