-1

我是一名非计算机科学或编程专业的学生,​​但我一直在为我的父母寻找一个脚本,他们是外国人并且不懂互联网术语。

我在 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]);
        }
    }
}
4

2 回答 2

0

对 Internet Explorer 的用户脚本支持参差不齐,而且很快就会过时。也就是说,有些人报告说让IE7Pro与 IE9 一起工作取得了一些成功。

但是 IE javascript 和 Chrome、Firefox 等的 javascript 可能有很大的不同。在iescripts.org
的教程中有一个完整的部分,关于将用户脚本移植到 IE (特别是第 5 节)。

阅读该教程并按照列出的指南诚实地尝试移植代码。

于 2012-12-26T19:31:15.870 回答
0

IE 不支持document.evaluate 。不知道脚本的作用,因此为您的人安装真正的浏览器可能会更容易。

也许jQuery可以帮助你。

于 2012-12-26T17:19:40.443 回答