thread for rank beginners - readyState never reaches 4

156 views
Skip to first unread message

jeromekjerome

unread,
Jun 12, 2011, 8:05:57 AM6/12/11
to Chromium-extensions
Sorry for the level of ignorance reflected in this post but got to
start somewhere. I am writing my first extension and can't seem to
get to first base. I've written in C++ and C# before but this is my
first dive into java - it's not so different.

Here is the manifest and the popup.html. readyState never gets past 1
in the code below. Here I am presuming the code will load a DOM from
the feedUrl so I can examine it. But nothing ever comes back. The
alerts all fire correctly. If I comment out the remaining while loop,
the thing just fires the alerts. It has to be a stupid error. Can
someone spot it?

POPUP.HTML

<html>
<script>
//global variables
var feedUrl="http://dating.ru/"; //target url
var req; //xmlHttp request

function handleResponse() {
var doc = req.responseXML;
if (!doc) {
alert("Error in handleResponse!");
return;
}
}

function displayDating()
{
alert("Going Dating!");
feedUrl="http://dating.ru/";
main();
}

function displayChpoking()
{
alert("Going Chpoking!");
feedUrl="http://chpoking.ru/";
main();
}

// The XMLHttpRequest object that tries to load and parse the feed.

function main() {
alert("main function!");
req = new XMLHttpRequest();
if (req == null){
alert("Unable to create request");
};
req.onreadystatechange =
alert(req.readyState);
req.open("GET", feedUrl, true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
alert(req.responseText);
else
alert("Error loading page");
}
};
req.send();
// while (req.readyState!=1) alert(req.readyState);
// while (req.readyState!=2) alert(req.readyState);
// while (req.readyState!=3) alert(req.readyState);
while (req.readyState!=4) alert(req.readyState);


req.onload = handleResponse;
alert(req.status);
if(req.status == 200) dump(req.responseText);
req.onerror = alert("Error on XMLHttpRequest!");
req.send(null);
// var profiles=document.getElementByTagName("photo_num")();
// alert(profiles.length);
alert("end main function!");
}
</script>

<body>
<div id="body">
<div id="title">
<h3>Dating</h3>

<form action="">
<button type="button" onclick="displayDating()">Dating</button>
<button type="button" onclick="displayChpoking()">Chpoking</button>
</form>
</div>
</body>
<script>


MANIFEST.JSON

{
"name": "Dating",
"version": "1.0",
"description": "Engine for Dating Search function, auto-initiate and
four levels of auto-response. Alice plug-in.",
"permissions": [
"http://chpoking.ru/*",
"http://dating.ru/*",
"tabs"
],
// "background_page": "background.html",

"browser_action": {
"default_icon": "pen.gif",
"default_title": "Dating",
"default_popup": "popup.html"
}
}

Boris Smus

unread,
Jun 13, 2011, 12:41:28 PM6/13/11
to jeromekjerome, Chromium-extensions
This is a group for chrome extensions and not general JavaScript questions. 

One of the issues with what you're doing might be this call: while (req.readyState!=4) alert(req.readyState);. XHR is asynchronous and you shouldn't be doing blocking while statements like the one above.

In general, I would take a look through https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest and other documentation, and write a reduced XHR sample to get your feet wet.

- Boris


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


PhistucK

unread,
Jun 13, 2011, 1:08:07 PM6/13/11
to jeromekjerome, Chromium-extensions
Besides what Boris wrote, also remove the asterisk from the host permissions ("http://chpoking.ru/*"-->"http://chpoking.ru/").
I am not sure whether the trailing slash is needed, I think it is. Though if it is not needed, it could also become an error when trying to access that origin.

PhistucK



jeromekjerome

unread,
Jun 14, 2011, 6:42:38 AM6/14/11
to Chromium-extensions
Thanks Boris.

I have read and re-read the documentation at
https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest
and it is simply not detailed enough for me to plumb and answer from
it. Secondly, my post is about Chrome Extensions, albeit a rather
simple question.

I was able to get that particular code to work if I change it to
synchronous but I understand that this is not a desirable way to code
it. As far as the while loop is concerned, I have already discarded
it and it works thusly:

req = new XMLHttpRequest();
if (req == null){
alert("Unable to create request");
};
req.open("GET", feedUrl, false);
req.send();
req.onLoad = handleResponse();

