Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Userscript to block from reopenning closed websites again and again. See http://softwarerecs.stackexchange.com/questions/31422/
// ==UserScript==
// @name TimeoutBlock
// @namespace white.ucc.asn.au
// @description Blocks you from reopening the applicable sites for a given timeout after they are closed
// @include https://www.facebook.com/*
// @include http://academia.stackexchange.com/*
// @version 1
// @grant GM_addStyle
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.1/js.cookie.js
// @run-at document-start
// ==/UserScript==
(function(){
// Add/Remove Sites by using @include and @exclude above
// Adjust Timeout Below:
var timeout = 30.0; //Minutes
//Allow for leaving the site and immediately returning to it (eg by reentering it in addressbar, or going to subsite)
var leeway = 5000; //milliseconds
//Immeditately check if should be blocking:
if(Cookies.get("TimeoutBlock")){
var blockEndsAt = new Date(Cookies.getJSON('TimeoutBlock').expires);
var now = new Date();
var remaining = blockEndsAt.getTime() - now.getTime();
if(timeout*60 * 1000-remaining<leeway) {
Cookies.remove('TimeoutBlock'); //Delete it, that page change didn't count
}else{
GM_addStyle("body {display:none;}"); //Hide The page immeditately (no flicker)
window.addEventListener('beforescriptexecute', function(e) {
//stop it running its scripts (for ajax etc)
e.stopPropagation();
e.preventDefault();
}, true);
//Now onces the page is actually loaded
$( document ).ready(function() {
$("body").html("<h1>Timeout engaged</h1>");
$("body").append("<span>The timeout occurs when you close the page.<br> It prevents you from immediately reopening it again.</span><br/>");
$("body").append(typeof(blockEndsAt).toString());
var rem_hours = Math.floor(remaining / (1000 * 60 * 60));
remaining -= rem_hours * (1000 * 60 * 60);
var rem_mins = Math.floor(remaining / (1000 * 60));
remaining -= rem_mins * (1000 * 60);
var rem_secs = Math.floor(remaining / 1000);
var rem_mins_str = ("00"+rem_mins).slice(-2);//http://stackoverflow.com/a/14760377/179081
var rem_secs_str = ("00"+rem_secs).slice(-2);//http://stackoverflow.com/a/14760377/179081
$("body").append("<h2> Remaining Time: "+rem_hours +":"+ rem_mins_str+":"+rem_secs_str+"</h2>");
GM_addStyle("body {display: table; margin: 100px auto; width:300px;}");
})
};
}
else { //Cookie was not found
// The cookie must be set when the page is closed or left. If it does not exists already
$(window).unload(function() {
var expireat = new Date();
expireat.setTime(expireat.getTime() + (timeout*60 * 1000));
Cookies.set("TimeoutBlock",
{
nb: "Delete Me to Clear Block",
expires: expireat
},
{ expires: expireat});
});
};
})();
/*
The MIT License (MIT)
Copyright (c) 2016: Lyndon White (AKA oxinabox AKA Frames)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment