Uniquely identify a duplicated Chrome tab

断了今生、忘了曾经 提交于 2021-02-07 07:58:40

问题


I am currently using window.sessionStorage to store a uniquely generated session ID. This ID is passed to all ajax calls to the server and persists across page navigation and reloads.

This seems to work perfectly for my target browsers, with one caveat: the duplicate tab functionality in Chrome.

Duplicating the tab copies the session storage to the new tab. When the new tab loads and communicates with the server, it now has the same "unique" identifier as the duplicated target tab.

Are there any ways that I can distinguish between the two duplicated tabs? Or are they truly duplicates and no information is different between the two?

Nothing persists after the tab/window is closed, so even a simple numeric tab id or anything would work as long as it is unique for that current instance.


回答1:


A variant of this worked for me:

https://stackoverflow.com/a/60164111/13375384

The trick is to only put the ID in session storage for the brief period of time when the tab is refreshing

In my TypeScript project, the solution looked like this:

const tabIdKey = "tabIdStorageKey"
const initTabId = (): string => {
  const id = sessionStorage.getItem(tabIdKey)
  if (id) {
    sessionStorage.removeItem(tabIdKey)
    return id
  }
  return uuid()
}
const tabId = initTabId()
window.addEventListener("beforeunload", () => {
  sessionStorage.setItem(tabIdKey, tabId)
})



回答2:


Session storage is on the browser level, and not on the tab level. See https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage .

The only method I know of to identify a tab is to pass a paramater at the end of each URL (and even so you would need to use JavaScript to prevent open in new Tab).

Other methods would be unbearable, such as embedding an iframe in the app (and storing the identifier in the parent), but possible.

In short, you should rephrase you question to simply identifying a tab, and you will nevertheless not find an answer.




回答3:


My eventual solution to this was to generate a unique ID on the server side which gets stored in the server session and passed back to be stored in sessionStorage. I also have another value (let's call it FOO) that is set on the client side and stored. Pretend it looks like this in a map:

{
    "unique_id" : "ABC123",
    "FOO" : "some value"
}

I send the values up to the server with every ajax request.

On page load, I check to see if I already have the unique_id, FOO pair in sessionStorage and load them. Any time the value of FOO changes, it will get compared with the server pair. If it has a different value then I store the new value for FOO under a new unique ID and hand it back to the client.

This doesn't 100% solve the problem, but it does make sure that FOO, which represents some persistent state, is never used incorrectly after tab duplication. The tab that originally had had the unique_id, FOO pair will continue to have that pair while the duplicated tab will have a new pair.

Technically, until the value of FOO changes the two tabs share the same unique_id, but as long as unique_id gets reassigned when FOO changes, they won't overwrite each other's state within the server session.



来源:https://stackoverflow.com/questions/28752524/uniquely-identify-a-duplicated-chrome-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!