paw34 / Pages Auto Updater

// ==UserScript==
// @name          Pages Auto Updater
// @namespace     pw_scripts
// @description   Update page every 10 seconds, prevent inactive timeout
// @run-at        document-start
// @include       *
// @copyright     paw
// @author        paw
// @license       BSD-2-Clause
// @version       1.0.0
// @grant         none
// @noframes
// ==/UserScript==

var interval = 10;

var timer = null;
var busy = false;
var msg_box = document.createElement('div');
msg_box.style.cssText = `position:fixed;
top: 0 !important;
left: 0 !important;
z-index:99999999 !important;
background-color: #ffff !important;
padding: 2px !important`;

function msg(text, color) {
   msg_box.style.color = color;
   msg_box.innerHTML = '[' + text + ']';
}

function on_complete(e) {
    busy = false;
    if(e.target.status == 200) msg('pulse ok', 'Green');
    else msg('connect error: ' + e.target.status, 'Red');
    console.log('load ' + e.target.responseURL + ' ' + e.target.status + ' ' + e.target.statusText);
}

function background_connect()
{
    if(!busy) {
        var xhr = new XMLHttpRequest();
        xhr.addEventListener("loadend", on_complete, false);
        xhr.open('GET', document.location, true);
        busy = true;
        xhr.send();
    }
}

document.onkeydown = function(e) {
    e = e || window.event;
    if(e.shiftKey) {
        switch (e.keyCode) {
            case 65: // A, start auto update
                if(msg_box.parentNode != document.body) {
                    if(msg_box.parentNode) msg_box.parentNode.removeChild(msg_box);
                    document.body.prepend(msg_box);
                }
                msg('Auto update every ' + interval + 's', 'Green');
                clearInterval(timer);
                timer = setInterval(background_connect, interval * 1000);
            break;
            case 83: // S, stop auto update
		document.body.removeChild(msg_box);
		clearInterval(timer);
            break;
        }
    }
};