How send CTRL+I from Tampermonkey script.
-
MajesticF last edited by
How send CTRL+I from Tampermonkey script to browser.
I try to send in Opera CTRL+I key combination to invoke a shortcut saved in the browser settings,
I have tried many methods, but no code wants to work.
If I click my icon I see Alert with text, but the key combination does not work.
icon.addEventListener('click', () => { var ciEvent = new KeyboardEvent("keydown", { key: "i", keyCode: 73, which: 73, code: "KeyI", location: 0, altKey: false, ctrlKey: true, metaKey: false, shiftKey: false, repeat: false }); // get the active element and dispatch the events to it var activeElement = document.activeElement; if (activeElement) { activeElement.dispatchEvent(ciEvent); alert('WORK OK'); } });
************************ code execution: const event = new InputEvent('input', { inputType: 'insertText', data: 'i', isComposing: false }); document.activeElement.dispatchEvent(event); console.log('InputEvent dispatched:', event); alert('WORK OK'); return: _app-b1721212d74c071b.js:27 InputEvent dispatched: InputEvent{isTrusted: false, data: 'i', isComposing: false, inputType: 'insertText', dataTransfer: null,…} and sending keys doesn't work. After pressing the keys manually return: Keydown event caught: KeyboardEvent{isTrusted: true, key: 'Control', code: 'ControlLeft', location: 1, ctrlKey: true,…} _app-b1721212d74c071b.js:27 Keydown event caught: KeyboardEvent{isTrusted: true, key: 'i', code: 'KeyI', location: 0, ctrlKey: true,…} _app-b1721212d74c071b.js:27 Ctrl+I detected!
It seems that the browser blocks the sending of keys with a script.
-
burnout426 Volunteer last edited by
@MajesticF said in How send CTRL+I from Tampermonkey script.:
It seems that the browser blocks the sending of keys with a script.
Yeah, I think synthetic events like that are blocked. I don't even think that will work with direct JS with a script in a web page, so it's probably not a Tampermonkey-specific issue.
-
MajesticF last edited by
@burnout426
Strangely, a similar script that sends the ALT and ENTER keys to a specific page (not the browser in general) works without a problem. I don't know where the error is.