如何根据页面中其他位置的文本停止某些输入值?

时间:2012-09-06 19:29:03

标签: javascript greasemonkey firefox2

我有一个完美运行的现有脚本! 它编码为 Firefox 2.x Greasemonkey 0.8.x ,我无法使用jQuery!

OLD WORKING SCRIPT:

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

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");
    oldInput[0].setAttribute ("disabled", "disabled");
}


  1. Here's a fiddle of how it looks/acts when the user is not in usersWhoCanSetPriority

  2. Here's a fiddle of how it looks/acts when the user has full access


  3. 新脚本示例:

    现在,如果存在 目的地 输入,则特权用户应该能够设置任何值。
    但是,应禁止非特权用户设置1222

    1. Privileged user, with Destination field

    2. Restricted user, with Destination field

    3. 即,如果用户 - > ' JOHN',' LUKE',' JEFF',' MAX',' ANDY' (从页脚)然后禁用prio输入,当用户输入" 12" 停止输入或" 22",并写一条错误信息。

      你必须知道史蒂夫("不允许的用户")被允许输入例如120,121,220,222和所有其他数字,但 12 22 !我不知道这是不是一个问题,因为12也是120! ' JOHN',' LUKE',' JEFF',' MAX',' ANDY'允许在输入中输入所有数字。

      旧脚本在site-one.html和site-two.html等不同页面上运行 但是,目标输入位于site-one.html和site-three.html上 - 这意味着有时我只有prio-input,有时我同时拥有prio-input和destination-input。

      我该怎么做?

      问候,Bernte

2 个答案:

答案 0 :(得分:3)

请参阅http://jsfiddle.net/WT9yZ/4/

您可以添加

if(bDisablePrio){
    var destExcluded=['12','22'];
    document.getElementById('dest').onchange=function(){
        if(destExcluded.indexOf(this.value)>-1){
            this.value='';
        }
    }
}

问题是,如果允许"122""12"不允许,则不能使用onkeypress事件以防止用户写入该事件,因为如果用户不能写"12",他也不能写"122"

然后,解决方案是onchange事件,当文本字段失去焦点(如果它已被修改)时会触发该事件。然后,如果用户只编写了"12""22",则可以清除文本字段。

答案 1 :(得分:1)

User user1653020's answer已接近,但它有几个问题:

  1. 它会在Greasemonkey 0.8中失败!见"Pitfall #2: Event Handlers"
  2. destination不存在时会崩溃。
  3. 它不是非常人性化。 (不要让用户知道她做错了什么。)
  4. 允许用户提交无效值。
  5. 转到jsfiddle.net/P2VeG,输入12作为目的地,然后按 Enter 或点击“提交”按钮。您将看到表单被发送到服务器,并且目标值无效,并且没有警告用户!

    以下代码应该克服所有这些问题。 (但由于显而易见的原因,它在GM 0.8和FF2中未经测试。)

    See the code in action at jsfiddle.net/P2VeG/2

    //--- 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]);
                },
                false
            );
    
            //--- Extra added to try and get FF2 to behave as later versions do.
            destInput[0].addEventListener (
                "keydown",
                function (zEvent) {
                    if (zEvent.keyCode == 13) {
                        if ( bCheckdestinationValue (destInput[0]) ) {
                            zEvent.preventDefault ();
                            zEvent.stopPropagation ();
                            return false;
                        }
                    }
                },
                true
            );
    
            destInput[0].form.addEventListener (
                "submit",
                function (zEvent) {
                    var destValue   = destInput[0].value;
                    if (    destValue === ''
                            ||  excludedDestinations.indexOf (destInput[0].value) > -1
                    ) {
                        //--- Stop the submit
                        /*--- According to:
                            http://www.webdevout.net/browser-support-dom#dom2events
                            preventDefault() and stopPropagation() are supported by FF2
                        */
                        zEvent.preventDefault ();
                        zEvent.stopPropagation ();
                        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);
            }
            //--- Uncomment this alert(), if all else fails in FF2.
            ////alert ('Destination cannot be 12 or 22.');
            return true;
        }
        else {
            var destErrNode = document.getElementById ("gmDestErrorDisp");
            if (destErrNode) {
                destErrNode.style.display = "none";
            }
        }
        return false;
    }