2011-10-07 32 views
3

我應該提到前面我是新來的代碼/ stackoverflow所以我道歉,如果這個問題沒有道理。我無法忍受,我試圖建立一個保存IP地址,URL和服務器指紋的Chrome擴展。 serverfingerprint是一個位於響應頭中的字段。使用我的background.js和localStorage,我可以保存這些信息,然後將其顯示在我的彈出窗口中。這是所有罰款和丹迪,除了我不知道如何將它保存在每個選項卡的基礎上,又名...如果我有5個選項卡打開,我想點擊我的擴展,並顯示每個網址相應的標籤。例如:點擊tab4並顯示tab4的網址,然後點擊tab2並顯示tab2的網址。Chrome擴展,使用localStorage保存ip,tabid,serverfingerprint每個標籤

下面的代碼工作除了它不綁定到tabId,所以它不完全理想。任何想從哪裏開始研究將非常感激!

我所迄今所做: 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']; 

回答

1

由於每個標籤都有唯一的ID(直到重新啓動瀏覽器),你可以使用它來識別標籤。

您可能只對當前選項卡感興趣,這會讓事情變得更簡單,因爲您不需要localStorage(這會在瀏覽器重新啓動之間保留數據)。只要使用背景頁面的命名空間來存儲數據有關的所有標籤:

//background 
var tabs = {}; //all tab data 
chrome.experimental.webRequest.onCompleted.addListener(function (details) 
{ 
    var tabInfo = {}; 
    tabInfo["ip"] = ...; 
    tabInfo["url"] = ...; 
    tabInfo["XServerFingerprint"] = ...; 

    tabs[details.tabId] = tabInfo; 
} 

//popup 
chrome.tabs.getSelected(null, function(tab){ 
    var tabInfo = chrome.extension.getBackgroundPage().tabs[tab.id]; //get from bg page 

    document.getElementById('url').innerText = tabInfo['url']; 
    document.getElementById('ip').innerText = tabInfo['ip']; 
    document.getElementById('XServerFingerPrint').innerText = tabInfo['XServerFingerPrint']; 
}); 

如果你確實需要的localStorage那麼你可以tabs對象轉換爲JSON字符串,並將其存儲在那裏。

0

好的,所以我已經整理了我的問題!那麼關於鉻擴展的哈哈,這看起來幾乎與塞爾格所說的一樣(thx Serg !!)我寫了一些不同的tho。

background.htm 

chrome.experimental.webRequest.onCompleted.addListener(function (details) 
{ 
    var headers = details.responseHeaders; 
    var tabId = details.tabId; 
    var ip = details.ip; 
    var url = details.url; 

    for (var i = 0, length = headers.length; i < length; i++) { 
     var header = headers[i]; 
      //custom field in response headers from my site 
     if (header.name == 'X-Server-Fingerprint') { 
      var XServerFingerprint = header.value.toString(); 
      var data = { 
       ip: ip, 
       url: url, 
       fingerprint: XServerFingerprint 
      } 
      //store it 
      localStorage[tabId] = JSON.stringify(data); 
      break; 
     } 
    } 
},{'urls': ['http://www.corbisimages.com/*']},['responseHeaders']); 
} 

and then on my popup.js 

chrome.tabs.getSelected(null, function(tab) { 

    var parseData = JSON.parse(localStorage[tab.id]); 
    document.getElementById('XServerFingerprint').innerText = parseData.fingerprint; 
    document.getElementById('url').innerText = parseData.url; 
    document.getElementById('ip').innerText = parseData.ip; 
});