Chrome extension - messaging

I am trying to get the information that is set on the options page to change the behavior of my extension.

Basically, if the checkbox in OptionsPage is set to true, the extension is executed, otherwise, no. I return true on background.html for testing purposes, but nonetheless it does not work.

Will you guys help me? Thank!

Code entered on the page:

if(chrome.extension.sendRequest() == 'true')
    alert("checkbox set to true");
else
    alert("it is disabled");

background.html

<script>
chrome.extension.onRequest.addListener(function(){
    return true;
    }
</script>
+5
source share
1 answer

If you have a page and want to connect to the background page , you can simply do chrome.extension.getBackgroundPage ()

"",


options.html

var bkg = chrome.extension.getBackgroundPage()
bkg.startExtension();
bkg.stopExtension();

background.html

function startExtension() {
  console.log('Starting Extension');
}

function stopExtension() {
  console.log('Stopping Extension');
}

Script,


", ", - -? , script Message Passing. .

content_script.js

chrome.extension.sendRequest({action:'start'}, function(response) {
  console.log('Start action sent');  
});

background.html

function onRequest(request, sender, sendResponse) {
 if (request.action == 'start')
   startExtension()
 else if (request.action == 'stop')
   stopExtension()

 sendResponse({});
};
chrome.extension.onRequest.addListener(onRequest);

, .

+14

All Articles