j-delaney / back-to-backspace

https://chrome.google.com/webstore/detail/back-to-backspace/cldokedgmomhbifmiiogjjkgffhcbaec
MIT License
45 stars 14 forks source link

Not working inside shadowdom #30

Open benjaco opened 7 years ago

benjaco commented 7 years ago

It doesn't work inside a shadow dom element, because document.activeElement will select the most outer custom element insted of the input.

Googles own "back-to-backspace" uses "var target = event.path[0];" to get the correct element, it works even inside a shadow element.

Another more hack'y way to do it is to check if it is a shadow element, and look inside of it if it is (this code is not fully tested)

function getFocusElement() {
    var focusedElement = document.activeElement;
    var focusShadowDomElement;

    if (focusedElement == null) {
        return false;
    }
    while (focusedElement.shadowRoot !== null) {
        focusShadowDomElement = focusedElement.shadowRoot.activeElement;
        if (focusShadowDomElement == null) {
            break;
        } else {
            focusedElement = focusShadowDomElement;
        }
    }

    return focusedElement;
}

19 Is related to this issue