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.
I think I figured it out, after learning a few things about the way this type of request is handled:
status for local files is always "0" (not 200, etc.).
if async is true, it won't throw an exception if the file is not found.
it seems to skip readyState 3 for some reason - if async is false, readyState goes straight to 4.
if the first part of the chrome URL (the extension name) doesn't exist, it throws an exception on
open()
.If async is false and the file doesn't exist, it throws an exception on
onreadystatechange
.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;
}
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;
}
}
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 :)
精彩评论