【发布时间】:2012-12-26 17:07:11
【问题描述】:
我是一名非计算机科学或编程专业的学生,但我一直在为我的外国父母寻找一个脚本,他们不懂互联网术语。
我在 userscripts.org 上找到了下面的脚本(全部归功于作者),它就像一个魅力。但是,我的父母太固执,无法从 IE9 切换到 Chrome 或 Firefox,所以我下载了 IE7Pro 并在那里安装了脚本。
这里的问题是安装在 IE9 上的用户脚本似乎根本不起作用,我正试图找出原因。 Chrome/Firefox 与 Internet Explorer 的 javascript 是否存在基本差异?
我该如何解决这些问题以使其在 IE 中运行?我只上过介绍性的 C 编程课程,在这个领域几乎没有经验,所以如果有人能简单地向我解释一下,那就太好了。
这是供参考的代码,如果有人能指出它在哪里/为什么不起作用,那将是很大的帮助。它不会太长,它会为我节省无数小时的学习时间,我真的不需要学习。非常感谢。
var words = {
///////////////////////////////////////////////////////
// Syntax: 'Search word' : 'Replace word',
"your a": "you're a",
"im*o": "in my honest opinion",
///////////////////////////////////////////////////////
"": ""
};
//////////////////////////////////////////////////////////////////////////////
// This is where the real code is
// Don't edit below this
//////////////////////////////////////////////////////////////////////////////
// prepareRegex by JoeSimmons
// Used to take a string and ready it for use in new RegExp()
String.prototype.prepareRegex = function () {
return this.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, "\\$1");
};
// Function to decide whether a parent tag will have its text replaced or not
function isOkTag (tag) {
return (
new RegExp (
"(," + tag + ",) | (," + tag + "$)",
"g"
).test (",pre,blockquote,code,input,button,textarea")
) == false;
}
// Convert the "words" JSON object to an Array
var regexs = new Array(),
replacements = new Array();
for (var word in words) {
if (word != "") {
regexs.push (new RegExp (word.prepareRegex ().replace (/(\\)?\*/g, function (e) {
return ((e !== "\\*") ? "[^ ]*" : "*");
} ), "gi"));
replacements.push (words[word]);
}
}
// Do the replacement
var texts = document.evaluate (
".//text()[normalize-space(.)!='']", document.body, null, 6, null
),
text = "",
len = regexs.length;
for (var i = 0, l = texts.snapshotLength; (this_text = texts.snapshotItem(i)); i++) {
if (isOkTag (this_text.parentNode.tagName) && (text = this_text.textContent) ) {
for (var x = 0; x < len; x++) {
text = this_text.textContent = text.replace(regexs[x], replacements[x]);
}
}
}
【问题讨论】:
-
有很多代码要弄清楚。另外,我们需要发誓吗?
-
抱歉,这不是我的代码,但我意识到事后看来我可以很容易地替换发誓
标签: javascript internet-explorer internet-explorer-9 userscripts document.evaluate