Hi. I am trying to create my first extension for personal use. It is quite simple (in theory at least), but I am having a problem with message passing.
In short, the goal is to create an extension that will display the meta tags of the currently loaded page, in a popup. Here is my code so far.
popup.html
background.htmlCode:<html> <head> <script src="js/jquery.js"></script> <script type="text/javascript"> function init() { var tabinfo = chrome.extension.getBackgroundPage().getData(); $("body").append( "<div id='ad_seo_inner'><h1>Details for " + tabinfo[0].details + "</h1>" + tabinfo[0].details + "</div>" ); } </script> </head> <body onload="init()"> <div id='about'>META SEO inspector</div> </body> </html>
content.jsCode:<html> <head> <script> var tabinfo = {}; tabinfo[0] = {"details": "tester"}; function getData() { chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { console.log(response.farewell); tabinfo[0] = {"details": response.farewell}; }); }); return tabinfo; } </script> </head> <body> </body> </html>
The popup itself is working, but i would have expected the getData function to overwrite the value of tabinfo[0] with the response from the content.js file (i.e. "goodbye". However, the originally declared tabinfo[0] is being displayed.Code:chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension"); if (request.greeting == "hello") sendResponse({farewell: "goodbye"}); else sendResponse({}); // snub them. });
Can anyone point me in the right direction?


LinkBack URL
About LinkBacks



Reply With Quote