How to open Google translator on a separate tab/window on first run, yet on same separate tab/window on succeeding runs

Viewed 73

DeepL (and many other websites) can be opened on a separate tab/window on first run, yet on same separate tab/window on succeeding runs. That is the objective

window.open("https://www.deepl.com/translator", "sameD");

Google Translator cannot, it always open in new tab on succeeding runs, even if the window name is same

window.open("https://translate.google.com", 'sameG');

Why is Google Translator not opening in same tab, does it explicitly disallow it? And how it does that?

Or is there another parameter in window.open to ensure a url can be opened in same separate tab/window?

2 Answers

Not a solution but an explanation for what you are experiencing:

As you already know

window.open("https://translate.google.com", 'sameG');

will open a new window/tab with the name sameG. The call will also return a proxy to the window object of that page.

Also this will set window.opener in the new window to a proxy of the window of the calling window.

Usually calling window.open() again with the same window name will update the previously opened window to navigate to the new URL -- as you do see with the deepl.com example.

But translate.google.com sends a

Cross-Origin-Opener-Policy: same-origin-allow-popups

header! This header is indeed a browser security header and it isolates the browser context of the new window, i.e., the return value of window.open() won't give you any access into the newly opened window and window.opener therein is set to null.

Additionally this loses the reference to the window name sameG and thus subsequent calls to window.open() will again open a new window instead of updating the already opened one.

TL;DR: The Cross-Origin-Opener-Policy is the reason for this.

The second parameter window.open() method takes is the target parameter which is the name of the browsing context the resource is being loaded into. You can use the special target keywords, in this case it'll be "_self":

window.open("https://translate.google.com", "_self")
Related