简体   繁体   English

如何在不打开任何窗口的情况下使用Firefox附加SDK解析html字符串

[英]how to parse a html string with Firefox add-on SDK without opening any window

I downloaded jquery module from github, only to find that a lot of functions and objects are undefined. 我从github下载了jquery模块,却发现很多函数和对象是未定义的。

  var Request = require("sdk/request").Request;
  var Jquery = require("jquery");
  var latestTweetRequest = Request({
  url: "https://example.com",
  onComplete: function (response) {
    var abc = Jquery.jquery(response);
  }
});

Is there any light-weight lib can help me 有没有轻巧的lib可以帮助我

You can use built-in DOMParser awesome topic and solution by MJ Saedy: 您可以使用MJ Saedy内置的DOMParser很棒的主题和解决方案:

https://stackoverflow.com/a/23207820/1828637 https://stackoverflow.com/a/23207820/1828637

documenation on it here: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser?redirectlocale=en-US&redirectslug=DOM%2FDOMParser 此处的文档: https : //developer.mozilla.org/en-US/docs/Web/API/DOMParser? redirectlocale = zh-CN&redirectslug = DOM%2FDOMParser

I'm not sure how to do this from XPCOM though but there has to be a way. 我不确定如何从XPCOM进行此操作,但是必须有一种方法。

edit: found out how to do it from XPCOM and addon-sdk. 编辑:找到如何从XPCOM和addon-sdk执行此操作。

do it like this: 像这样做:

var DOMParser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);

see here for addon sdk details: https://stackoverflow.com/a/9172947/1828637 请参阅此处以获取插件SDK详细信息: https : //stackoverflow.com/a/9172947/1828637

111 /**
112  * Parse the given string into a DOM tree
113  *
114  * @param str       The string to parse
115  * @param docUri    (optional) The document URI to use
116  * @param baseUri   (optional) The base URI to use
117  * @return          The parsed DOM Document
118  */
119 cal.xml.parseString = function(str, docUri, baseUri) {
120     let parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
121                            .createInstance(Components.interfaces.nsIDOMParser);
122 
123     parser.init(null, docUri, baseUri);
124     return parser.parseFromString(str, "application/xml");
125 };

http://mxr.mozilla.org/comm-central/source/calendar/base/modules/calXMLUtils.jsm#120 http://mxr.mozilla.org/comm-central/source/calendar/base/modules/calXMLUtils.jsm#120

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

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