JavaScript + Chrome Tabs Api - 无法获取选项卡的 URL
我有以下代码,问题是 chrome.tabs.getSelected 的回调在使用空 url 发送的请求之后进行评估。我该如何解决这个问题?
function send() {
var url = '';
chrome.tabs.getSelected(null, function(tab) {
url = tab.url;
});
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if(this.readyState == 4) {
alert(this.status);
}
}
client.open("POST", "http://myurl");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(url);
}
I have the following piece of code and the problem is that the callback from chrome.tabs.getSelected is evaluated after the request which is send with empty url. How can I solve this?
function send() {
var url = '';
chrome.tabs.getSelected(null, function(tab) {
url = tab.url;
});
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if(this.readyState == 4) {
alert(this.status);
}
}
client.open("POST", "http://myurl");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(url);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到异步编程
Welcome to Asynchronous Programming