3

I'm using the following function in a Firefox extension to check if a file exists in another extension:

function chromeFileExists(fileLoc) // in extension package
{
    var xmlhttp = new window.XMLHttpRequest();
    try {
        xmlhttp.open("GET", "chrome://"+fileLoc, false);
        xmlhttp.send(null);
        var xmlDoc = xmlhttp.responseXML.documentElement;
    }
    catch(ex) {
        return false;
    }
    return true;
} 

But the problem is, of course, that if the file does exist, it actually loads the file before it tells me. Some of the files I'm querying are over 1MB in size, so I'd rather not load them into memory.

How to check for the existence and return without loading the file itself? I've tried working with onreadystatechange, but can't seem to figure it out.

3 Answers 3

3

I think I figured it out, after learning a few things about the way this type of request is handled:

  1. status for local files is always "0" (not 200, etc.).

  2. if async is true, it won't throw an exception if the file is not found.

  3. it seems to skip readyState 3 for some reason - if async is false, readyState goes straight to 4.

  4. if the first part of the chrome URL (the extension name) doesn't exist, it throws an exception on open().

  5. If async is false and the file doesn't exist, it throws an exception on onreadystatechange.

  6. If async is false and the file does exist, aborting onreadystatechange stops it from actually reading the file.

So, it seems the way to go is async=false, abort after the readyState change occurs successfully (to 4) and return true (the file exists). If there is an exception on open or onreadystatechange, return false (doesn't exist).

Here's the code, which seems to abort while xmlhttp.responseXML is still null if the file exists, and throws an exception if it doesn't:

function chromeFileExists(fileLoc) // in extension package
{
    var xmlhttp = new window.XMLHttpRequest();
    try {
        xmlhttp.open("GET", "chrome://"+fileLoc, false);
        xmlhttp.onreadystatechange=function() {
            xmlhttp.abort();
        }
        xmlhttp.send(null);
    }
    catch(ex) {
        return false;
    }
    return true;
} 
Sign up to request clarification or add additional context in comments.

1 Comment

how to use this from a php file?
1

You can use the onreadystatechange method of an XMLHTTPRequest and a setTimeout. I haven't actually tried this but I imagine it would go something like:

var clearhttp = function() {
    xmlhttp.abort();
    fileDoesNotExist = false;
}
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 3) {
        setTimeout(clearhttp, 250);
    } else if(xmlhttp.readyState == 4 && xmlhttp.status == 404){
        fileDoesNotExist = true;
    }
}

3 Comments

+1, but I imagine that if you ever hit readyState==3 and it's a 404 you'll already have the status code, thus making the timeout unnecessary (or at least too long). Also, I'm not sure if this would even work with "local" files as it may just copy the entire thing in one block.
No, that doesn't work... if the file exists, the status is always 0, and the ready state always 4. If I use xmlhttp.open("GET", "chrome://"+fileLoc, true);, it works if the file exists but throws NS_ERROR_FILE_NOT_FOUND if it doesn't (which I assumes means it is ignoring the abort).
Yes, I believe that's the rub... it might work with remote files, but not local ones.
1

what about:

xmlhttp.open("HEAD", "chrome://"+fileLoc, false);

Probably easier for you to test than it is for me since I haven't messed with extensions in a while and you can't exactly request chrome:// from a normal page :)

4 Comments

Thanks for the suggestion, but no, this still returns the contents of the file.
I don't believe HEAD is a recognized method: developer.mozilla.org/en/XMLHttpRequest#open%28%29 - also, it says it will ignore the method on non-HTTP requests, which I assume this is?
It's a valid HTTP method, but you're right that it says it's ignored... meh, sorry it was my best shot!
S'okay, thanks for the help. I figured the thing out, but since I'm a new user I can't reply to my own question until eight hours. I'll post a solution later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.