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