2

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?

Michael Buen
  • 38,643
  • 9
  • 94
  • 118
  • @LukeBorowy should be able to open on a separate tab/window, yet on same window on succeeding run. A user might not or forget close the window dialog of Google translator, to ensure that there will be no multiple windows opened, the succeeding window.open should reuse the existing same name window. I remember, I tried closing the existing window before opening a new one, but it fails. So I resorted to same window name approach, it works on many websites, except for Google – Michael Buen Jul 13 '22 at 23:51
  • @seriously `window.open(url,'_blank');` will always open on a new tab/window. Need to open it on new tab/window on first run, yet opens on same tab/window on succeeding runs. See my comment to LukeBorowy above – Michael Buen Jul 13 '22 at 23:53
  • To be honest what you meant by “ open on a separate tab/window, yet on same window on succeeding run.” is kinda confusing. – seriously Jul 13 '22 at 23:57
  • @seriously I concur with your assessment, I now renamed the title to more explicit one: `How to open Google translator on a separate tab/window on first run, yet on same separate tab/window on succeeding runs`. See the use case I replied to @LukeBorowy – Michael Buen Jul 13 '22 at 23:58
  • Maybe save info onto cookies or local storage on first open then check if that info exist on next link open then if exists close that tab then reopen the link on new tab? – seriously Jul 14 '22 at 00:02
  • @seriously I also mentioned that in comment, I tried that approach first, but it fails. It's like a browser's security consideration for Chrome extensions code, hence the browser is restricting window.close on Chrome extensions. So I resorted to same window name approach, this approach works on many websites, but not on translator.google.com – Michael Buen Jul 14 '22 at 00:06
  • Does your project allow you to add a custom function to the translator page code? – seriously Jul 14 '22 at 00:09
  • 1
    This is quite a fascinating issue. I've been looking into this for probably the past 20 minutes or so. I've found that in Firefox, the first statement returns an object, but the second one returns `null`. In Chrome, both return an object, but the `window` property of the object in the second statement is null. It looks like for whatever reason Google Translate is loading in a way that the browser can't get a handle on the tab (potentially asynchronously), but DeepL is loading "normally". I'll keep digging and see what I can find. – Jesse Jul 14 '22 at 00:12
  • @seriously No, there is no custom/user-defined code, should have left out the word code in `Chrome extensions code`. It looks like the restricted `window.close` use is not even a browser's restriction on extensions. Closing a window is allowed only on same domain `To make this work, security-imposed cross browser compatibility requires that the window that is to be closed must have already been opened by the user clicking a button within the same site domain.` -- https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome/23679748#23679748 – Michael Buen Jul 14 '22 at 00:14
  • Is the Google page updating `window.name`? – Pointy Jul 14 '22 at 13:45
  • For what it's worth, I just added an event listener to the above comment on this page, having it open Google translate in a new tab with a name I invented. The tab opened, and subsequent clicks reload in that same tab; it does not open a new one. (I'm on Safari.) – Pointy Jul 14 '22 at 13:50

2 Answers2

0

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.

acran
  • 7,070
  • 1
  • 18
  • 35
  • Looks like a workaround is not possible. Got to accept the reality which Google made on their translation website. Can't automate Google Translate as a sidebar-like window or split-screen; every time it is opened from different origin, its COOP will just reject targeting the same window name – Michael Buen Jul 16 '22 at 03:38
-1

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")