0

我有一个 Greasemonkey 脚本,其逻辑如下:

  1. 除非登录用户属于特权组(JOHN、LUKE、JEFF、MAX、ANDY),否则禁用<input>with name="prio"

  2. 如果不是特权用户,请阻止从<input name="dest">.
    具体来说,阻止“12”或“22”并写入错误消息。


该脚本工作正常,但有一个错误:

当我在其他输入字段中插入文本或更改选择字段并按Enter时,提交停止并且我在控制台中收到错误消息:

错误:destInput[0] 没有属性源文件:file:///E:/​​FirefoxPortable2.x/Data/profile/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee​​082ba24781%7D/components/greasemonkey.js 行: 379

var err = this.findError(script, line - lineFinder.lineNumber - 1);

我认为该规则name="dest"适用于所有输入字段和选择字段?但我只想要“dest”输入的规则。

这是我的脚本代码

// ==UserScript==
// @name           _SO Block submit on custom FF2 page.
// @namespace      PC
// @include        file:///D:/temp/foo%20SO/task*
// @include        file:///D:/temp/foo%20SO/fixture*
// @include        file:///D:/temp/foo%20SO/pallet*
// ==/UserScript==

GM_log ("Script start.");

unsafeWindow._oldSubmit  = unsafeWindow.document.forms[0].submit;
unsafeWindow.document.forms[0].submit = function () {
    GM_log ("Submit function fired.");

    var destInput   = document.getElementsByName ("dest");
    if ( ! destInput  ||  destInput.length == 0) {
        //unsafeWindow._oldSubmit ();
    }

    var destValue   = destInput[0].value;
    if (    /^\s*$/.test (destValue)
            ||  excludedDestinations.indexOf (destValue) > -1
    ) {
        GM_log ("Submit should be blocked! (1)");
    }
    else {
        //unsafeWindow._oldSubmit ();
    }
};


//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority  = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];
var excludedDestinations    = ['12', '22'];

var bDisablePrio    = true;
var tdNodes         = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1;  J >= 0;  --J) {
    var tdNode      = tdNodes[J];
    if (tdNode.className == "user") {
        var userName        = tdNode.textContent.replace (
            /^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
        ).toUpperCase ();
        if (usersWhoCanSetPriority.indexOf (userName) > -1) {
            bDisablePrio = false;
        }
    }
}

if (bDisablePrio) {
    var oldInput    = document.getElementsByName ("prio");
    if (oldInput  &&  oldInput.length) {
        oldInput[0].setAttribute ("disabled", "disabled");
    }

    var destInput   = document.getElementsByName ("dest");
    if (destInput  &&  destInput.length) {
        destInput[0].addEventListener (
            "change",
            function (zEvent) {
                bCheckdestinationValue (destInput[0]);
                GM_log ("Change handler fired.");
            },
            false
        );

        destInput[0].form.addEventListener (
            "submit",
            function (zEvent) {
                GM_log ("Submit handler fired.");
                var destValue   = destInput[0].value;
                if (    /^\s*$/.test (destValue)
                        ||  excludedDestinations.indexOf (destValue) > -1
                ) {
                    //--- Stop the submit
                    zEvent.preventDefault ();
                    //zEvent.stopPropagation ();
                    GM_log ("Submit should be blocked! (2)");
                    return false;
                }
            },
            true
        );
    }
}

function bCheckdestinationValue (destInputNd) {
    //--- Returns true if value is bad.
    if (excludedDestinations.indexOf (destInputNd.value) > -1) {
        destInputNd.value = ''; // Blank input

        //--- Add or show Error message.
        var destErrNode = document.getElementById ("gmDestErrorDisp");
        if (destErrNode) {
            destErrNode.style.display = "inline";
        }
        else {
            destErrNode             = document.createElement ('b');
            destErrNode.id          = "gmDestErrorDisp";
            destErrNode.style.color = "red";
            destErrNode.textContent = "12 and 22 are forbidden";
            destInputNd.parentNode.appendChild (destErrNode);
        }
        return true;
    }
    else {
        var destErrNode = document.getElementById ("gmDestErrorDisp");
        if (destErrNode) {
            destErrNode.style.display = "none";
        }
    }
    return false;
}

这是 jsFiddle 上的旧工作版本,以便更好地理解

PS:pastebin 代码(unsafeWindow..)是我旧系统上唯一的工作版本。系统运行 FF 2.0.0.11 和 Greasemonkey 0.8 并且无法更新。

有没有办法消除这个错误?:D

4

1 回答 1

1

将 forms[0].submit 代码更改为:

if (unsafeWindow.document.forms[0]) {
    unsafeWindow._oldSubmit  = unsafeWindow.document.forms[0].submit;
    unsafeWindow.document.forms[0].submit = function () {
        GM_log ("Submit function fired.");

        var destInput   = document.getElementsByName ("dest");
        if ( ! destInput  ||  destInput.length == 0) {
            unsafeWindow._oldSubmit ();
        }
        else {
            var destValue   = destInput[0].value;
            if (    /^\s*$/.test (destValue)
                    ||  excludedDestinations.indexOf (destValue) > -1
            ) {
                GM_log ("Submit should be blocked! (1)");
            }
            else {
                unsafeWindow._oldSubmit ();
            }
        }
    };
}

在使用它们之前确保它们存在是一项正确的工作。

另外,取消注释此行:

//zEvent.stopPropagation ();
于 2012-10-19T01:48:44.460 回答