Lose focus on Embededd/Flash element/tab in Firefox - javascript

Without AutoHotkey and Firefox -unfocus since it's involved in operating another program and it doesn't work on linux and without Restore Window Focus After Flash addon since doesn't give you control over flash tabs.
Did anyone succeeded in implementing this gBrowser.selectedTab.unfocus(); method and bind it to a keystroke in Firefox v3.6 ?
I've also tried (based on the addon above)
if (document.getElementsByTagName("EMBED").length == 0) {
return;
}
if (document.activeElement.tagName == "EMBED") {
document.activeElement.blur();
}
return;
But it doesn't work.
Also tried this JS code binded to a keystroke:
var file = Components.classes["#mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
var process = Components.classes["#mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
var args = ["-unfocus"];
file.initWithPath("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
process.init(file);
process.run(false, args, args.length);
Still no go.

Hi I am the author of unfocus extension.
Generally as far as I now you cannot simply bind keystroke in Firefox to unfocus plugin, because Firefox doesn't have focus. Plugin have full keyboard focus, so to unfocus plugin you must use external application.
That's the reason for creating such extension. It's the only way with the most simple implementation.
I run linux and almost every Window Manager I know support keybindings to commands. AutoHotkey is only one of the ways to achieve this on Windows. Because of this I always seen this extension as far more simple for Linux/BSD crowd.
Bind a keystroke to firefox -unfocus command.
If you use metacity (default WM of gnome):
gconf-editor -> / -> apps -> metacity -> global_keybindings and keybinding_commands
If you use kwin (kde):
http://maketecheasier.com/configure-custom-shortcuts-in-kde/2009/09/28
novell.com/coolsolutions/qna/11619.html
If you use xfwm (xfce):
Keyboard Settings -> Shortcuts
If you use openbox (lxde):
http://urukrama.wordpress.com/openbox-guide/#Key_mouse
Note that I don't use previously mentioned WM-s and DE-s, I just searched a little.
If you use tiling wm you should already know how to bind a keystroke.

Related

"Run all cells" command in Google Colab programmatically

I need to run certain command "Run all" from Google Colab menu "Runtime" programmatically. It does not have any obvious "onclick" eventHandler which I could call from javascript code on that page.
Other "divs" on the page are OK to be called from js, for exapmle, I can connect to runtime using js code:
document.querySelector('#top-toolbar > colab-connect-button').shadowRoot.querySelector('#connect').click();
Runtime menu is a dropdown menu and I tried to .click() every <div> item inside it but no effect.
Also "Run all" command has a hotkey Ctrl + F9 but dispatching event to the document element has no effect. But I can send Enter command to any input field inside the notebook with this code:
document.querySelector('input.raw_input').dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}))
Using Chrome code inspector Ctrl + Shift + I I looked inside "Run all" command and it looks like:
<div command="runall" class="goog-menuitem" role="menuitem" id=":1w" style="user-select: none;"><div class="goog-menuitem-content" style="user-select: none;">Run all<span class="goog-menuitem-accel">Ctrl+F9</span></div></div>
So I searched inside Sources tab of inspector code on the page and found occurrences of "runall" in https://colab.research.google.com/v2/external/external_polymer_binary.js file:
, Eja = X(new W({
id: "runall",
description: "Run all cells in notebook",
shortcut: IG(120)
120 - is a keycode of F9 button by the way. Also I found I think exact place where needed menu item is called:
case "runall":
d.runAll();
break;
but it's almost impossible for me to understand what is d. and where its reference!
Also I found many other interesting and useful commands like this.notebook.getKernel().isRunning() or c.notebook.getKernel().restart() but the question is the same all the time: what is the root object for those commands? I tried document. and window. but the result is "undefined" or "is not a function". I think that I could call runall() command in a string like:
document.**SOMETHING I DONT KNOW**.runAll()
I am very bad with frontend/js and its very difficult to find something in obfuscated code but if we have such function as .runAll() in javascript code which is connected to required menu item I thick it is possible to run it programmatically from console or javascript injection
Or maybe it is possible to dispatch a keyboard event Ctrl + F9 to some element in order to run this command thus the question is WHAT is the required object to dispatch the keyboard event
I spent a while combing through that javascript file for a similar reason, and finally figured out how to make this work.
Here's a function to programmatically run all cells:
function runAll() {
const F9Event = {key: "F9", code: "F9", metaKey: true, keyCode: 120};
document.dispatchEvent(new KeyboardEvent("keydown", F9Event));
}
Note that KeyboardEvent.keyCode is deprecated in favor of KeyboardEvent.code, but you still need to provide it here (as of 5/18/21) since it's the property Colab uses to check keyboard inputs.
You can also use metaKey: true and ctrlKey: true interchangeably, regardless of platform, since Colab just checks whether either KeyboardEvent.metaKey or KeyboardEvent.ctrlKey is present for shortcuts that require them.
Also I found many other interesting and useful commands like this.notebook.getKernel().isRunning() or c.notebook.getKernel().restart() but the question is the same all the time: what is the root object for those commands?
There's a global colab object that provides access to some (but not all) functionality. Most things are accessible through colab.global, e.g. to restart the kernel, you can use:
colab.global.notebook.kernel.restart()

Add (#host)listener for fullscreen change

I want to detect if the fullsceen mode is enabled by using the HTML5 fullscreen API. I'm using Chrome and Angular 4.x.
I've created an Angular component and added following #HostListener:
#HostListener('document:webkitfullscreenchange', [$event])
FSHandler(event: KeyboardEvent) {
console.log('fullscreen mode changed...');
}
This doesn't work for some reason. I've tried removing the browser prefix, capturing other events like webkitFullscreenEnabled, but all of that didn't help.
Any suggestions? Thanks.
You need to handle the fullscreenchange event for Browser/browserkit that the app will be on, and from most guides I've seen, there's four of them. This is how I've done it (I'm an Angular novice, so there may be a nicer / cleaner way),
#HostListener('fullscreenchange', ['$event'])
#HostListener('webkitfullscreenchange', ['$event'])
#HostListener('mozfullscreenchange', ['$event'])
#HostListener('MSFullscreenChange', ['$event'])
screenChange(event) {
console.log(event);
}
And that detects the change in every browser I've tried (Desktop ones on OSX, and mobile ones on Android).
I was able to do it in pure javascript
this.document.addEventListener('webkitfullscreenchange', () => {
console.log('fullscreen mode changed...');
})
You could also do something like
document.onfullscreenchange = () => console.log('fullscreenchange event fired!');
Although do not forget in that last example that if you get multiple objects where you have this line, document is a singleton. So be aware. I personally at a function where I modified a variable inside, but multiple instance of the object containing that callback existed, so the same document got overwritten.
But then if you want to do it the Angular way. You got the proper approach. I do have the same code as but my event is of type 'any'. I don't think the event is of type KeyboardEvent. That might be your problem.

JavaScript: How to clear my custom keyboard mappings and use the default ones

In my web application I define my own keyboard event handler and override some default key mappings:
document.onkeydown = function(eventParam)
{
var keycode;
keycode = eventParam.which;
// detect ESC key
if (keycode == 27)
{
//close the window
LightboxFileInfo.close();
}
return true;
};
However, at another point in my web application, i want to clear my custom key mappings and to use again the default ones. This is my problem. Could you please advise how to do just that? How to clear all my keyboard mappings and again to use the default ones without restarting the web-application?
Basically, my web application has numerous windows. The windows are lightboxes. For each window/lightbox I use different functions for the same keys. Remember, that I have a web application, not a website. It means, that everything occurs in one webpage/JavaScript Document, where I display different windows through the already mentioned lightboxes and not as different .html/.php files.
If I need to save the default keyboard mappings before changing them, then fine. Would you please advise how to do that? My JavaScript knowledge simply ends here. Of course, I look for the simplest solution.
I am looking for a solution using:
JavaScript
I do not use jQuery, just plain JavaScript.
Besides that, I use:
HTML 5
CSS 3
PHP 5.5.8
MySQL 5.6.15
Apache 2.4.7
Thank you in advance
Since you aren't disabling the default behavior with eventParam.preventDefault();, the normal defaults should work too in the first place. But to disable your own functionality from being called, you can just override the handler function with empty one
document.onkeydown = function() { };

Why does this simple login script work in Tampermonkey but not Greasemonkey?

I have this tiny little script that I run inside Chrome using Tampermonkey and works great.
However, when I use it in Firefox with Greasemonkey, it shows up on the active list, meaning its matching the page but it doesn't actually execute the code. I know it has to be a simple something I am overlooking but its not hitting me.
var myVar=setInterval(function(){myTimer();},100);
function myStopFunction()
{
clearInterval(myVar);
}
function myTimer()
{
var p1 = "Login";
var p2 = "mode=login";
var x = document.body.innerHTML;
if (x.match(p1) && x.match(p2)){
document.documentURI = "/ucp.php?mode=login";
}
myStopFunction();
}
Script Logic/Function
I am using a timer to prevent the script from triggering over and over in a permanent loop.
It simply detects if I am logged into a phpBB forum or not, if not send me to the login page so I can log in.
I am using document URI so that the location of the original is preserved so upon login, it takes me right back to it.
Often phpBB when you log in, it will take you back to the index page so this preserves my original intent of going to the actual link.
This script works perfectly and as expected on Chrome using TM but on Firefox using GM it doesn't trigger, am I missing something here?
From the Firefox spec:
(document.documentURI)
Returns the document location as string. It is read-only per DOM4 specification.
And, indeed, the latest spec still specifies that this attribute must be read only.
If Chrome lets you write this property, then that is non-standard behavior and maybe a bug.
Use location.assign(), or location.replace(), or just programmatically click the login button -- which often preserves the target page.

Monitor ajax calls from IE BHO

I'm trying to find a way to detect changes on a page from a BHO. For the Firefox version of the plugin I'm using DomNodeInserted, but this isn't available in IE. I've also looked at using onpropertychange, but this only allows you to monitor a single element (not children).
I'm now wondering if it's possible to monitor when AJAX requests are called. If so, I can make the changes to the page after the AJAX request has completed. Only problem is, I can't find a way to do it.
Here's my attempt so far, based on jeffamaphone's suggestions, it doesn't work but maybe it'll jog someone's memory.
public class ChangeMonitor : IHTMLChangeSink {
public void Notify()
{
Trace.WriteLine("notified");
}
}
void registerMonitor()
{
HTMLDocument document = _webBrowser2.Document;
ChangeMonitor monitor = new ChangeMonitor();
IHTMLChangeSink changeSink = monitor;
IHTMLChangeLog changeLog = null;
((IMarkupContainer2)document).CreateChangeLog(changeSink, out changeLog, 1, 1);
}
For IE, onreadystatechange will work as an equivalent to DomNodeInserted. A DHTML behavior must be attached to the element via htc, but the htc file does not have to exist:
document.documentElement.addBehavior("foo.htc");
document.documentElement.attachEvent("onreadystatechange", Notify);
The BHO can inject the script to handle the event.
I've never done it, but my theory is you can use IMarkupContainer2::CreateChangeLog().

Categories