简体   繁体   中英

How to check if a chrome:// file exists via javascript, without actually loading the file?

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.

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;
    }
}

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;
} 

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM