Chrome Extension - first link is auto-focused in popup

后端 未结 6 1302
感动是毒
感动是毒 2021-02-07 11:18

How do I stop my Google Chrome extension\'s default action to auto-focus the first link in my popup.html? I know I could probably do some roundabout hack with JS o

6条回答
  •  我寻月下人不归
    2021-02-07 11:49

    This is the best solution to the problem. The tabindex="-1" solution harms user experience, and as opposed to @link0ff's solution, this one removes the focus instantly.

    This element should be the first focusable element in the DOM:

    
    

    This simply removes the button once it's been focused:

    function removeAutoFocus(e) {
        if (e.target.classList.contains("invisible-button")) {
            e.target.style.display = "none";
            document.removeEventListener("focus", removeAutoFocus);
        }
    }
    document.addEventListener("focus", removeAutoFocus, true);
    

提交回复
热议问题