views:

151

answers:

2

This may seem like a no-brainer, but I can't find a way to do this that isn't considered a security issue (other than the obvious ways)...

So, I want to build an add-on for Firefox to use with my team. Basically it will be a status bar icon letting us know if the authentication cookie for our tools site has expired, so we can tell without losing any work currently in the browser.

At first I thought I could have the add-on check the cookie, but this seems to be a huge hassle for such a simple idea. Then it occurred to me...DUH...that I could just have the add on try to access the main page of our site. If it gets a "Access Denied" response, it can show the icon for "Not Logged In", but if it gets anything else, it can show "Signed In".

However, all attempts to do this with AJAX are proving to be almost as difficult as my cookie attempts.

Is there a simple way, with javascript preferably, but in XUL otherwise, to say

var url = "http://example.com";
var response = pingURL(url, "blah);
status = (response = "Welcome!") ? "Signed in" : "Not Signed In";

where "pingURL" would be the method of "going" to the url and getting the response?

+1  A: 
function checkAccess(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                callback(true);
            } else {
                callback(false);
            }
        }
    };
}

This should work... Just call with "checkAccess('http://example.com', function(ready){});" as an example where ready is a boolean value.

devyn
Doesn't look so good. I'm still getting this in firebug:uncaught exception: Access to restricted URI denied (NS_ERROR_DOM_BAD_URI)and it just sits there in Chrome. I'm pretty sure I'm missing something obvious.
Anthony
God I hate SOP (Same Origin Policy). I don't think extensions should have it though... Are you running the XUL file in Chrome, as a Local File, or from the Web?
devyn
Actually, I'm having an SOP problem myself right now... Too bad you can't access localhost from elsewhere. And I don't think Firefox has a "expose" option...
devyn
A: 

Exactly why do you consider cookies a huge hassle? That would undoubtedly be faster and probably simpler to implement. Reading cookies from chrome is simple and well-documented. Ask for help if you can't figure out how to parse the cookie.

Matthew Flaschen