1

I downloaded jquery module from github, only to find that a lot of functions and objects are undefined.

  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

Nya
  • 310
  • 2
  • 10
  • Yes this is because you are loading the jquery into privelaged scope, where things like `window` mean something different. Lots of stuff jquery is looking for is not availabe in that scope. – Noitidart Nov 15 '14 at 04:47
  • You have to either use string manipulation on the response. Or like some people recommend, not to use regex to parse html, but use a parser, i dont know of any right now but im pretty sure firefox should have something. http://stackoverflow.com/questions/1444409/in-javascript-how-can-i-replace-text-in-an-html-page-without-affecting-the-tags/1444893#1444893 – Noitidart Nov 15 '14 at 06:31
  • lookie what i found some parser in xpcom: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIScriptableUnescapeHTML from SO topic: http://stackoverflow.com/questions/10195857/how-to-keep-attributes-with-parsefragment-in-firefox-extension – Noitidart Nov 15 '14 at 06:32
  • even nicer example here: http://stackoverflow.com/questions/23192000/using-mozilla-firefox-parser-rendering-engine-in-an-extension – Noitidart Nov 15 '14 at 06:33

1 Answers1

0

You can use built-in DOMParser awesome topic and solution by MJ Saedy:

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

I'm not sure how to do this from XPCOM though but there has to be a way.

edit: found out how to do it from XPCOM and 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

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

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323