Last active
November 11, 2019 11:58
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name MantisAutoRefresh | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @updateURL https://gist.github.com/CEBracco/70d2feeabc313aaa3428976503ceef43/raw/MantisAutoRefresh.user.js | |
// @description Agrega opcion de auto refresh a la pagina de incidencias de mantis | |
// @author cbracco | |
// @match https://incidencias.cespi.unlp.edu.ar/view_all_bug_page.php | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var myInterval; | |
// Active | |
//window.addEventListener('focus', startTimer); | |
// Inactive | |
//window.addEventListener('blur', stopTimer); | |
function timerHandler() { | |
location.reload(); | |
} | |
// Start timer | |
function startTimer() { | |
console.log('Reloaded ' + formatDate(new Date())); | |
myInterval = window.setInterval(timerHandler, 20000); | |
} | |
// Stop timer | |
function stopTimer() { | |
window.clearInterval(myInterval); | |
} | |
function formatDate(date) { | |
var monthNames = [ | |
"January", "February", "March", | |
"April", "May", "June", "July", | |
"August", "September", "October", | |
"November", "December" | |
]; | |
var day = date.getDate(); | |
var monthIndex = date.getMonth(); | |
var year = date.getFullYear(); | |
var hours = padTime(date.getHours()); | |
var minutes = padTime(date.getMinutes()); | |
var seconds = padTime(date.getSeconds()); | |
return `Reloaded ${day} ${monthNames[monthIndex]} ${year} ${hours}:${minutes}:${seconds}` | |
} | |
function padTime(n) { | |
var z = '0'; | |
var width = 2 | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
function setCookie(name,value,days) { | |
var expires = ""; | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime() + (days*24*60*60*1000)); | |
expires = "; expires=" + date.toUTCString(); | |
} | |
document.cookie = name + "=" + (value || "") + expires + "; path=/"; | |
} | |
function removeCookie(name) { | |
document.cookie=`${name}=;expires=Thu; 01 Jan 1970` | |
} | |
function getCookie(name) { | |
var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for(var i=0;i < ca.length;i++) { | |
var c = ca[i]; | |
while (c.charAt(0)==' ') c = c.substring(1,c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); | |
} | |
return null; | |
} | |
function addAutoRefreshButton() { | |
$("#bug_action .widget-header").append("<button type='button' class='btn btn-primary btn-white btn-round btn-sm btn-autorefresh' style='position: absolute;right: 8px;top: 2px;'>Activar Auto-Refresh</button>"); | |
$(".btn-autorefresh").click(function(){ | |
setCookie("auto-refresh",true,999) | |
location.reload(); | |
}); | |
} | |
function initAutorefresh() { | |
startTimer(); | |
$("#bug_action .widget-header").append("<button type='button' class='btn btn-primary btn-round btn-sm btn-disable-autorefresh' style='position: absolute;right: 8px;'><i class='fa fa-refresh fa-spin fa-fw'></i> Desactivar Auto-Refresh</button>"); | |
$(".btn-disable-autorefresh").click(function(){ | |
removeCookie("auto-refresh") | |
location.reload(); | |
}); | |
} | |
if(getCookie("auto-refresh")) { | |
initAutorefresh() | |
} else { | |
addAutoRefreshButton() | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment