简体   繁体   English

如何在 Firefox 扩展中将一串 HTML 转换为 DOM 对象?

[英]How can I turn a string of HTML into a DOM object in a Firefox extension?

I'm downloading a web page (tag soup HTML) with XMLHttpRequest and I want to take the output and turn it into a DOM object that I can then run XPATH queries on.我正在下载带有 XMLHttpRequest 的网页(标记汤 HTML),我想获取输出并将其转换为 DOM 对象,然后我可以在其上运行 XPATH 查询。 How do I convert from a string into DOM object?如何将字符串转换为 DOM 对象?

It appears that the general solution is to create a hidden iframe and throw the contents of the string into that.看来一般的解决方案是创建一个隐藏的 iframe 并将字符串的内容扔进去。 There has been talk of updating DOMParser to support text/html but as of Firefox 3.0.1 you still get an NS_ERROR_NOT_IMPLEMENTED if you try.已经有议论更新的DOMParser来支持文本/ HTML,但像Firefox 3.0.1的你仍然可以获得一个NS_ERROR_NOT_IMPLEMENTED如果你尝试。

Is there any option besides using the hidden iframe trick?除了使用隐藏的 iframe 技巧之外,还有其他选择吗? And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up the code, etc)?如果不是,那么执行 iframe 技巧的最佳方法是什么,以便您的代码在任何当前打开的选项卡的上下文之外工作(以便关闭选项卡不会搞砸代码等)?

This is an example of why I'm looking for a solution other than the iframe hack, if I have to write all that code to have a robust solution, then I'd rather keep looking for something else. 是我为什么要寻找 iframe hack 以外的解决方案的一个示例,如果我必须编写所有代码才能获得强大的解决方案,那么我宁愿继续寻找其他解决方案。

Ajaxian actually had a post on inserting / retrieving html from an iframe today. Ajaxian 今天实际上有一篇关于从 iframe 插入/检索 html帖子 You can probably use the js snippet they have posted there.您可能可以使用他们在那里发布的 js 片段。

As for handling closing of a browser / tab, you can attach to the onbeforeunload ( http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx ) event and do whatever you need to do.至于处理浏览器/选项卡的关闭,您可以附加到 onbeforeunload ( http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx ) 事件并执行您需要执行的任何操作.

Try this:试试这个:

var request = new XMLHttpRequest();

request.overrideMimeType( 'text/xml' );
request.onreadystatechange = process;
request.open ( 'GET', url );
request.send( null );

function process() { 
    if ( request.readyState == 4 && request.status == 200 ) {
        var xml = request.responseXML;
    }
}

Notice the overrideMimeType and responseXML .注意overrideMimeTyperesponseXML
The readyState == 4 is 'completed'. readyState == 4是“已完成”。

Try creating a div尝试创建一个 div

document.createElement( 'div' );

And then set the tag soup HTML to the innerHTML of the div.然后将标签soup HTML设置为div的innerHTML。 The browser should process that into XML, which then you can parse.浏览器应该将其处理为 XML,然后您可以对其进行解析。

The innerHTML property takes a string that specifies a valid combination of text and elements. innerHTML 属性采用指定文本和元素的有效组合的字符串。 When the innerHTML property is set, the given string completely replaces the existing content of the object.当设置了innerHTML 属性时,给定的字符串将完全替换对象的现有内容。 If the string contains HTML tags, the string is parsed and formatted as it is placed into the document.如果字符串包含 HTML 标记,则在将字符串放入文档时对其进行解析和格式化。

So you want to download a webpage as an XML object using javascript, but you don't want to use a webpage?因此,您想使用 javascript 将网页下载为 XML 对象,但又不想使用网页? Since you have no control over what the user will do (closing tabs or windows or whatnot) you would need to do this in like a OSX Dashboard widget or some separate application.由于您无法控制用户将执行的操作(关闭选项卡或窗口或诸如此类),因此您需要像 OSX 仪表板小部件或某些单独的应用程序一样执行此操作。 A Firefox extension would also work, unless you have to worry about the user closing the browser. Firefox 扩展也可以使用,除非您必须担心用户关闭浏览器。

Is there any option besides using the hidden iframe trick?除了使用隐藏的 iframe 技巧之外,还有其他选择吗?

Unfortunately, no, not now.不幸的是,不,不是现在。 Otherwise the microsummary code you point to would use it instead.否则,您指向的摘要代码将使用它。

And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up code, etc)?如果不是,那么执行 iframe 技巧的最佳方法是什么,以便您的代码在任何当前打开的选项卡的上下文之外工作(以便关闭选项卡不会搞砸代码等)?

The code you quoted uses the recent browser window, so closing tabs won't affect parsing.您引用的代码使用最近的浏览器窗口,因此关闭选项卡不会影响解析。 Closing that browser window will abort your load, but you can deal with it (detect that the load is aborted and restart it in another window for example) and it doesn't happen very often.关闭该浏览器窗口将中止您的加载,但您可以处理它(例如,检测到加载中止并在另一个窗口中重新启动它)并且这种情况不会经常发生。

You need a DOM window for the iframe to work properly, so there's no clean solution at the moment (if you're keen on using the mozilla parser).您需要一个 DOM 窗口让 iframe 正常工作,因此目前没有干净的解决方案(如果您热衷于使用 mozilla 解析器)。

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

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