Auto-focus on first textbox

Auto-focuses on the first/main textbox of a site

当前为 2015-01-30 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Auto-focus on first textbox
// @namespace   http://userscripts.org/users/23652
// @description Auto-focuses on the first/main textbox of a site
// @include     http://*
// @include     https://*
// @exclude     file://*
// @copyright   JoeSimmons
// @version     1.0.3
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

/* CHANGELOG

    1.0.3 (1/30/2015)
         - added some small delays for YouTube for it to work
         - removed the use of JSL; it's unnecessary

*/

(function () {
    // make sure the page is not in a frame
    if (window.frameElement || window !== window.top) { return; }

    var domain = window.document.domain,
        rDomain = /([a-z0-9]+\.)?([a-z0-9-]+(\.[a-z0-9]+)+)$/,
        site = '',
        delay = 99, // by default, use a small delay to make sure the site's onload stuff is done
        e = document.querySelector('input[type="text"], input[type="search"], textarea'),
        len;

    // grab the domain minus the subdomain
    if ( domain.match(rDomain) ) {
        site = domain.match(rDomain)[2];
    }

    // this section is for sites that require special attention ;)
    switch (site) {
        case 'youtube.com': {
            e = document.querySelector('#masthead-search-term');
            delay = 750;
            break;
        }
        case 'userscripts.org': case 'google.com': {
            e = document.querySelector('input[name="q"]');
            break;
        }
        case 'facebook.com': {
            e = document.querySelector('textarea[name="xhpc_message_text"], textarea[name="xhpc_message"]');
            delay = 750;
            break;
        }
    }

    // if the element exists and is visible (in-view), then proceed
    if (e && Math.max(e.offsetWidth, e.offsetHeight) > 0) {
        len = e.value.length;

        if (typeof e.focus === 'function') {
            // some sites require a delay
            window.setTimeout(function (element, length) {
                // focus the text box
                element.focus();

                // highlight its text
                if (length > 0 && typeof element.setSelectionRange === 'function') {
                    element.setSelectionRange(0, length);
                }
            }, delay, e, len);
        }
    }
}());