2011-06-07 50 views
1

我在開發一個擴展,我不想使用選項頁面。我使用瀏覽器操作(圖標顯示在右上角),並通過該頁面進行了一些首選項,並將它們存儲在localStorage中。從彈出頁面到內容腳本的通信頁面

但是,我的內容腳本需要讀取localStorage,但我知道它無法訪問它。我看着消息傳遞,但無法完成我想要的。

這裏是我曾嘗試:

popup.js

$(document).ready(function(){ 
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
     if (request.method == "getList") 
      sendResponse({status: localStorage['list']}); 
     else 
      sendResponse({}); // snub them. 
     }); 
}); 

content.js

$(document).ready(function(){ 
    var p; 
    chrome.extension.sendRequest({method: "getList"}, function(response) { 
     p = response.status; 
     alert(response.status); 
    }); 
}); 

回答

5

彈出的短暫意味着代碼只活了彈出窗口打開的時間。這意味着你的聽衆會丟失。

將您的聽衆代碼從popup.html移動到background page,那麼這應該可以正常工作。

+2

我注意到了區別,謝謝。我通過popup - > background-> localStorage-> contentScript – Mustafa 2011-06-30 08:01:08

+0

建立了連接您可以通過後臺腳本跟蹤存在於默認彈出窗口中的按鈕的onclick事件嗎? – user146303 2016-06-21 15:56:25

相關問題