Skip to content

Instantly share code, notes, and snippets.

@Sunappnio
Last active March 8, 2016 03:18
Show Gist options
  • Save Sunappnio/4a8a8ccd029bd1ab48e7 to your computer and use it in GitHub Desktop.
Save Sunappnio/4a8a8ccd029bd1ab48e7 to your computer and use it in GitHub Desktop.

Revisions

  1. Sunappnio revised this gist Mar 1, 2016. No changes.
  2. Sunappnio revised this gist Mar 1, 2016. No changes.
  3. Sunappnio revised this gist Mar 1, 2016. No changes.
  4. Sunappnio revised this gist Mar 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wxd.user.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @author [email protected]
    // @namespace https://claire.ml
    // @description prevent web wechat from exit unexpectedly
    // @include https://wx.qq.com/*
    // @include https://wx.qq.com*
    // @run-at document-end
    // ==/UserScript==

  5. Sunappnio created this gist Mar 1, 2016.
    29 changes: 29 additions & 0 deletions wxd.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // ==UserScript==
    // @name disable backspace key
    // @author [email protected]
    // @namespace https://claire.ml
    // @description prevent web wechat from exit unexpectedly
    // @include https://wx.qq.com/*
    // @run-at document-end
    // ==/UserScript==

    function forbidBackSpace(e) {
    var ev = e || window.event; //获取event对象
    var obj = ev.target || ev.srcElement; //获取事件源
    var t = obj.type || obj.getAttribute('type'); //获取事件源类型
    //获取作为判断条件的事件类型
    var vReadOnly = obj.readOnly;
    var vDisabled = obj.disabled;
    //处理undefined值情况
    vReadOnly = (vReadOnly === undefined) ? false : vReadOnly;
    vDisabled = (vDisabled === undefined) ? true : vDisabled;
    //当敲Backspace键时,事件源类型为密码或单行、多行文本的,
    //并且readOnly属性为true或disabled属性为true的,则退格键失效
    var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly === true || vDisabled === true);
    //当敲Backspace键时,事件源类型非密码或单行、多行文本的,则退格键失效
    var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
    //判断
    if (flag2 || flag1) return false;
    }

    document.onkeydown = forbidBackSpace;