'How to close chrome browser window using tampermonkey
Am trying to close chrome browser windows using tampermonkey but am not getting desire result fully.
the code works when there is time out condition occur , it kill the tab but the 2nd condition where task is completed then window.close()
is not working and even if task is completed when timeout occur the browser tab get killed. so its very strange issue i not understand why window.close()
is working it one point and not working it another point , i even swapped both if block up and down but no success. so any idea what am doing wrong.
my code
// ==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);
});
Solution 1:[1]
Tampermonkey not allowing window.close is a feature to stop scripts like:
//@match *://*/*
(function() {
'use strict';
window.close();
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | asdfish |