【问题标题】:Convert userscript from Firefox/Chrome to Internet Explorer 9?将用户脚本从 Firefox/Chrome 转换为 Internet Explorer 9?
【发布时间】: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


【解决方案1】:

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

也许jQuery 可以帮助你。

【讨论】:

  • 好的,有道理。该脚本的重点是在网页中搜索某些文本并将其替换为显示您指定的内容。我会四处搜索我猜想找到一个实现这一点的IE解决方案,他们真的不想切换浏览器。
【解决方案2】:

对 Internet Explorer 的用户脚本支持参差不齐,而且很快就会过时。也就是说,some people have reported some success getting IE7Pro to work with IE9.

但 IE javascript 和 Chrome、Firefox 等的 javascript 可能完全不同。
There is a whole section, on porting userscripts to IE, in iescripts.org's tutorial (see section 5 especially).

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

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多