FF extension - getting xmlhttp.status==0

186 views
Skip to first unread message

Alon

unread,
Aug 20, 2011, 9:48:58 AM8/20/11
to mozilla-labs-jetpack
I'm writing an extension for FF and it is calling using "PageMod" to
the js file that contains:

function handleServerResponse() {

if (xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
//some code
}
else {
alert("Error during AJAX call. Please try again");
}
}
}

var xmlHttp = new XMLHttpRequest();
var txtname = document.getElementById("txtname");
xmlHttp.open("POST","http://localhost:8080/Jelly/GetStuff",true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send("url=" + document.URL);

i'm keep getting xmlhttp.status==0 and not 4, even when instead of
"localhost" i put the ip address. Any ideas?

Thanks a lot...

ZER0

unread,
Aug 21, 2011, 6:44:11 AM8/21/11
to mozilla-la...@googlegroups.com
On Saturday, August 20, 2011 at 15:48 PM, Alon wrote:
> I'm writing an extension for FF and it is calling using "PageMod" to
> the js file that contains:
[..]

> i'm keep getting xmlhttp.status==0 and not 4,
I guess you meant `readyState`.
> even when instead of
> "localhost" i put the ip address. Any ideas?

I assuming the code you wrote is in the content script injected by PageMod. In that case, the XMLHttpRequest is subject to the Same Origin Policy of the host page, so it can't perform cross domain requests (at least in FF > 4). That's because you obtain that value.

The solution is doing the requests from the addon code, not the content script code, using the request module:

<https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/request.html>

In order to pass data between content script and addon code:

<https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/dev-guide/addon-development/web-content.html>

> Thanks a lot...

Hope it helps!

Alon

unread,
Aug 23, 2011, 4:29:52 PM8/23/11
to mozilla-labs-jetpack
Hi,

Thanks a lot for the help!
May you please be more detailed? Who is calling the Request? How to
use the response?

Thanks again

main.js:
...
pageMod1 = pageMod.PageMod({
include: "*",
contentScriptFile: [data.url("panel.js")],
contentScriptWhen: 'ready',
contentScript: 'document.body.innerHTML += "THE ADD-ON IS WORKING'
});

panel.js:
function handleServerResponse() {

if (xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
//some code
}
else {
alert("Error during AJAX call. Please try again");
}
}
}

var xmlHttp = new XMLHttpRequest();
var txtname = document.getElementById("txtname");
xmlHttp.open("POST","http://localhost:8080/Jelly/GetStuff",true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send("url=" + document.URL);

On Aug 21, 1:44 pm, ZER0 <z...@mozilla.com> wrote:
> On Saturday, August 20, 2011 at 15:48 PM, Alon wrote:
> > I'm writing an extension for FF and it is calling using "PageMod" to
> > the js file that contains:
> [..]
> > i'm keep getting xmlhttp.status==0 and not 4,
>
> I guess you meant `readyState`.
>
> > even when instead of
> > "localhost" i put the ip address. Any ideas?
>
> I assuming the code you wrote is in the content script injected by PageMod. In that case, the XMLHttpRequest is subject to the Same Origin Policy of the host page, so it can't perform cross domain requests (at least in FF > 4). That's because you obtain that value.
>
> The solution is doing the requests from the addon code, not the content script code, using the request module:
>
> <https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/add...>
>
> In order to pass data between content script and addon code:
>
> <https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/dev-guide/ad...>

ZER0

unread,
Aug 23, 2011, 5:33:26 PM8/23/11
to mozilla-la...@googlegroups.com
On 08/23/11 22:29 PM, Alon wrote:

> Thanks a lot for the help!
> May you please be more detailed? Who is calling the Request? How to
> use the response?

Your code should be something like that (more or less):

// main.js
let pageMod = require("page-mod");
let data = require("self").data;
let Request = require("request").Request;

let myPageMod = pageMod.PageMod({


include: "*",
contentScriptFile: [data.url("panel.js")],
contentScriptWhen: "ready",

onAttach: function onAttach(worker) {

worker.on("message", function (url) {
Request({
url : "http://localhost:8080/Jelly/GetStuff",
contentType : "application/x-www-form-urlencoded",
content : "url=" + url,

onComplete : function onComplete(response) {
if (response.status === 200) {
worker.postMessage(response.text);
} else {
worker.postMessage(null);
}
}
}).post();

});
}
});

// panel.js
console.log("The Addon is working");

// Manage the response from the Addon code
onMessage = function onMessage(content) {
if (content === null) {
alert("Request failed")
} else {
console.log("data received: " + content);
}
}

// Send the URL to the Addon code
self.postMessage(document.URL);

You can find more information in the documentation:
<https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/page-mod.html>
<https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/request.html>

Notice that, of course, you can send more meaningful data and complex
object from / to the addon code.

> Thanks again

You're welcome!

Alon

unread,
Aug 27, 2011, 1:51:37 PM8/27/11
to mozilla-labs-jetpack
Hi,

Thanks! it is working!

Can you assist in one last thing?
Using context-menu I added in a contextMenu.js file HTML code to the
document:
something like that - document.body.appendChild(element);

this element has a button that onClick needs to call a function.
As I understand I can't put this function in the file(the document
doesn't recognize the method),
I can't inject it because of the changes in FF versions >4.
I have read the documentation, but still having trouble-
how to use this function? where to put it?

Thanks again
> <https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/add...>
> <https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/add...>
Reply all
Reply to author
Forward
0 new messages