Javascript 自动关闭Chrome扩展插件
我正在尝试在保存数据后自动关闭扩展 以下是用于保存数据的代码:Javascript 自动关闭Chrome扩展插件,javascript,jquery,google-chrome-extension,auto-close,Javascript,Jquery,Google Chrome Extension,Auto Close,我正在尝试在保存数据后自动关闭扩展 以下是用于保存数据的代码: $(function(){ function displayMessage(string){ alert(string); } var submit = $('#submit'); $('#create').submit(function(){ $('#loading_image').html('<img src="images/wait.gif">'
$(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;
});
});
但这不起作用。我是否应该编写任何EventListener来自动关闭扩展
感谢您的回答考虑一下您的代码片段:
window.location.href = 'saved.html';
setTimeout(function(){
window.close();
}, 2000);
这是行不通的。一旦您更改位置,页面就会开始导航,最终会在加载新页面时擦除窗口的状态和所有超时设置
您需要从saved.html
设置超时,才能使其正常工作。定义“关闭扩展”。弹出窗口?你是如何“保存你的数据”的?嗨@Xan。更新了保存数据的代码。请检查一下,是的,那是丢失的那块。下一次你提出一个问题,考虑阅读。所以,如果我在一个JS文件中写入SETTIMEOUT,并将其加载到SaveD.HTML中,那么它会起作用吗?是的,这就是我所说的。
window.location.href = 'saved.html';
setTimeout(function(){
window.close();
}, 2000);