javascript - message passing from web page to contentscript.js in chrome extension -


this question has answer here:

want send status value contentscript.js in chrome extension page in browser, doing adding value newly created event passing extension, but receiving value 'undefined'. code follows.

page.html

<html> <head></head> <body> <script>     var go = function() {     var sam=1;         var event = document.createevent('event',{"details": 1});         event.foo=sam;         event.initevent('hello');         document.dispatchevent(event);     } </script> <a href="javascript:go();">click me</a> </body> </html> 

trying access value of 'foo' , 'status'(either of ), getting both values 'undefined'.

contentscript.js

document.addeventlistener("hello", function(data) {     alert("test:foo "+data.foo+":"+data.status); }) 

please let me know how access value.

 based on documentation from: [https://developer.chrome.com/extensions/messaging#external-webpage][1]      - manifest.json      "externally_connectable": {       "matches": ["*://<your extension id>/*/page.html"]     }      page.html:     // id of extension want talk to.     var editorextensionid = "abcdefghijklmnoabcdefhijklmnoabc";      // make simple request:     chrome.runtime.sendmessage(editorextensionid, {"details": 1},       function(response) {         if (!response.success)           handleerror(url);       });      contentscript.js      chrome.runtime.onmessageexternal.addlistener(       function(request, sender, sendresponse) {         if (sender.url === blacklistedwebsite)           return;  // don't allow web page access         if (request.details)           //process message       }); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -