Tab输入

按TAB键在输入框之间快速切换

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Tab输入
// @name:en      Tab-Input
// @namespace    http://css.thatwind.com/
// @icon         https://using-1255852948.cos.ap-shanghai.myqcloud.com/auto_tab-icon.png
// @description:en Switch to input by Tab button.
// @version      0.1.3
// @description  按TAB键在输入框之间快速切换
// @author       遍智
// @match        *://*/*
// @grant        none
// @run_at       document_end
// ==/UserScript==

(function() {

    console.log("auto_tab");

    var all_input=[]; // 所有可见输入框
    
    var now_target=null; // 当前聚焦输入框

    setInterval(refresh,50);    

    function refresh(){
		
        // 刷新获取所有可见输入框 刷新当前聚焦输入框
        var input_ls=document.querySelectorAll("input[type=text]:not([disabled]),input[type=password]:not([disabled]),input[type=number]:not([disabled]),input[type=search]:not([disabled]),input:not([type]):not([disabled])");
        var input_ls_re=[];
        for(var i=0;i<input_ls.length;i++){
            if(input_ls[i].clientWidth>0&&input_ls[i].clientHeight>0) input_ls_re.push(input_ls[i]);
        }
        all_input=input_ls_re;
        if(document.querySelector("input:focus")) now_target=document.querySelector("input:focus");
        else if(getIndex(all_input,now_target)==-1) now_target=null;
        
        // console.log(all_input,now_target);
    }



    document.body.addEventListener("keydown",function(e){ // tab键事件
        if(e.keyCode===9){
            e.preventDefault();
            refresh();
            var new_index;
            if(now_target&&now_target.clientWidth>0&&now_target.clientHeight>0){ // now_target存在
                new_index=getIndex(all_input,now_target)+1;
                if(new_index>=all_input.length) new_index=0;
            }
            else new_index=0;
            if(all_input.length<1) return;
            trigger(document.body,"mousedown");
            trigger(document.body,"mouseup");
            trigger(document.body,"click");
            all_input[new_index].click();
            all_input[new_index].focus();
            refresh();
        }
    });

    document.body.addEventListener("click",function(){
        refresh();
    })

    function getIndex(e_arr,e){
        for(var i=0;i<e_arr.length;i++){
            if(e_arr[i]===e){
                return i;
            }
        }
        return -1;
    }


    function myDelegate(p,e,s,f){p.addEventListener(e,function(ev){var targetNow=ev.target;while(targetNow!=p){if(indexOf(p.querySelectorAll(s),targetNow)!=-1){f(ev,targetNow);break;}targetNow=targetNow.parentNode;}});function indexOf(arr,e){for(var i=0;i<arr.length;i++){if(arr[i]===e)return i;}return-1;}}

    function trigger(elem,event){
        if(document.all) {
            elem.event();
        } else {
            var evt = document.createEvent("Events");
            evt.initEvent(event,true,true);
            elem.dispatchEvent(evt);
        };
    }


})();