我是一名非计算机科学或编程专业的学生,但我一直在为我的父母寻找一个脚本,他们是外国人并且不懂互联网术语。
我在 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]);
}
}
}