0

I have a page with a few links.
Each link opens in a new tab.
The next time I click a given link, I want it to recall the same tab where it was previously opened.
Is it possible? How?
All pages come from the same domain.

I've seen this answer: Check if my website is open in another tab
It seems possible to store the URIs in localStorage, for repetitions detection.
I think this technique will allow me to detect repetitions, but I can't figure out how to aim at the specific tab (activate it, bring it to the front).
I don't control the server, need to implement it in the client (via Tampermonkey).

For the curious, I work a lot with BitBucket issues.
The page with the links is the issues listing, the other pages are the individual issues.
I want to be protected from opening the same issue twice, update both versions, and have a data loss.
Additionally, each page can have two different URIs, one for viewing and the other for editing. But I think I can solve it with JS.

Juan Lanus
  • 2,293
  • 23
  • 18

2 Answers2

2

Recalling a Browser Tab by Name

You can first specify a name for each tab you create, by using window.open():

let newWindow = window.open(newUrl, newWindowName);

or by using target attribute in an anchor tag:

<a href="http://my-new-url" href="myNewWindowName">Make New Window</a>

If you then call window.open with the name of an existing tab, that will use the existing tab. If the window by that name doesn't exist, window.open will create a new window (or tab).

MDN web docs page for Window.open


Bringing Window to the Front

Using window.open() alone may not be enough to bring the window to the front. That may be possible with a different function, window.focus(), which issues a request to bring the window to the front. Combining the two:

window.open(myUrl, myWindowName).focus();

Caution: A browser's user preference setting may still prevent focus() from bringing the window to the front, so this is not guaranteed to work. From MDN web docs:

It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

terrymorse
  • 6,771
  • 1
  • 21
  • 27
  • Nice! It seems feasible. I would have to take control of the links of the index page and replace them with `window.open` commands. – Juan Lanus Apr 19 '20 at 15:30
  • 1
    @JuanLanus I *think* you could just put the name in the links `target=` attribute. It's usually used for `target="_blank"`, but I think it does accept any name. – Tomáš Zato Apr 19 '20 at 15:43
  • @TomášZato-ReinstateMonica: will give it a try, sounds useful to simplify the needed code. Yes, it accepts anything, so I could use a repeatable slug to addres each issue page, something like "issue1234" (no "#" pls). – Juan Lanus Apr 19 '20 at 16:06
0

You can get the previous page's URL with the following:

let prevURL = document.referrer;

You can get the URLs from all open tabs with the windows.getAll() function.

From this answer - You cannot programmatically focus the browser to a specific tab due to security concerns. Consider updating the title to notify the user with document.title = {{new title}}

  • yes, I can not, but... If instead of a Tampermonkey script I write an extension, and enable the "tabs" permission, then I'll be able to use the windows object that you brought up at will. This is good! – Juan Lanus Apr 19 '20 at 16:25