Chrome扩展运行时.Sendmessage等待响应
Chrome Extension runtime.sendmessage wait for responses
我正在开发一个chrome扩展,我有一些问题与chrome.runtime.sendMessage
功能
我的代码是这样设置的:
chrome.runtime.sendMessage( { method : "getLocalStorage", key: "email" }, function ( response ) {
console.log( "the response has been received" );
});
console.log( "I am here" );
$.ajax({});
输出:
I am here
the response has been received
所以我的问题是chrome.runtime.sendMessage与其他代码异步运行。我能做的当然是把ajax放到sendMessage的响应函数中。唯一的问题是,在我做ajax调用之前,我有sendMessage的3个事件来返回我需要的不同变量,所以不幸的是,这不是一个可行的选择。
是否有任何方法来停止ajax调用,直到所有的sendmessage调用已经完成?
您应该考虑从旧的"在localStorage
中存储数据,在sendMessage
中查询后台"模式切换到新的"在chrome.storage
中存储数据,在任何地方访问"模式;chrome.storage
API是专门为这个目的制作的。
缺点是所有访问都是异步的。但至少你可以优化一下,例如你可以把你的调用粘合在一起:
chrome.storage.local.get([key1, key2, key3], function(data) {
// Use data.key1, data.key2, etc.
$.ajax({});
});
你甚至可以在没有默认值的情况下提供默认值:
chrome.storage.local.get({key1: default1, key2: default2, key3: default3}, function(data) {
// Use data.key1, data.key2, etc.
$.ajax({});
});
最后但并非最不重要的是,你有chrome.storage.sync
,将自动传播到其他登录配置文件
一个选择是使用Async.js。然后你可以这样做:
async.parallel([
function(done) {
chrome.runtime.sendMessage( { your first message }, function ( response ) {
done();
});
},
function(done) {
chrome.runtime.sendMessage( { your second message }, function ( response ) {
done();
});
},
function(done) {
chrome.runtime.sendMessage( { your third message }, function ( response ) {
done();
});
}
], function() {
console.log( "I am here" );
$.ajax({});
})
相关文章:
- 在单击事件时更新主干模型,并在更改位置之前等待响应
- 如何使用 Ajax 一次仅按顺序发布数组的 1 个值,并在继续之前等待响应
- 如何在 $.post jQuery 中等待响应
- 如何在 ajax 中调用函数到 php 函数并等待响应
- Ajax在返回值之前等待响应
- 为什么这个呼叫在继续之前没有等待响应
- 如何在最里面的循环等待响应时延迟嵌套迭代
- 如何使用async.series来等待响应
- Angularjs$http等待响应
- 当使用带有单个套接字的httpAgent时,它会在发送下一个请求之前等待响应吗?
- Node.js:服务器等待响应和超时
- Google Maps放置API——如何让代码等待响应
- iMacros可以单击按钮而不等待响应吗?
- AngularJS控制器等待响应(或设置回调)
- Chrome扩展运行时.Sendmessage等待响应
- Node.js (node-pg)或C (libpq) -执行PostgreSQL请求而不等待响应
- 在JavaScript中异步加载配置,如何最好等待响应
- 当我们进行ajax调用并等待响应并阻塞页面时显示一个旋转器
- 运行jQuery函数.Ajax等待响应的时间足够长
- js:将记录保存到Ember.数据,等待响应后再显示