Communicate between content script and options page

Posted by Gaurang Tandon on Stack Overflow See other posts from Stack Overflow or by Gaurang Tandon
Published on 2014-06-07T15:10:03Z Indexed on 2014/06/07 15:24 UTC
Read the original article Hit count: 321

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:

  1. Why isn't the approach not working?
  2. Is there any other (better) approach for this procedure.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about google-chrome