簡體   English   中英

自動關閉Chrome擴展程序

[英]Close the Chrome Extension automatically

保存數據后,我試圖自動關閉擴展名。

這是用於保存數據的代碼:

$(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;
    });
});

我正在用它來關閉擴展

setTimeout(function(){window.close();}, 3000);

但這是行不通的。 我應該編寫任何EventListener來自動關閉擴展程序嗎?

欣賞答案

考慮一下您的代碼中的以下片段:

window.location.href = 'saved.html';
setTimeout(function(){
  window.close();
}, 2000);

這是行不通的。 更改位置后,頁面立即開始導航,最終擦除了window狀態,並在加載新頁面時設置了所有超時。

您需要從saved.html設置超時才能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM