How to check if non-chrome window is focused Chrome Extensions API - javascript

I'm working on a chrome extension that tracks the active chrome tab and chrome window, but I want it to know when the active window is a non-chrome window.
When using a listener for chrome.windows.onFocusChanged, the listener only triggers when the new focused window is a chrome window or if all chrome windows are minimized. It does not trigger if you maximize or alt-tab into another non-chrome window. Is there a solution for this or is this problem out of scope for the Chrome Extensions API?

This can be achieved (partially) with the Page Visibility API. document.visibilityState will return the value of hidden when the current tab is covered by another maximized program window, or when the tab is not visible.
You can add this to your content scripts:
var string = document.visibilityState;
Or as an event listener:
document.addEventListener("visibilitychange", function() {
console.log( document.visibilityState );
// Modify behavior...
})
Unfortunately, document.visibilityState is not accurate for all programs I've tested (such as Steam or Windows Settings), and it will only return the value of hidden when the chrome tab is completely covered from view, not partially.

Related

Chrome/Safari: how to open the same window from different tabs or windows

In a web application, I have an optional popup window into which logging content is being loaded. This window is opened via Javascript using window.open().
The users typically have multiple tabs of the application open and my intention is to always reuse the same popup window (if currently open), no matter from which tab they trigger the popup. To do so, I use code similar to this to open the popup:
<script>
function myFunction() {
var myWindow = window.open("popup.html", "my_popup", "width=600,height=400,status,resizable");
}
</script>
This works fine in Firefox and IE - only a single popup window is ever opened and all content that I want to log is being loaded into that window.
Chrome and Safari however, use different popup windows depending on the tab from which the popup is launched.
I assumed that the second parameter of window.open() specifies a target name from a global namespace, which seems to be the case for most browsers. But Chrome and Safari behave as if there were tab-specific namespaces for these target names, i.e. "my_popup" referenced from tab 1 refers to a different target than "my_popup" from tab 2. (to make things more complicated, tabs that have been "duplicated" seem to share the same namespace, i.e. they do reuse the same popup window, but tabs created otherwise don't.)
Is there a way to circumvent this behavior of Chrome and Safari and access the same popup window from all tabs that the user may have open?
Or do I have to assume that this is browser-specific behavior for which there seems to be no workaround?
I have tested this with various browsers:
IE 11.0.9600.18204
All tabs with content from the same domain open/reload a single shared popup window (i.e the target namespace seems to be local per domain).
All tabs with content from file:// URLs reuse the same popup window as tabs with content from http:// URLs that point to the local intranet.
Firefox 46.0.1
All tabs with content from the same domain open/reload a single shared popup window (i.e the target namespace seems to be local per domain).
All tabs with content from file:// URLs use their own single shared popup window (i.e. file:// URLs seem to have their own target namespace).
Edge 20.10240.16384.0
The behavior is inconsistent: in most of my tests, Edge behaved like Firefox, but occasionally like Chrome as well.
I don't have a problem in ignoring Edge for the time being.
Chrome 50.0.2661.94
Each tab opens or reloads its own popup window (i.e the target namespace seems to be local per tab)
Safari 9.1
Each tab opens or reloads its own popup window (i.e the target namespace seems to be local per tab)
By the way, in all of these browsers the behavior does not depend on whether or not the popup window was launched due to user interaction: the behavior is identical between cases where the popup is launched via body onload() and cases where it is loaded via button onclick().
[here's additional background information that I assume not to be relevant to the problem, but who knows... ;-)
Actually, our application does not directly open the popup windows, instead we're using log4javascript which opens a logging window (or in case of Chrome: multiple logging windows). I'm willing to extend the log4javascript code to deal with this and send a patch to the maintainer, but in order to do so I need to solve the basic problem described above]

Open new Firefox tab with JavaScript with expected behavior

When a user clicks a link in my application, I need to "enrich" the clicked URL with some additional context (depending on the interaction) and then proceed as normal.
"As normal" means to set the window location to the new URL, or to open a new tab if a meta key is held while clicking.
This works as expected in all modern browsers except Firefox; in Firefox, the tab is always focused, regardless of what's set in Preferences > Tabs > "When I open a link in a new tab, switch to it immediately".
I can't just let the MouseEvent through, because I need to direct to a different URL than that on the clicked <a href>; I have to stop propagation of the click event.
In short, how can I get window.open('url', '_blank') to respect the user's preferences and any meta keys in Firefox?
EDIT: My question differs from Open a new tab in the background? in that my need is for Firefox to work like other browsers; Chrome is working fine here. Also, I don't want the target page to always open in the background, I want it to open in another tab on a ctrl+click/cmd+click/middle-mouse click, or in the current window on a non-meta click, and to respect user prefs for opening focused or in the background. Updated the title to address this.

chrome extension "chrome.windows.onFocusChanged.addListener" not fired

I think it's a bug, just wanted to make sure that I'm doing it right
this is in my bg script
background.js
chrome.windows.onFocusChanged.addListener(function(windowId) {
console.log("Focus changed.");
console.log(windowId);
});
event is fired when Chrome is in window mode and minimize button is clicked(-1) or when switching focus between Chrome windows (including background page inspect window), but if I toggle Chrome window visibility from win7 task-bar clicking on "badge" (restore or minimize it), nothing happens.
Also if some other window/app takes focus, event is not fired (when Chrome window goes back, or to front)
Is there a solution for this or... it is a bug?

Automatically close a browser window when the user clicks outside it

Is it possible to automatically close a web browser window (e. g., in Google Chrome) when the user clicks outside the window? In order to do this, there would need to be some method for detecting whether or not the window was focused, and also a method for closing the window.
Most current browsers only let you close the child windows of a parent.
You can't close the master parent window through a script.
By default most browsers do not allow javascript to close windows that were not opened by javascript itself. But if you use e.g. window.open(...) you can close that window with window.close().
So if you have a window opened by javascript you can do the following:
window.addEventListener('blur', function(){window.close();}, false);

resizeTo() not working in Firefox

Running Firefox 12.0. I'm just starting to write a app that I want to run on localhost. I want it to open it in a (600,400) window, but since I have Firefox set to open everything in tabs I thought I could bookmark it and right-click to open it in a new window.
$(document).ready(function() {
window.resizeTo(600,400);
});
Doesn't work. Is what I am trying to do possible?
Thanks, Jim
This isn't possible, unless the window was opened through javascript using window.open and has only 1 tab in it.
Since Firefox 7, it's no longer possible for a web site to change the default size of a window in a browser, according to the following rules:
- You can't resize a window or tab that wasn’t created by window.open.
- You can't resize a window or tab when it’s in a window with more than one tab.
MDN docs

Categories

Resources