1

我不是这段代码的作者,但它不再被维护。所以我正在尝试修复它,但我在 javascript 方面的经验很少。从 Firefox 9 开始,Components.classesByID["{3a9cd622-264d-11d4-ba06-0060b0fc76dd}"]。已经过时了。相反,建议使用 document.implementation.createDocument。有人可以告诉我如何实施这些更改吗?我似乎是,只是用我尝试过的一切来敲打我的头。

Mozilla 开发者网络给出的示例是:

    var doc = document.implementation.createDocument ("http://www.w3.org/1999/xhtml", "html", null);
var body = document.createElementNS("http://www.w3.org/1999/xhtml", "body");
body.setAttribute("id", "abc");
doc.documentElement.appendChild(body);
alert(doc.getElementById("abc")); // [object HTMLBodyElement]

我要修复的 .jsm 中的代码是:

createFgImageData: function helpers_createFgImageData() {

    // I don't know how to load images synchronously *FIX*
    // Until then, this awesome bitmap format below will do. 

    this.fgImageData = {};
    this.fgImageData["check"] = [
        "      *",
        "     **",
        "*   ***",
        "** *** ",
        "*****  ",
        " ***   ",
        "  *    "];
    this.fgImageData["radio"] = [
        " **** ",
        "******",
        "******",
        "******",
        "******",
        " **** "];
    this.fgImageData["menu-ltr"] = [
        "*   ",
        "**  ",
        "*** ",
        "****",
        "*** ",
        "**  ",
        "*   "];
    this.fgImageData["menu-rtl"] = [
        "   *",
        "  **",
        " ***",
        "****",
        " ***",
        "  **",
        "   *"];            

    // I think I'm doing something slightly wrong when creating the document
    // but I'm not sure. It works though. *FIX*

    var domi = Components.classesByID["{3a9cd622-264d-11d4-ba06-0060b0fc76dd}"].
                    createInstance(Components.interfaces.nsIDOMDOMImplementation);
    this.document = domi.createDocument("http://www.w3.org/1999/xhtml", "html", null);
    this.canvas = this.document.createElementNS("http://www.w3.org/1999/xhtml", "html:canvas");

    for(var name in this.fgImageData) {
        if (this.fgImageData.hasOwnProperty(name)) {
            var data = this.fgImageData[name];
            var width = data[0].length;
            var height = data.length;

            this.canvas.width = width;
            this.canvas.height = height;
            var g = this.canvas.getContext("2d");
            g.clearRect(0, 0, width, height);
            var idata = g.getImageData(0, 0, width, height);
            for(var y=0, oy=0; y<height; y++, oy+=idata.width*4)
                for(var x=0, ox=oy; x<width; x++, ox+=4)
                    idata.data[ox+3] = data[y][x] == " " ? 0 : 255;

            this.fgImageData[name] = idata;
        }
    }
}, 
4

2 回答 2

1

JSM 中没有可使用的预先存在的文档。你有几个选择:

  1. 传入调用者的相关父文档。
  2. 使用 DOMParser 对空字符串或 XHR 创建一个空白文档到一个空数据:URI。
于 2012-09-09T13:06:30.693 回答
1

如果您无法访问实际的窗口(例如在 JavaScript 模块中),最好使用DOMParser创建 HTML 文档(感谢 Boris 的提示)。它可以在任何上下文中实例化:

var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
                       .createInstance(Components.interfaces.nsIDOMParser);
var dummyDocument = "<html xmlns='http://www.w3.org/1999/xhtml'/>";
this.document = parser.parseFromString(dummyDocument, "text/xml");
this.canvas = this.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");

请注意,第二个参数createElementNS()应该是"canvas",而不是"html:canvas"- 命名空间在第一个参数中指定。另请注意,从 Firefox 12 开始,DOMParser 还可以解析 HTML 代码(MIME 类型"text/html"),因此您也可以编写:

this.document = parser.parseFromString("", "text/html");

一个最小的 HTML 文档确实是空字符串。

于 2012-09-09T19:15:21.143 回答