javascript - PhantomJS 无法打开未知扩展名的本地文件

标签 javascript phantomjs

我正在使用 phantomjs 来获取本地文件的屏幕截图。 现在我传递了一个完全有效的 html 文件:

<!DOCTYPE html><html><head><title>Title of the document</title></head><body>The file name dummy</body></html> 

文件名为dummy.hoo

PhantomJS 似乎无法打开这个。这在某个地方有记录吗?不过,扩展名为 .html.htm 的本地文件就可以了。

示例调用(页面路径始终转换为 Uri 方案)

"Phantomjs.exe" --proxy-type=none --ssl-protocol=any --local-to-remote-url-access=true "Scripts\screenshot.js" "file:///D:/dummy.hoo" "base.png"

js很简单:

var page = require('webpage').create();
var system = require('system');

if (system.args.length !== 3) {
    console.log('Usage: script.js <URL> <screenshot destination>');
    phantom.exit();
}

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

page.open(system.args[1], function(status) {
    if (status !== 'success') {
        console.log('Failed to load address '+system.args[1]+' ' + page.reason_url               + ": " + page.reason);
        phantom.exit(-1);
    }
    page.render(system.args[2]);
    phantom.exit();
});

当我复制 Uri 并将其粘贴到 firefox 等时,我可以正确看到 dummy.hoo 的 html 内容。只有 phantomjs 似乎拒绝渲染此内容。

对于 dummy.hoo,它始终会出现错误路径,提示无法加载地址,状态为 fail 并且不会通过回调给出任何原因。 (当我传递一个不存在的网址时,我得到一个适当的理由)

Failed to load address file:///D:/dummy.hoo undefined: undefined

我使用了此处的详细错误输出链接: Debugging PhantomJS webpage.open failures

这是结果:

= onNavigationRequested
  destination_url: file:///D:/dummy.hoo
  type (cause): Other
  will navigate: true
  from page's main frame: true
= onResourceRequested()
  request: {
    "headers": [
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.0 Safari/534.34"
        },
        {
            "name": "Accept",
            "value": "*/*"
        }
    ],
    "id": 1,
    "method": "GET",
    "time": "2015-03-01T16:40:11.080Z",
    "url": "file:///D:/dummy.hoo"
}
= onLoadStarted()
  leaving url: about:blank
= onResourceReceived()
  id: 1, stage: "start", response: {"bodySize":110,"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redir
ectURL":null,"stage":"start","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onResourceReceived()
  id: 1, stage: "end", response: {"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redirectURL":null,"sta
ge":"end","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onLoadFinished()
  status: fail
Failed to load address file:///D:/dummy.hoo undefined: undefined

最佳答案

我能够在 phantomjs 中找到处理 mime 类型的代码(不同驱动程序的多个位置):

https://github.com/ariya/phantomjs/blob/48fabe06463460d2fb7026d6df9783216e26265c/src/qt/qtwebkit/Source/WebCore/platform/MIMETypeRegistry.cpp#L154

https://github.com/ariya/phantomjs/blob/48fabe06463460d2fb7026d6df9783216e26265c/src/qt/qtwebkit/Source/WebCore/platform/win/MIMETypeRegistryWin.cpp#L80 等等

背后的要点(呵呵)是本地文件不发送包含 MIME 类型的 header 信息。因此,Phantomjs 不知道应该调用哪个处理程序来正确呈现内容。我基本上可以将 .jpeg 重命名为 .exe,只要 Web 服务器发送 jpg mime 类型,它就会正确呈现。这是网络中的常见行为,根据任何内容(正则表达式、扩展名等)重定向 url 部分

Phantoms 没有某种推理来检测文件的真实内容(这完全合理),因此它必须依赖于文件扩展名和给定的映射。

所以知道我必须接受我可以使用 htmlhtm 扩展来呈现 html 数据,而不是其他。

关于javascript - PhantomJS 无法打开未知扩展名的本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796218/

相关文章:

javascript - JavaScript中变量的同步值

javascript - grunt-usemin:定义自定义流程

javascript - 如何从对象数组中获取最大值以在 d3.scale.linear().domain() 中使用

javascript - react 中的 Mapbox(ReferenceError : L is not defined)

PhantomJS:在任何其他脚本运行之前注入(inject)脚本

ruby-on-rails-3 - Rails Phantomjs、poltergeist 和 Capybara 不能很好地配合

javascript - ES5 中的 block 作用域

javascript - CasperJS中如何遍历网站的dom树并获取所有元素?

javascript - phantomjs/casperjs 计算 DOM 元素

phantomjs - 节点 js 和 phantomjs - 找不到模块 'weak'