I am trying to use a bookmarklet to modify the content of the currently-active textbox.
If I write let a = document.activeElement; console.log(a), then in my console, I see the currently-active textbox correctly printed to the console. If, in the console, I then write a.value = "hello", the content of that textbox changes exactly as expected.
If I write let a = document.activeElement; a.value = "hello" in my bookmarklet, however, it replaces the entire document with the text node "hello." Same with document.activeElement = "hello".
I am guessing that once the bookmarklet is clicked, the entire document or browser window becomes the activeElement. But if that's true, I'm not sure why the correct node would be referenced and preserved in the first case. Nor can I find any way to preserve the reference to do anything with outside of the console.
Is it possible to do this?
I'm not sure it's possible to detect focus after it has been changed, in this case, by clicking the bookmarklet. Although more annoying, you could check for focus changes with the onfocus event, and only then use document.activeElement.