gpt4 book ai didi

javascript - 将 Components.classesByID 替换为 document.implementation.createDocument

转载 作者:行者123 更新时间:2023-11-30 18:19:53 25 4
gpt4 key购买 nike

我不是此代码的作者,但已不再维护。所以我正在尝试修复它,但我在 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;
}
}
},

最佳答案

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

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 文档确实是空字符串。

关于javascript - 将 Components.classesByID 替换为 document.implementation.createDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com