簡體   English   中英

如何使用 tampermonkey 關閉 chrome 瀏覽器 window

[英]How to close chrome browser window using tampermonkey

我正在嘗試使用 tampermonkey 關閉 chrome 瀏覽器 windows,但沒有完全獲得所需的結果。 當出現超時情況時,代碼會工作,它會殺死選項卡,但第二個條件是任務完成然后window.close()不起作用,即使在超時發生時任務已完成,瀏覽器選項卡也會被殺死。 所以這是一個非常奇怪的問題,我不明白為什么window.close()在一點上起作用而在另一點上不起作用,我什至交換了兩個 if 上下塊但沒有成功。 所以知道做錯了什么。

我的代碼

// ==UserScript==
// @name         chromium windows / tab close
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  used by my APP to close chromium browser window
// @author       mee
// @grant        window.close
// @require      https://code.jquery.com/jquery-3.5.1.min.js
// ==/UserScript==

var stime = 0;
var timeout = 300; // 5 min

// millis to seconds
function miliseconds_to_seconds(millis) {
  return ((millis % 60000) / 1000).toFixed(0);
}

// close browser
function close_browser(){
    // get task progress
    let progress = $("#close_browser").val();
    let kill_it = false;
    
    // check for task time out
    let ctime = performance.now();
    let time_passed = miliseconds_to_seconds(ctime - stime); // in sec
    console.log("Time Passed : "+time_passed+'  gviewer progress : '+progress);
    if(time_passed >= timeout){
        console.log('closing tab time out');
        window.close(); // kill tab ........ WORKING OK
    }
    
    // check if task is completed
    if(progress == 100){
        console.log('closing tab task done');
        window.close(); // kill tab  ............ NOT WORKING 
        timeout = 0; // will force to execute condition 1 but it will not close window till the real timeout is over mean 300 sec.
    }

}

$(document).ready(function() {
       stime = performance.now();
       var intervalID = setInterval(close_browser, 5000);
});

Tampermonkey 不允許 window.close 是停止腳本的功能,例如:

//@match *://*/*
(function() {
    'use strict';
    window.close();
})

暫無
暫無

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

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