javascript - Chrome Extension's Content Script catch custom events? -


if firing custom event page, can content script injected page catch event , process?

yes, content script can communicate injected web page. because context of content script , injected web page isolated each other, must communicate shared dom.

i think content script can't catch custom event fired injected page directly. can post message content script when specific custom event fired. window.postmessage can fit needs.

injected page:

<!doctype html> <html> <head>     <title></title> </head> <body>     <button id="btn">test</button>     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>     <script>         $("#btn").click(function(){             $(document).trigger("my_event");         });         $(document).on("my_event",function(){             window.postmessage({ type: "hello", text: "hello webpage!" }, "*");         });     </script> </body> </html> 

content script:

window.addeventlistener("message", function(event) {      if (event.source != window)     return;      if (event.data.type && (event.data.type == "hello")) {         alert("content script received: " + event.data.text);     } }, false); 

hope helpful you.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -