3

Is is possible in general to make Chrome not to focus on any of the fields automatically, for example, after the page has been loaded?

Extensions that use keys on keyboard don't work well with autofocus and type instead of executing commands.

6

I've just written script for you:

// ==UserScript==
// @name           Disable auto-focussing
// @author         ComFreek <comfreek at the following domain 'outlook' with the TLD 'com'>
// @description    Disable auto-focussing
// @include *
// @version        1.0
// ==/UserScript==

var maxTime = 3000;
var timeoutInterval = 5;

var usedTime = 0;
var isManualFocus = false;
function check() {
    if (!isManualFocus && document.activeElement.tagName.toLowerCase() == "input") {
        console.log("BLURRED");
        document.activeElement.blur();
    }
    usedTime += timeoutInterval;
    if (usedTime < maxTime) {
        window.setTimeout(check, timeoutInterval);
    }
}
check();


document.body.addEventListener("click", function (evt) {
    if (evt.target.tagName == "INPUT") {
        console.log("MANUAL CLICK");
        isManualFocus = true;
    }
});

document.body.addEventListener("keydown", function (evt) {
    isManualFocus = true;
});

Warning The script will interfere with the user if he immediately starts typing while the script is still operating. This is fixed.

Installation (manual method)

  1. Save the script as XX.user.js (XX can be any string, but .user.js is important here!)

  2. Open the extensions page in Chrome (the URI is chrome://extensions/ as of Chrome v31)

  3. Drag the script from the file explorer and drop it over the extensions page.

  4. Confirm the installation

Installation (TamperMonkey)

My script should work with TamperMonkey according to OP's comment below. Please consult TamperMonkey's manual for further information on how to install my script.

5
  • It works great with TamperMonkey as well. Thank you a lot and Happy New Year! – Anton Tarasenko Dec 28 '13 at 13:57
  • You're welcome! Happy new year, too :) – ComFreek Dec 28 '13 at 15:24
  • Would it be possible to tailor this script to focus the address bar, via superuser.com/questions/1036326/override-focus-events-in-my-browser/1036330?noredirect=1#comment1447268_1036330 ? – Wolfpack'08 Feb 6 '16 at 20:16
  • @Wolfpack'08 Do you mean the browser's address bar? AFAIK this is impopossible within the scope of a user script. – ComFreek Feb 6 '16 at 21:07
  • 1
    I tried a lot of solutions and yours is the only one that interacts with my pages. +1'd. I changed the interval down to 500ms as 3000ms was too long for how fast I click on things once a page is loaded. – Shadoninja Sep 9 '16 at 17:16
0

Small riff on ComFreek's excellent solution above, that also immediately removes focus from any input fields when a tab is focused. This is particularly useful for apps like Google Sheets, which automatically focus on an input field when you refocus the tab. I think this is a slight improvement for the original use case specified by the question (being able to use extension keyboard shortcuts).

var maxTime = 3000;
var timeoutInterval = 5;

var usedTime = 0;
var isManualFocus = false;
function check(force = false) {
    if (force || (!isManualFocus && document.activeElement.tagName.toLowerCase() == "input")) {
        console.log("BLURRED");
        document.activeElement.blur();
        window.blur();
    }
    usedTime += timeoutInterval;
    if (usedTime < maxTime) {
        window.setTimeout(check, timeoutInterval);
    }
}
check();

document.addEventListener('visibilitychange', function(evt){
    if (document.visibilityState === 'visible') {
        check(true)
    }
})

document.body.addEventListener("click", function (evt) {
    if (evt.target.tagName == "INPUT") {
        console.log("MANUAL CLICK");
        isManualFocus = true;
    }
});

document.body.addEventListener("keydown", function (evt) {
    isManualFocus = true;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.