What I still don't understand, though, is why the following code works
in my first extension:

postMsg.js:

var subject = document.getElementById("subject");
var body = document.getElementById("body");

chrome.extension.sendRequest({name: "domain"},
     function(response)
     {
     subject.setAttribute("value", response.domain + ": ");
     });

chrome.extension.sendRequest({name: "url"},
     function(response)
     {
     body.defaultValue = "URL of last page visited: " + response.url;
     });

The above code inserts some text into the subject line and message
body of a Google Groups post page. It works all the time, so there
doesn't seem to be a race condition of any kind, yet the same code
fails in my present app. Maybe there's an implicit wait in the
asynchronous processing for the response before the content script
thread terminates.

On Dec 30, 11:53 pm, PAEz <[email protected]> wrote:
> The whole asynchronous way of doing things did my head in to start
> with.
> You know you can send a bunch of variables in one request so you dont
> have to keep playing ping pong.
> Heres an example of what you can do....
> //content
> chrome.extension.sendRequest({request: "hostDetails"},
>   function(response)
>     {
> var server = "https://"; + response.hostname + ":" + response.port;
> //do the rest of your script here
>     });
>
> //background
> //Wait for request for the host name, port, e-mail address and
> password from content script
> chrome.extension.onRequest.addListener(
>   function(request, sender, sendResponse)
>   {
>   if (request.request == "hostDetails")
>     {
> sendResponse({"hostname": localStorage["hostname"], "port":
> localStorage["port"], "passwd": localStorage["passwd"], "email_addr":
> localStorage["email_addr"]});
>     } else sendResponse({}); // snub them.
>   }
>   );
>
> ....didnt test it, but it should work
>
> On Dec 31, 4:04 pm, PhistucK <[email protected]> wrote:
>
>
>
> > Well, you can put there a set of IFs and load other functions that will
> > actually do whatever you want.
> > There is no nice way of doing it, sorry.
>
> > ☆PhistucK
>
> > On Wed, Dec 30, 2009 at 19:37, FractalBob <[email protected]> wrote:
>
> > > Thanks for your suggestion, PhistucK. It helps but it's not quite
> > > there. The problem is that I need to handle several responses, so in
> > > GetResponseAndAct() I need to determine which response I'm receiving,
> > > store it in a variable and move the server declaration outside the
> > > function. How do I interrogate GetResponseAndAct for the response?
>
> > > On Dec 30, 4:04 am, PhistucK <[email protected]> wrote:
> > > > Yes, everything is asynchronous.
>
> > > > You have sent a request. The response will not be received immediately.
> > > > You put a "return" in the function you have assigned to the callback, it
> > > > returns this value to... nothing.
>
> > > > What you should do, is something like that -
> > > > function GetResponseAndAct(Response)
> > > > {
> > > >  var server = "https://"; + Response.hostname + ":" + Response.port;}
>
> > > > function getHostParm(parm)
> > > > {
> > > > chrome.extension.sendRequest({name: parm}, GetResponseAndAct)}
>
> > > > getHostParm("hostname");
>
> > > > ☆PhistucK
>
> > > > On Wed, Dec 30, 2009 at 10:27, FractalBob <[email protected]> wrote:
> > > > > Hi,
>
> > > > > I thought I had this problem licked in my last extension. And I did.
> > > > > But now it's happening again and I don't see how this context is any
> > > > > different from the last.
>
> > > > > The following code, in the content script, is sending a couple of
> > > > > requests to background.html for data in local storage:
>
> > > > > background.html waits for requests:
>
> > > > > //Wait for request for the host name, port, e-mail address and
> > > > > password from content script
> > > > > chrome.extension.onRequest.addListener(
> > > > >  function(request, sender, sendResponse)
> > > > >  {
> > > > >  if (request.name == "hostname")
> > > > >    {
> > > > >    sendResponse({hostname: localStorage["hostname"]});
> > > > >    }
> > > > >  if (request.name == "port")
> > > > >    {
> > > > >    sendResponse({port: localStorage["port"]});
> > > > >    }
> > > > >  if (request.name == "passwd")
> > > > >    {
> > > > >    sendResponse({passwd: localStorage["passwd"]});
> > > > >    }
> > > > >  if (request.name == "email_addr")
> > > > >    {
> > > > >    sendResponse({email_addr: localStorage["email_addr"]});
> > > > >    }
> > > > >  }
> > > > >  );
>
> > > > > However, when I ran the debugger, it seemed that getHostParm()
> > > > > completed before the listener could respond. In any case, getHostParm
> > > > > () returns "undefined". I know the requested data is on disk, because
> > > > > it's part of my options processing and I know that works. Anyone have
> > > > > any ideas?
>
> > > > > --
>
> > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > "Chromium-extensions" group.
> > > > > To post to this group, send email to
> > > [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]<chromium-extensions%2Bunsu
> > > > >  [email protected]><chromium-extensions%2Bunsu
> > > [email protected]>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/chromium-extensions?hl=en.
>
> > > --
>
> > > You received this message because you are subscribed to the Google Groups
> > > "Chromium-extensions" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<chromium-extensions%2Bunsu
> > >  [email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/chromium-extensions?hl=en.

--

You received this message because you are subscribed to the Google Groups 
"Chromium-extensions" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/chromium-extensions?hl=en.


Reply via email to