我有一個使用存儲的chrome擴展程序,無法通過一次輸入點擊從存儲中獲取值。Chrome擴展程序:無法通過點擊獲得存儲空間的價值
有一個輸入字段。在用戶輸入一個值並按回車後,擴展應該從存儲中獲取該值並將該用戶的輸入添加到該值。第一次輸入按下不起作用,但如果用戶第二次單擊Enter,則會看到存儲的值。
我認爲問題在於函數的排序,但我無法理解到底在哪裏。
代碼在正確的順序:
var repo, moduleCodes, url;
// Third process
function getStoredUrl() {
chrome.storage.sync.get(function (item) {
url = item.savedUrl;
});
}
// Fourth process
function setVariables() {
repo = document.getElementById("repo").value.toLowerCase();
moduleCodes = {
admin: "EHEALTHADM"
};
}
// Second process
function openGraph() {
getStoredUrl();
setVariables();
if (moduleCodes[repo] !== undefined) {
// ERROR: field "test" doesn't have value url, but should to have
document.getElementById("test").value = url;
//window.open(url + moduleCodes[repo]);
} else {
returnError("Can't find repo " + repo, "repo");
}
}
var enter = 13;
// First process
function inputRepoListener(e) {
"use strict";
if (e.keyCode === enter) {
openGraph();
}
}
整個代碼可以在GitHub庫中可以看出:https://github.com/iriiiina/fisheyeGraph
'getStoredUrl'在回調到'sync.get'之前終止。 (實際上,在回調有機會運行之前,所有*代碼都會運行。)請參閱[如何從Ajax調用返回響應?](http://stackoverflow.com/q/14220321/710446) – apsillers 2014-09-02 18:34:15