Chrome Extension Popup - Setting DIV text from background.js function -


thank reading.

i stuck on passing information between background.js , popup.html!

in example, trying call testrequest in background.js popup.html. have looked @ dozens of examples of how , coded many solutions, have not been successful...this 1 of solutions tried:

popup.html:

<html lang="en"> <head>     <meta charset="utf-8" />     <title></title> </head> <body>     <script type="text/javascript"  src="background.js">                             $(document).ready(function () {                 document.getelementbyid("test").textcontent = chrome.extension.getbackgroundpage.testrequest();             });     </script>     <div id="test"></div> </body> </html> 

background.js

var testrequest = function(){ return 'yay!'; } 

your appreciated!

~brian

you use simple message passing this. example:

popup.html

<html lang="en">   <head>     <meta charset="utf-8" />     <title></title>     <script src="jquery.min.js"></script>     <script src="popup.js"></script>   </head>   <body>     <div id="test"></div>   </body> </html> 

popup.js

$(function() {   chrome.runtime.sendmessage({method:"testrequest"},function(reply){     $('#test').text(reply);   }); }); 

background.js

chrome.runtime.onmessage.addlistener(function(message,sender,sendrepsonse){   if(message.method == "testrequest")     sendresponse(testrequest()); }); function testrequest(){   return "yay!"; } 

inline code not allowed in popup.html move external file, using jquery don't afraid use more, , if using it, don't forget include it.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -