Chrome 扩展,使用 localStorage 保存每个选项卡的 ip、tabid、serverfingerprint
我应该提前提到我是代码/stackoverflow 的新手,所以如果这个问题没有意义,我很抱歉。我很困惑,我正在尝试构建一个 chrome 扩展来保存 ip 地址、url 和服务器指纹。服务器指纹是位于响应标头中的字段。使用我的background.js 和localStorage,我可以保存此信息,然后将其显示在弹出窗口中。这一切都很好,除了我不知道如何在每个选项卡的基础上保存它,又名...如果我打开了 5 个选项卡,我想单击我的扩展程序并让它显示每个选项卡的 url相应的选项卡。例如:单击 tab4 并显示 tab4 的 url,然后单击 tab2 并显示 tab2 的 url。
下面的代码可以工作,只是它不与 tabId 绑定,所以它并不完全理想。任何关于从哪里开始研究的想法将不胜感激!
到目前为止我所做的: 背景.js:
chrome.experimental.webRequest.onCompleted.addListener(function (details)
{
var headers = details.responseHeaders;
localStorage['ip'] = details.ip;
localStorage['url'] = details.url;
for (var i = 0, length = headers.length; i < length; i++)
{
var header = headers[i];
if (header.name == 'X-Server-Fingerprint')
{
localStorage['XServerFingerprint'] = header.value.toString();
break;
}
}
},{'urls': ['http://www.someurl.com/*']},['responseHeaders']);
弹出.js:
document.getElementById('url').innerText = localStorage['url'];
document.getElementById('ip').innerText = localStorage['ip'];
document.getElementById('XServerFingerPrint').innerText = localStorage['XServerFingerPrint'];
I should mention up front I'm new to code/stackoverflow so my apologies if this question doesn't makes sense. I'm beyond stumped, I'm trying to build a chrome extension that saves the ip address, url and a server finger print. The serverfingerprint is a field that lives within the response headers. Using my background.js and localStorage I can save this information and then display it in my pop up window. This is all fine and dandy except I can't figure out how to save it on a per tab basis, aka... if I have 5 tabs open, I'd like to click my extension and have it display the url for each corresponding tab. example: click tab4 and shows tab4's url, then click tab2 and it shows the url of tab2.
the below code works except for it doesn't tie to the tabId so it's not exactly ideal. Any ideas of where to start researching would be very appreciated!
what i've done thus far:
background.js:
chrome.experimental.webRequest.onCompleted.addListener(function (details)
{
var headers = details.responseHeaders;
localStorage['ip'] = details.ip;
localStorage['url'] = details.url;
for (var i = 0, length = headers.length; i < length; i++)
{
var header = headers[i];
if (header.name == 'X-Server-Fingerprint')
{
localStorage['XServerFingerprint'] = header.value.toString();
break;
}
}
},{'urls': ['http://www.someurl.com/*']},['responseHeaders']);
popup.js:
document.getElementById('url').innerText = localStorage['url'];
document.getElementById('ip').innerText = localStorage['ip'];
document.getElementById('XServerFingerPrint').innerText = localStorage['XServerFingerPrint'];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于每个选项卡都有唯一的 ID(直到浏览器重新启动),因此您可以使用它来识别选项卡。
您可能只对当前选项卡感兴趣,这使事情变得更简单,因为您不需要
localStorage
(它在浏览器重新启动之间保留数据)。只需使用后台页面的命名空间来存储有关所有选项卡的数据:如果您确实需要 localStorage,那么您可以将
tabs
对象转换为 json 字符串并将其存储在那里。As each tab has unique id (until browser restart), you can use it to identify tabs.
You are probably interested only in current tabs, which makes things simpler as you don't need
localStorage
for this (which persists data between browser restarts). Just use background page's namespace to store data about all tabs:If you do need localStorage then you can convert
tabs
object to json string and store it there.好的,我已经解决了我的问题!好吧,关于 chrome 扩展的那些哈哈,这似乎与 Serg 所说的差不多(谢谢 Serg!!)我写的有点不同。
Ok, so I've sorted out my issues! Well the ones concerning chrome extensions haha, which appears to be pretty much exactly what Serg is saying (thx Serg!!) I wrote it a bit different tho.