admin管理员组文章数量:1426855
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'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.
Share Improve this question edited May 8, 2011 at 5:25 yuttadhammo asked May 8, 2011 at 4:55 yuttadhammoyuttadhammo 5,2096 gold badges37 silver badges46 bronze badges3 Answers
Reset to default 3I 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 :)
本文标签:
版权声明:本文标题:How to check if a chrome: file exists via javascript, without actually loading the file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483913a2660296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论