user278064
user278064

Reputation: 10180

chrome extensions message-passing

I'm trying to develop (for the moment!) a simple chrome extension using the message exchanging API.

My contentscript ask the background page for its url, and wait the background answer. However, my contentscrpt never get the answer. Why? Thanks for your answer.

content_script.js


/**
 * Retrieve the url or the page currently visited.
 */
 chrome.extension.sendRequest({'action' : 'getUrl'}, function(response) {
    alert(response.url);
 });

background.html


...
                
   function onRequest(request, sender, callback) {  
      sendResponse({'url' : sender.tab.url});                       
   };

   chrome.extension.onRequest.addListener(onRequest);                     

Upvotes: 0

Views: 303

Answers (1)

Mihai Parparita
Mihai Parparita

Reputation: 4236

Your onRequest function has the final parameter named callback, but you call sendResponse in it. Assuming this is what your actual code looks like, you'll need to make the two names the same. If you inspect the background page in the developer tools, you should see a JavaScript exception about sendResponse being undefined.

Upvotes: 1

Related Questions