0

I've been trying to open a "popup" [1] without it taking the focus automatically.

I tried to blur it, from the opener or from the popup itself unsuccessfully. I also looked for pop-under but didn't find anything relevant on Google about both topic.

I'm using Firefox. (Well… Palemoon 24.7.1 for x64 actually)

My goal is to be able to open a bunch of tabs (with middle click for instance) and having them closing on their own a bit later.

To do so in Firefox, and afaik, you MUST use a popup (right?). But everytime I open a new tab (with middle click) it focuses on it which is very annoying.


[1] Or anything else that could match my expectations.

EDIT: NOTE: This is for personnal use. :)

jeromej
  • 10,508
  • 2
  • 43
  • 62
  • You are trying to what all of the pop up ads did in the 90's! So `window.focus()` did not work? Show what you tried. – epascarello Sep 22 '14 at 13:36
  • 1
    What are you trying to accomplish with these popups? It's up to the user to decide to close a webpage/browser/popup, not you. This sounds like a bad practice from the start – hotforfeature Sep 22 '14 at 13:37
  • @abmitchell It's for personal use :) I forgot to mention it, thank you! I'm trying to launch a bunch of downloads on their own and having them close on their own too! – jeromej Sep 22 '14 at 13:38
  • Definitely try for it if you want to, but you could accomplish a lot more with a download manager than a script to try and close tabs. I'm not sure if it's even possible to automatically close tabs in Firefox – hotforfeature Sep 22 '14 at 13:40
  • @epascarello Mainly Googling a lot and didn't find anything relevant or working. 98% of them were about `focus` and `blur`. Wait… Are those suppose to work?! – jeromej Sep 22 '14 at 13:40
  • @abmitchell Well, I did manage to do so… but with popup only, which have that annoying thing they like to do. Which ruins all the point. – jeromej Sep 22 '14 at 13:42
  • Of course a Firefox *extension* can close a tab: [`window.BrowserApp.closeTab(tab);`](https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/BrowserApp/closeTab) – Makyen Sep 23 '14 at 18:59

2 Answers2

1

First the easy one: For mouse-middle-click: In Tools->Options on the "Tabs" tab, there is an option "When I open a link in a new tab, switch to it immediately". After un-checking this option, links followed by selecting "Open in new Tab", or mouse-middle-click will open in a new tab without being focused. On the same option tab, you will also want "Open new windows in a new tab instead" checked (first checkbox).

This will work for most normal links. Links which are actually JavaScript code snipits will not work in a new tab because they rely on the code existing in the current page.

A change that I find useful in Firefox is to have the cursor change depending on what type of link it is hovering over. This will allow you to visually distinguish, at a basic level, what will happen when the link is clicked without having to look at the destination address. I originally found this at askvg. It is an addition to the file <profile directory>/chrome/userContent.css (create the directory and file if they do not exist):

/* Change mouse cursor for hyperlinks that open in a new window or tab */
:link[target="_blank"], :visited[target="_blank"],
:link[target="_new"], :visited[target="_new"] {
cursor: crosshair;
}
/* Change mouse cursor for JavaScript links */
a[href^="javascript:"] {
cursor: move;
}

/* Cursor types
default - Normal select cursor
text - Text select cursor (I bar)
vertical-text - Vertical text select cursor
progress - Working in background cursor
wait - Busy cursor
help - Help cursor
crosshair - Precision select cursor
move - Move cursor
no-drop - Unavailable cursor
not-allowed - Unavailable cursor
e-resize - Horizontal resize cursor
n-resize - Vertical resize cursor
nw-resize - Diagonal resize 1 cursor
ne-resize - Diagonal resize 2 cursor
col-resize - Column resize cursor
row-resize - Row resize cursor
*/

Beyond that, it is unclear in what context you want to do this in, or at least to what extent you are willing to go to accomplish this. Your mentioning having windows/tabs open in the background and closing on their own implies that you do not actually need the user to view the window/tab. Is it that you just want a request to have been made of some URLs? Would using XMLHttpRequest be sufficient?

You want this for yourself, so if you go the route of a Firefox extension then it is quite easy to open tabs and windows and not have them be focused. It can be done with addTab().

It would be helpful for you to describe what it is that you are overall attempting to accomplish and the context in which you are doing so.

Additional information:

Based on the additional information which you have described what you need is to write a Firefox extension which can give you complete control of popups and tabs to have them work the way you desire. For what it sounds like you desire you should see (at least) the following Mozilla documentation:

Copying an example on Tabbed browser:

// Add tab  (without it becoming active)
gBrowser.addTab("http://www.google.com/");

// Add tab, then make active
gBrowser.selectedTab = gBrowser.addTab("http://www.google.com/");

From Tabbed browser:

Opening a URL in the correct window/tab

There are methods available in chrome://browser/content/utilityOverlay.js that make it easy to open URL in tabs such as openUILinkIn and openUILink.

openUILinkIn( url, where, allowThirdPartyFixup, postData, referrerUrl ) where:

  • "current" current tab (if there aren't any browser windows, then in a new window instead)
  • "tab" new tab (if there aren't any browser windows, then in a new window instead)
  • "tabshifted" same as "tab" but in background if default is to select new tabs, and vice versa
  • "window" new window
  • "save" save to disk (with no filename hint!)

Also from Tabbed browser, an example of code for an overlay extension which

will open a URL in a new tab, an existing tab, or an existing window based on which mouse button was pressed and which hotkeys (ex: Ctrl) are being held. The code given is for a menuitem, but will work equally well for other XUL elements. This will only work in an overlay of browser.xul.

XUL:

<menuitem oncommand="myExtension.foo(event)" onclick="checkForMiddleClick(this, event)" label="Click me"/>

JS:

var myExtension = {
  foo: function(event) {
    openUILink("http://www.example.com", event, false, true);
  }
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Your first trick doesn't work when middle-click opens a popup though. Popups are popups, not really new tab. I have that option unchecked already. Actually, yes, the user doesn't need to see the tab, I already used some userscript so that the download starts on its own, my goal was that those tabs would then close themselves after the download started. It works. But now I have that "focus problem". I might have a look into extensions but I wanted to keep something "easy to do, easy to setup" kind of deal. Thank you for your reply. – jeromej Sep 23 '14 at 15:20
  • (Furthermore I need the JS in the page to be executed because I don't want to reverse engineering the obfuscated js code of those download websites) – jeromej Sep 23 '14 at 18:09
  • Add [Mozilla Developer Network](https://developer.mozilla.org/en-US/Add-ons) links and code examples taken from there. – Makyen Sep 23 '14 at 19:27
  • I'm not interested in that solution: too hard to implement, I wanted something easy. Thank you for the info though, now I know it's possible and how if I ever change my mind or need it for something else. – jeromej Sep 26 '14 at 18:29
0

In the end, I think I used this technique to get more or less what I wanted to achieve:

The opened tab ("popup") would communicate with a webserver (in my case, in Python) thanks to a JavaScript loaded on it by using an addon like GreaseMonkey.

The server would then tell the popup when to close itself: Worked great!


EDIT: I just thought of an alternative way of doing it (faking it) that might or might not suit your needs (depends on your app).

Open a "popup" (new tab) with the current URL (that's where it might do the trick for you or not) then change the URL of your first page to the "popunder" you were looking for.

jeromej
  • 10,508
  • 2
  • 43
  • 62