0

popup.js存儲監聽器:Chrome擴展:用於只存儲一個可變

... 
chrome.storage.sync.set({'source': source, 'active': active, 'secs': secs, 'domain': domain}, function() { 
    console.log('Settings saved'); 
}); 
... 

background.js:

chrome.storage.onChanged.addListener(function(tab) { 
    //something 
}); 

所以,可以予添加一個偵聽的變化(例如)'活性'?

+3

當你嘗試過時發生了什麼? – msanford

+1

正如您在文檔中看到的,監聽器會被調用以進行任何更改。 – wOxxOm

+0

如果我更改任何存儲變量的值,它將啓動函數。謝謝wOxxOm –

回答

0

您必須在偵聽器中爲特定鍵添加if或switch語句,如此;

chrome.storage.onChanged.addListener(function(changes, namespace) { 
    for(key in changes) { 
    if(key === 'active') { 
     // Do something here 
    } 
    } 
});