Javascript Chrome扩展-无法获取';延迟';来自eventPage.js的响应
我昨天刚开始一个chrome扩展,所以我是一个新手,请原谅我的noob问题 我有一个popup.js,它在popup.html中显示内容-因此popup.js很简单 popup.jsJavascript Chrome扩展-无法获取';延迟';来自eventPage.js的响应,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我昨天刚开始一个chrome扩展,所以我是一个新手,请原谅我的noob问题 我有一个popup.js,它在popup.html中显示内容-因此popup.js很简单 popup.js $(document).ready(function () { chrome.tabs.getSelected(null, function (tab) { chrome.runtime.sendMessage({ tab: tab }, functi
$(document).ready(function () {
chrome.tabs.getSelected(null, function (tab) {
chrome.runtime.sendMessage({
tab: tab
}, function (response) {
document.getElementById("lockDiv").hidden = false;
document.getElementById("loadingDiv").hidden = true;
});
});
});
chrome.runtime.sendMessage({
tab: tab
}, function (response) {
response.then(resultFromListener => {
// resultFromListener is the value resolved (resultYouWantToReturn) above
document.getElementById("lockDiv").hidden = false;
document.getElementById("loadingDiv").hidden = true;
});
});
它获取所选选项卡,将消息(带选项卡)发送到事件页面,如下所示
eventPage.js
chrome.runtime.onMessage.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
setInterval(function () {
// Do something after 5 seconds
}, 5000);
});
});
chrome.runtime.onMessage.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
return new Promise(resolve => {
setInterval(function () {
// Do something after 5 seconds
resolve(resultYouWantToReturn)
}, 5000);
});
});
});
现在它可以工作了(有点),但一旦popup.js运行,在执行之前5秒钟内不会有延迟
document.getElementById("lockDiv").hidden = false;
document.getElementById("loadingDiv").hidden = true;
执行上述操作时,无需等待eventPage.js的响应
我如何才能让它等待响应并让evenPage.js中的操作在显示已执行的上述代码之前完成?已经阅读了for onMessage-(它应该与chrum扩展相同,因为mozilla web扩展基于chrum)-包含关于“异步发送响应”的部分-您可以执行以下操作(显然)
eventPage.js
chrome.runtime.onMessage.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
setInterval(function () {
// Do something after 5 seconds
}, 5000);
});
});
chrome.runtime.onMessage.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
return new Promise(resolve => {
setInterval(function () {
// Do something after 5 seconds
resolve(resultYouWantToReturn)
}, 5000);
});
});
});
popup.js
$(document).ready(function () {
chrome.tabs.getSelected(null, function (tab) {
chrome.runtime.sendMessage({
tab: tab
}, function (response) {
document.getElementById("lockDiv").hidden = false;
document.getElementById("loadingDiv").hidden = true;
});
});
});
chrome.runtime.sendMessage({
tab: tab
}, function (response) {
response.then(resultFromListener => {
// resultFromListener is the value resolved (resultYouWantToReturn) above
document.getElementById("lockDiv").hidden = false;
document.getElementById("loadingDiv").hidden = true;
});
});
在退出onMessage listener之前添加
返回true
。并在onMessage listener中使用sendResponse
。ChromeAPI不适用于承诺。OP需要在onMessage中返回true,如文档中所述。有趣的是,MDN文档没有提到这种不一致性。令人惊讶的是,Mozilla的API基于ChromePromises与浏览器
名称空间一起工作,这是WebExtension API,而不是ChromeAPI。问题被标记为google Chrome extension——我从标记信息中假设它确实与现在所谓的Web扩展有关