Javascript 在异步存储函数中运行函数无法完成回调
所以我想用从存储器中提取的数据来响应请求。不知道为什么这段代码可以工作,但无论如何,我知道如何删除注释下的警报,这样我就不会有一个空的弹出窗口,这仍然可以工作Javascript 在异步存储函数中运行函数无法完成回调,javascript,jquery,google-chrome-extension,Javascript,Jquery,Google Chrome Extension,所以我想用从存储器中提取的数据来响应请求。不知道为什么这段代码可以工作,但无论如何,我知道如何删除注释下的警报,这样我就不会有一个空的弹出窗口,这仍然可以工作 background.js: var mData = []; chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){ if (request.command == "submit-chat") { chrome.tabs.ge
background.js:
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
//Not sure why the alert is needed it is like it lets the sendResponce out of the function
alert(chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
sendResponse({emots: mData.join("|≈|")});
}));
}
});
它被称为什么地方
content.js
var emoticons = null;
window.onload = function(){
jQuery.noConflict();
chrome.runtime.sendMessage({command: "get-emots"}, function(response) {
emoticons = response.emots;
if (emoticons !== null) {
var input = document.getElementsByClassName("input")[0].innerHTML = "";
jQuery(".input").load(chrome.extension.getURL("input.html"), function(){...});
}
});
}
function getPage(){
alert("You are going to be redirected to a page to get all hipchat emoticons. Please refresh that page then come back and refresh this page.");
var win = window.open('http://hipchat-emoticons.nyh.name', '_blank');
win.focus();
};
var getEmots = function(callback){
chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
typeof callback === 'function' && callback(mData);
});
};
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
getEmots(function(mData){ sendResponse({emots: mData.join("|≈|")}); });
return true;
}
});
更新:所以我尝试了这个,但仍然没有成功
var getEmots = function(callback){
chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
typeof callback === 'function' && callback(mData);
});
};
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
getEmots(function(mData){
console.log("Data: " + mData);
sendResponse({emots: mData.join("|≈|")});
});
}
});
更新2:让它在这里工作是其他人的最终选择。谢谢大家,这里是决赛
这是background.js
var emoticons = null;
window.onload = function(){
jQuery.noConflict();
chrome.runtime.sendMessage({command: "get-emots"}, function(response) {
emoticons = response.emots;
if (emoticons !== null) {
var input = document.getElementsByClassName("input")[0].innerHTML = "";
jQuery(".input").load(chrome.extension.getURL("input.html"), function(){...});
}
});
}
function getPage(){
alert("You are going to be redirected to a page to get all hipchat emoticons. Please refresh that page then come back and refresh this page.");
var win = window.open('http://hipchat-emoticons.nyh.name', '_blank');
win.focus();
};
var getEmots = function(callback){
chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
typeof callback === 'function' && callback(mData);
});
};
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
getEmots(function(mData){ sendResponse({emots: mData.join("|≈|")}); });
return true;
}
});
由于Javascript是单线程的,异步回调在UI到达空闲循环之前不会运行。您对
alert()
的调用会在等待用户关闭弹出窗口时强制其空闲,从而允许异步代码运行。如果getPage()是异步的,@Barmar是正确的。您需要使用承诺或某种回调来替换警报。我尝试使用回调,但没有运行sendResponse({emots:mData.join(“)|≈|")});
但是它确实运行了控制台.log(“数据:+mData);
看到这个答案:在调用getEmots
之后,您需要返回true
。谢谢,这一切都非常有帮助