简体   繁体   English

如何在不实际加载文件的情况下通过javascript检查chrome://文件是否存在?

[英]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: 我在Firefox扩展程序中使用以下功能来检查另一个扩展程序中是否存在文件:

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. 我要查询的某些文件的大小超过1MB,所以我宁愿不将它们加载到内存中。

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. 我曾尝试使用onreadystatechange,但似乎无法弄清楚。

You can use the onreadystatechange method of an XMLHTTPRequest and a setTimeout. 您可以使用XMLHTTPRequest和setTimeout的onreadystatechange方法。 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.). 本地文件的状态始终为“ 0”(而不是200等)。

  2. if async is true, it won't throw an exception if the file is not found. 如果async为true,则在找不到文件的情况下不会引发异常。

  3. it seems to skip readyState 3 for some reason - if async is false, readyState goes straight to 4. 由于某种原因,它似乎跳过了readyState 3-如果async为false,则readyState直接进入4。

  4. if the first part of the chrome URL (the extension name) doesn't exist, it throws an exception on open() . 如果chrome URL的第一部分(扩展名)不存在,则会在open()上引发异常。

  5. If async is false and the file doesn't exist, it throws an exception on onreadystatechange . 如果async为false且文件不存在,则它将在onreadystatechange上引发异常。

  6. If async is false and the file does exist, aborting onreadystatechange stops it from actually reading the file. 如果async为false并且文件确实存在,则中止onreadystatechange使其无法实际读取文件。

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). 因此,似乎要走的路是async = false,在readyState更改成功发生(到4)后中止并返回true(文件存在)。 If there is an exception on open or onreadystatechange, return false (doesn't exist). 如果open或onreadystatechange发生异常,则返回false(不存在)。

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: 这是代码,如果该文件存在,则xmlhttp.responseXML仍为null时似乎会中止,如果不存在,则抛出异常:

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 :) 您测试起来可能比我更容易,因为我已经有一段时间没有弄乱扩展了,并且您不能从普通页面中完全请求chrome://

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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