views:

84

answers:

2

Hi All,

Have a small doubt in how message passing works in chrome using content scrips. I modified the default example (http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/messaging/timer/) for message passing given in the chromium documentation to the one that looks below :

popup.html

function testRequest() {

  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {counter: "getHTML"}, function handler(response) {
      alert("Inside Client = "+response.counter2);
    });
  });
}

and my content script looks like this :

page.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    alert(request.counter);
    alert("Inside server .. Req Counter = "+request.counter);
    sendResponse({counter2: "5"});
  });

When I execute the testRequest from popup.html, the content script is getting called as expected. I do get both the alerts i have declared with their respective values. But my popup.html response code doesnt seem to be called .. The alert I have inside the popup.html - alert("Inside Client = "+response.counter2); is not being executed.

On the other hand, If i have a debug point inside the client, its working ! Kinda strange.. Can somebody tell me how and why this is happening ?

Thank you in advance..

+1  A: 
Mohamed Mansour
oh.. ok.. great :) .. But my confusion was that when I start debugging, the alerts popped up.. ! Also, instead of popping up an alert of the response if I did the following :chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendRequest(tab.id, {counter: "getHTML"}, function handler(response) { responseCall(response.counter2); }); });function responseCall(txt){ alert(txt);}It worked ! But very inconsistent.. Just stops working sometimes.. But whenever I put the debug on, it works.. Anything wrong with what I am doing ?
paypalcomp
As I said, you cannot use alert in a popup or background page (use console). If it does work while debugging, that must be a bug that needs to be corrected.
Mohamed Mansour
oki.. Thank you so much for your answer Mohamed.. Really appreciate it :)
paypalcomp
Hi Paypal, sorry, but what I said was incorrect. I was fooled by what I saw when I attempted your example. Make sure you run the browser action on a real page like google.com, and it should work. You cannot run any scripts or send requests on the extension page (where you install, refresh your extension).
Mohamed Mansour
A: 

As far as I can tell, alerts from a popup will only appear if the popup is open.

You see the alert when you're debugging the popup because the debugger keeps the popup open.

I'm pretty sure there are also no problems with creating alerts from the background page.

Greg

related questions