我有一个使用存储的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