简体   繁体   English

Chrome扩展程序:XHR后关闭窗口

[英]Chrome Extension: Close Window After XHR

I want to make an XHR from a chrome extension when the user clicks a button. 当用户单击按钮时,我想通过chrome扩展名创建XHR。 Then I want to close the window. 然后我要关闭窗口。

If I close the window after the send, the request is canceled. 如果在发送后关闭窗口,请求将被取消。 If I close the window in the request listener, the extension crashes. 如果关闭请求侦听器中的窗口,扩展名将崩溃。

How to close the window? 如何关闭窗户?

var requestListener = function() {
  bkg.console.log(this.readyState);
  window.close() // <-- When I put this here, the extension crashes.
};

document.addEventListener('DOMContentLoaded', function() {
  var button = document.getElementById('button');
  button.addEventListener('click', function() {
    var data = {foo: "bar"};
    var xhr = new XMLHttpRequest();
    xhr.open("post", "http://localhost:8080", true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.onreadystatechange = requestListener;
    xhr.send(JSON.stringify(data));
    window.close(); // <-- When I put this here, the request is canceled.
  });
});

A wild guess: Maybe the window closes too soon so something like... 一个大胆的猜测:也许窗户关得太早,所以像...

<body onbeforeunload="SendFunction();">

...may help. ...可能会有所帮助。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM