Auto-focus on first textbox

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

目前为 2015-01-30 提交的版本。查看 最新版本

// ==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);
        }
    }
}());

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址