适用于浏览器会话之间的Chrome选项卡的持续性唯一ID [英] Persistent unique ID for Chrome tabs that lasts between browser sessions

查看:761
本文介绍了适用于浏览器会话之间的Chrome选项卡的持续性唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定某种方式来为Chrome标签建立符合以下条件的唯一ID:


  • 唯一标识每个标签

  • 在浏览器重新启动(会话还原标签)之间,给定选项卡保留相同标签

  • 如果选项卡关闭然后重新打开,

  • $ b
  • 如果选项卡重复,则保留不变

  • 我已经做了一些相当积极的研究,以找到一个全面的解决方案,但似乎没有什么相关的窍门。以下是我尝试过的方法,按照功能的增加顺序:使用Chrome提供的tab.id :b
    $ b

    • 在浏览器会话或关闭/撤销关闭之间不会持续存在
    • >
    • 在localStorage中放置一个GUID:在浏览器会话和关闭/撤消关闭之间持续存在,但不是每个标签唯一,仅限于每个域
    • 在sessionStorage中放置一个GUID:每个选项卡都是唯一的,在关闭/撤销关闭时保持不变,对于重复的选项卡是唯一的,但在浏览器会话之间被擦除

    • 使用可识别的网页文件属性作为唯一键:这是迄今为止我发现的最佳方法。可以通过以下值的内容脚本构造一个键: [location.href,document.referrer,history.length]



    关于最后一种方法,构建的键在所有共享通用URL,引用链接和历史记录长度的选项卡上是唯一的。对于浏览器重新启动/会话恢复和关闭/撤销关闭之间的给定选项卡,这些值将保持不变。虽然此关键字非常独特,但有些情况并不明确:例如,为 http://打开了3个新标签页www.google.com 都有相同的关键(这种情况在实践中经常发生)。



    将GUID放入sessionStorage方法还可用于在当前浏览器会话期间关闭/撤消关闭和重复选项卡的情况下使用相同的构造键为多个选项卡消除歧义。但是这并不能解决浏览器重启之间的模糊问题。 在会话还原期间,通过观察哪些标签Chrome在哪些窗口中一起打开,以及针对给定的不明确关键字推断哪个标签属于哪个窗口,可以部分缓解最后的不明确性出现预期的兄弟标签(在前一个浏览器会话期间记录)。正如你可能想象的那样,实施这个解决方案是相当困难的,而且相当不方便。而且它只能在Chrome恢复到不同窗口的同一键标签之间消除歧义。这留下了相同的键标签,恢复到同样的窗口,不可调和的模棱两可。



    有更好的方法吗?在浏览器重新启动(会话恢复)和关闭/撤消关闭之间保持唯一的,浏览器生成的每个选项卡GUID将是理想的,但到目前为止我还没有发现任何类似的内容。

    解决方案

    如果我正确理解你的问题,你的第5个方法应该做的伎俩,但随着这两个标准:


    • chrome.tabs.windowId (包含该标签的窗口的ID)
    • chrome.tabs.index (该窗口中该选项卡的从零开始的索引)



    所有这些值都需要存储在您的扩展中。除此之外,你还必须将你的扩展连接到 chrome.tabs.onUpdated(),并相应地进行更新,当标签被拖动,跨所有者窗口等移动时。


    I'm trying to ascertain some way to establish a unique ID for Chrome tabs that meets the following conditions:

    • Uniquely identifies each tab
    • Stays the same for a given tab between browser restarts (session-restored tabs)
    • Stays the same if a tab is closed and then reopened with Undo Closed Tab (Ctrl+Shift+T)
    • Stays distinct if a tab is duplicated

    I've done some rather aggressive research to find a comprehensive solution, but nothing seems to quite do the trick. Here are the methods I have tried, in increasing order of efficacy:

    • Use Chrome's provided tab.id: does not persist between browser sessions or close/undo-close
    • Put a GUID in cookies: is not unique per tab, only per domain/URL
    • Put a GUID in localStorage: persists between browser sessions and close/undo-close, but is not unique per tab, only per domain
    • Put a GUID in sessionStorage: unique per tab, persists across close/undo-close, unique for duplicated tabs, but is wiped out between browser sessions
    • Use identifiable webpage document attributes as a unique key: this is the best approach I've found so far. A key can be constructed via a content script from the following values: [location.href, document.referrer, history.length].

    Regarding this last approach, the constructed key is unique across all tabs which share a common URL, referrer, and history length. Those values will remain the same for a given tab between browser restarts/session-restores and close/undo-closes. While this key is "pretty" unique, there are cases where it is ambiguous: for example, 3 new tabs opened to http://www.google.com would all have the same key in common (and this kind of thing happens pretty often in practice).

    The "put GUID in sessionStorage" method can additionally be used to disambiguate between multiple tabs with the same constructed key for the close/undo-close and duplicated-tab cases during the current browser session. But this does not solve the ambiguity problem between browser restarts.

    This last ambiguity can be partially mitigated during session restore by observing which tabs Chrome opens together in which windows, and extrapolating for a given ambiguous key which tab belongs to which window based on the presence of expected 'sibling' tabs (recorded during the previous browser session). As you might imagine, implementing this solution is quite involved and rather dodgy. And it can only disambiguate between same-keyed tabs that Chrome restores into different windows. That leaves same-keyed tabs that restore into the same window as irreconcilably ambiguous.

    Is there a better way? A guaranteed unique, browser-generated, per-tab GUID that persists between browser restarts (session restores) and close/undo-close would be ideal but so far I haven't found anything like this.

    解决方案

    If I correctly understand your problem, your 5th method should do the trick, but along with these two criteria:

    • chrome.tabs.windowId (The ID of the window the tab is contained within)
    • chrome.tabs.index (The zero-based index of the tab within its window)

    All these values need to be stored inside your extension. Besides that, you will also have to hook up your extension to chrome.tabs.onUpdated() and updated accordingly, when tabs are being dragged around, moved across owner windows, etc.

    这篇关于适用于浏览器会话之间的Chrome选项卡的持续性唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