[英]Close the Chrome Extension automatically
I am trying to close the extension automatically after saving my data. 保存数据后,我试图自动关闭扩展名。
Here is the code which is used to save the data: 这是用于保存数据的代码:
$(function(){
function displayMessage(string){
alert(string);
}
var submit = $('#submit');
$('#create').submit(function(){
$('#loading_image').html('<img src="images/wait.gif">');
var user_email = localStorage.getItem('email');
var api = localStorage.getItem("user_api_key");
var auth = "apikey" +' ' +(user_email+":"+api);
var favicon_key = localStorage.getItem("favicon_image_key");
var peoples = [];
var tags_peoples = $("#s2id_people .select2-choices .select2-search-choice");
for (i=0; i<tags_peoples.length; i++) {
peoples.push({"label": tags_peoples[i].childNodes[1].textContent})
}
var subjects = [];
var tags_subjects = $("#s2id_subjects .select2-choices .select2-search-choice");
for (i=0; i<tags_subjects.length;i++) {
subjects.push({"label": tags_subjects[i].childNodes[1].textContent})
}
var places = [];
var tags_places = $("#s2id_places .select2-choices .select2-search-choice");
for (i=0; i<tags_places.length; i++) {
places.push({"label": tags_places[i].childNodes[1].textContent})
}
var begin = $(".daterangepicker_start_input")[0].childNodes[1].value;
var end = $(".daterangepicker_end_input")[0].childNodes[1].value;
var data = {
'content': $('#title').val(),
'people': peoples,
'subjects': subjects,
'begin_date': begin,
'end_date': end,
'places': places
}
$.ajax({
type: "POST",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", auth);
xhr.setRequestHeader("Content-Type","application/json");
xhr.setRequestHeader("Accept","application/json");
},
url: "https://my_site_url/api/v3/data/",
dataType: "json",
data: JSON.stringify(data),
contentType: "application/json",
success: function(data) {
$('#loading_image').hide();
window.location.href = 'saved.html';
setTimeout(function(){
window.close();
}, 2000);
},
error: function(data) {
$('#div1').text("Error on saving the data");
$('#loading_image').hide();
},
complete: function() {
submit.removeAttr('disabled').text('Save');
}
});
return false;
});
});
I am using this to close the extension 我正在用它来关闭扩展
setTimeout(function(){window.close();}, 3000);
But this doesn't work. 但这是行不通的。 Should I write any EventListener to close the extension automatically?
我应该编写任何EventListener来自动关闭扩展程序吗?
Appreciated the answers 欣赏答案
Consider this snippet from your code: 考虑一下您的代码中的以下片段:
window.location.href = 'saved.html';
setTimeout(function(){
window.close();
}, 2000);
This will not work. 这是行不通的。 As soon as you change location, the page starts to navigate away, eventually wiping the
window
state and all timeouts set when the new page loads. 更改位置后,页面立即开始导航,最终擦除了
window
状态,并在加载新页面时设置了所有超时。
You need to set the timeout from saved.html
for this to work. 您需要从
saved.html
设置超时才能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.