Can we simulate a key press without an event handler? - javascript

There has been a lot of discussions on Stack Overflow about how to dispatch a keyboard event programmatically with JavaScript. However, they are not simulating 'real' key presses in the sense that they merely fire a predefined event handler.
What I want is to simulate CTRL+F to bring up the browser search box. Is that possible at all?

window.find(…) does that.
In general, you're out of luck though trying to orchestrate native browser functionality from within a webpage. Browser extensions can do more.

Related

How can I get a WKWebView to show the keyboard on iOS?

My iOS app uses a WKWebView with contenteditable = true on a specific div. I'd like to have code to make the keyboard show up for the web view, so the user can just start typing. Things I've tried that have had no effect:
Telling the web view to becomeFirstResponder (a long shot, because the web view wouldn't know what div to use).
Injecting JS to tell the div to focus(). (This works in other browsers, but sadly not in WKWebView)
Simulating touch events in JS via TouchEvent and dispatchEvent() in the hope of making it seem that the user had tapped on the div.
In the third case I also used addEventListener() to observe the simulated touches and compare them to real touch events from tapping the screen. It looks like the key difference is that the event's isTrusted value is false for the simulated touches.
I get that it's a potential security issue to let apps simulate touch events, but I didn't have any other ideas. I'm trying to get the keyboard to appear, what the user types is up to them and not something I want to mess with. Basically I want the same thing as calling becomeFirstResponder() on a UITextView.
This is very similar to a WebKit issue 142757 but I haven't figured out how to use the suggested workaround linked from there.
Clarification: I can set up and use an editable web view, but the keyboard doesn't appear until I tap on the web view. I'm trying to make the keyboard appear automatically, without requiring a tap to initiate editing.
I tried this in an iPad playground, and it works without any action on my part. It’s possible there is another view that is capturing touches, or “contenteditable” is misspelled, or something else?

Simulate Shift+Esc combination using JavaScript

I made a Chrome extension which overrides 'New Tab' page. On the page, I created a clickable icon. I want to click and 'Task Manager' window to appear. Though I overridden the click-event handler (by simulating a keyboard event using KeyboardEvent() constructor) associated to that icon, nothing happens. This is the code:
event1=new KeyboardEvent('keypress',{
bubbles:true,
cancelable:true,
shiftKey:true,
code:'Escape',
key:'Escape'
});
window.dispatchEvent(event1);
return false; //Shift+Escape is Chrome shortcut for Task Manager
What am I doing wrong? I'd rather use Javascript.
The chrome task manager being such a low-level feature, I'd be surprised if you can invoke it by faking a keypress event even from the extension code. Key presses are filtered on the way down .. through the extensions and eventually to pages. This particular keypress is probably always intercepted at the highest level. It'd probably be undesirable to allow an extension or a page to know that the event occurred and it would certainly be undesirable to allow it to be intercepted. So I would think that Chrome isn't listening for the event to bubble back up before it invokes the window - it just does it and doesn't forward or listen for it bubbling.
Instead you'd need to ask Google for a special API call for the extension to call, proxied by the extension from any page code (if necessary). There doesn't seem to be an "chrome://tasks" even.

Is it possible to simulate browser click events?

How can I simulate exact behave of mouse click on a browser's button (e.g. Firefox)?
When user clicks on a button using mouse click then an event is generated. Could I use or call the event manually?
I can use Javascript to simulate click on a button but it will not call the same event in the same way as when a user really clicks on the button.
The reason I ask for solution: record engine for recording any event occur in the document level works in background, I want to create web app with self play functionality, that means when I browse the web app in browser it will do business flow automatically and then the engine will record the events.
Please ask if my question not clear, Thanks.
This is easiest with something like jQuery.
//listen for clicks - real or simulated
$('#some_element').on('click', function() { alert('click!'); });
//simulate clicks (two ways)
$('#some_element').click();
$('#some_element').trigger('click');
It is possible to know, from inside the event callback, whether the event was real or simulated. I did a blog post on this some months ago.
You should look into Selenium. It uses automation and other native APIs to simulate user input in exactly the way you wish.

How can I specify a hotkey for my browser action? [duplicate]

This question already has answers here:
Open Browser Action's Popup with keyboard shortcut
(3 answers)
Closed 5 years ago.
Is it possible to specify a hotkey that will activate a Google Chrome browser action?
No, you can manipulate almost every other aspect of the browserAction and the popup (including closing it) but it cannot be triggered programatically.
#hamczu is right that the only way to bind global keyboard shortcuts is to inject a Content Script that listens for keystrokes in every page.
However you will not be able to make those keystrokes (or anything else) trigger the browserAction.
I think you should look to Vimium project source. Global hotkeys are done by binding keyboard events in content script and communicate to background page. As authors say in Wiki there is no way "to add global keyboard shortcuts (without using a content script)".
Unfortunately I have found related issue in the bugtracker and it seems there is no way to so so.
The chrome.commands api enables the user to bind hotkeys (with your suggestion for the hotkey) that will trigger commands such as opening the browser action.
Duplicate of answer.

IE6 connection interruption in Comet streaming

I am using a forever frame (COMET streaming technique) and in IE6 whenever a user clicks on a link (to even just basic JavaScript method) the connection is immediately dropped and has to be manually refreshed.
Has anyone come across a similar issue and / or know how to address it?
How to address it: return false from your event handlers (event.preventDefault for listeners etc) so that the link is not followed and so no navigation occurs on a simple left-click. Put all your logic in event handlers attached from script (and not javascript: URL, which are a horrible fragile hack that should never be used).
Further: if it's just a button that does some scripting when clicked, and doesn't actually point to anywhere usefully navigable, it shouldn't be marked up as a link. Ideally it should be a button (input or button with type="button"), which you can then use CSS to style like a link rather than a button if you prefer.
(Another approach, that requires less styling work but has accessibility drawbacks, is to do what SO does and just put an onclick event on a <span> or <div>.)

Categories