If to code this as an asynchonous get, how do I know when it finishes
loading? onLoad just returns that same values forever as far as I can
see. Do I need a listener or do I need to fire some other event so
that onLoad updates its status? I think I remember a C++ example,
where it was necessary to put it into a loop with a "message-aware
delay" until the request came back ready.

On Jun 13, 8:41 pm, Boris Smus <s...@chromium.org> wrote:
> This is a group for chrome extensions and not general JavaScript questions.
>
> One of the issues with what you're doing might be this call: while
> (req.readyState!=4) alert(req.readyState);. XHR is asynchronous and you
> shouldn't be doing blocking while statements like the one above.
>
> In general, I would take a look throughhttps://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequestand
> other documentation, and write a reduced XHR sample to get your feet wet.
>
> - Boris
>
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

Ben

unread,
Jun 14, 2011, 10:04:31 AM6/14/11
to jeromekjerome, Chromium-extensions
On Tue, 2011-06-14 at 03:42 -0700, jeromekjerome wrote:
> req.onLoad = handleResponse();
>
> If to code this as an asynchonous get, how do I know when it finishes
> loading? onLoad just returns that same values forever as far as I can
> see. Do I need a listener or do I need to fire some other event so
> that onLoad updates its status? I think I remember a C++ example,
> where it was necessary to put it into a loop with a "message-aware
> delay" until the request came back ready.

This looks wrong... is it a typo, or is that what you actually have in
your code? Seems like it should be req.onLoad = handleResponse;

Boris Smus

unread,
Jun 14, 2011, 12:48:13 PM6/14/11
to jeromekjerome, Chromium-extensions
Try reading https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Example.3a_Asynchronous_request. It gives a complete sample of an asynchronous XHR.

- Boris

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Ben

unread,
Jun 14, 2011, 1:08:47 PM6/14/11
to jeromekjerome, Chromium-extensions
The onError alert is called every time because you are calling it every
time; you need to place it in an anonymous function, like so:

req.onError = function () { alert("Error on XMLHttpRequest!") }

Similarly, handleResponse is being called every time because you are
calling it every time. It should be referenced without the parentheses:

req.onLoad = handleResponse;

I don't know if that will fix all of your problems, though.

Oh, and you dropped the list from CC; be sure to hit reply-all if you
mean to send the message to the list. ;-)


> The following code works but the last line always triggers the onError
> alert. If I use this code in the default popup from a browser action,
> run it under "inspect popup" monitoring req.status and req.readystate,
> these values go to 200 and 4 right away if running synchronously but
> never get past 1 if running asynchronously.
>
> handleResponse is a function and when the code executes as below,
> handleResponse gets called.


>
> req = new XMLHttpRequest();
> if (req == null){
> alert("Unable to create request");
> };
> req.open("GET", feedUrl, false);
> req.send();
> req.onLoad = handleResponse();

> req.onError = alert("Error on XMLHttpRequest!");


>
>
> On Tue, Jun 14, 2011 at 6:04 PM, Ben <benj...@gmail.com> wrote:
> On Tue, 2011-06-14 at 03:42 -0700, jeromekjerome wrote:

> > req.onLoad = handleResponse();
> >
> > If to code this as an asynchonous get, how do I know when it
> finishes
> > loading? onLoad just returns that same values forever as
> far as I can
> > see. Do I need a listener or do I need to fire some other
> event so
> > that onLoad updates its status? I think I remember a C++
> example,
> > where it was necessary to put it into a loop with a
> "message-aware
> > delay" until the request came back ready.
>
>

Jerome

unread,
Jun 14, 2011, 1:16:50 PM6/14/11
to Ben, Chromium-extensions
Thanks a lot.  I'm sure it won't cure all the defects in my coding.  But it points out that I need to research anonymous functions.
--
s/ Jerome W. Dewald

What you do speaks so loud that I cannot hear what you say.
           -- Ralph Waldo Emerson

Jerome

unread,
Jun 14, 2011, 1:29:02 PM6/14/11
to Ben, Chromium-extensions
I see that my ignorance of anonymous functions stems from my c++ background and virtually no experience in javascripting.
Reply all
Reply to author
Forward
0 new messages