2017-07-13 165 views
0

我正在嘗試保存用戶的電子郵件和密碼。我在這裏錯過了什麼?chrome.storage.local.set/get not working properly?

這裏是我的HTML:

<input type="text" name="email" id="email" placeholder="E-mail" /> 
<input type="password" name="password" id="password" placeholder="Password" /> 
<button id="import">Import!</button> 
<script type="text/javascript" src="popup.js"></script> 

這裏是我的JavaScript:

chrome.storage.local.get('email', function(result) { 
    document.getElementById("email").value = result.value; 
}); 

chrome.storage.local.get('password', function(result) { 
    document.getElementById("password").value = result.value; 
}); 

document.getElementById("import").addEventListener("click", function() { 
    chrome.storage.local.set({'email': document.getElementById("email").value}); 
    chrome.storage.local.set({'password': document.getElementById("password").value}); 
} 

Strangly,當我刷新頁面,值不被保留。

+0

[看這裏】(https://stackoverflow.com/questions/13872542/chrome-chrome-storage-local-get-and-set)任何幫助嗎? – Constantine

+0

當我試着'console.log(result)'我意識到,'chrome.storage.local.get'實際上返回一個沒有'value'的對象。我會發布答案。 –

回答

0

它應該是:

chrome.storage.local.get('email', function(result) { 
    document.getElementById("email").value = result.email; 
}); 

chrome.storage.local.get('password', function(result) { 
    document.getElementById("password").value = result.password; 
});