浏览器重新启动后,在Chrome扩展中重新建立标签身份 [英] Re-establishing tab identity in a chrome extension after browser restart

查看:146
本文介绍了浏览器重新启动后,在Chrome扩展中重新建立标签身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Chrome扩展程序将一些数据保存在localStorage中,并且需要以每个选项卡为基础执行此操作。使用我从API获得的标签ID可以让我以我需要的方式保存数据。不幸的是,如果浏览器重新启动,选项卡似乎会得到不同的ID,并且我的方案分崩离析。有没有一种方法可以在重新启动的情况下识别标签? URL不够,因为相同的URL可能出现在不同的标签中,不应该导致这些标签混淆。

My Chrome extension saves some data in localStorage, and it needs to do this on a per-tab basis. Using the tab id I get from the API allows me to save the data in a way I need. Unfortunately, if the browser is restarted, tabs seem to get different ids, and my scheme falls apart. Is there a way to identify tabs in a way that survives restarts? The URL is not sufficient, as the same URL can appear in different tabs and should not cause those tabs to be confused.

这是什么模式?

推荐答案

我也有很大的问题。最后,我通过为Chrome创建一个新选项卡设置侦听器来解决此问题。看看下面的代码:

I had big problems with this too. Finally I resolved it by setting a listener for Chrome creating a new tab. See the code below:

chrome.tabs.onCreated.addListener(function(tab) {
    //Callback function gives you the tab that was created.
    var tabId = tab.id;
    //Or, in my case I just added it here to an array of tab ids.
});

我个人保留了一些我需要的标签ID,并根据需要添加和删除它们。请参阅此文档,以获取与选项卡相关的所有帮助信息。

I personally kept an array of tab ids that I needed and added and removed them as needed. See this documentation for help with everything to do with tabs.

这篇关于浏览器重新启动后,在Chrome扩展中重新建立标签身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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