Skip to content

Instantly share code, notes, and snippets.

@Far-Se
Created October 30, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Far-Se/cab6cbba5aa1513eb82fcd879f3d1cf8 to your computer and use it in GitHub Desktop.
Save Far-Se/cab6cbba5aa1513eb82fcd879f3d1cf8 to your computer and use it in GitHub Desktop.
Tampermonkey script to block certain websites based on time
// ==UserScript==
// @name BlockWebsite
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://discord.com/*
// @icon https://icons.duckduckgo.com/ip2/iconfinder.com
// @grant none
// @run-at document-start
// ==/UserScript==
(() => {
let changing = false;
let timeout = 0;
let checkURL = () => {
const domains = {
// enforce:
// : true if you want only for affectedLinks
// : false if you want all others except for affectedLinks
// use `redirect` or `execute` to redirect to al link or execute specific code
//hard
"xdiscord.com":{
from:"8:00",
to: "17:00",
days:[1,2,3,4,5,6,7],
enforce: false,
affectedLinks:["/channels/1140924149443866738"],
redirect:"https://google.com",
delayTime: 1*1000
},
//soft
"discord.com":{
from:"8:00",
to: "17:00",
days:[1,2,3,4,5,6,7],
enforce: true,
affectedLinks:["/channels/484640057764872193"],
execute:`document.querySelector('[data-dnd-name="Notite"]').firstChild.querySelector('div').click();`,
delayTime: 10*1000
},
};
if(domains.hasOwnProperty(window.location.hostname))
{
const domain = domains[window.location.hostname];
const now = new Date();
if(!~domain.days.indexOf(now.getDay()+1))return;
const time = now.getTime();
const startTime = domain.from.split(":").map(e=>~~e);
const endTime = domain.to.split(":").map(e=>~~e);
const startHour = now.setHours(startTime[0],startTime[1],0,0);
const endHour = now.setHours(endTime[0],endTime[1],0,0);
let change = false;
if(time > startHour && time < endHour) change =true;
if(change === true && domain.affectedLinks)
{
let regex = new RegExp(`^(${domain.affectedLinks.join("|")})`);
if(window.location.pathname.match(regex)) change = domain.enforce;
else change = !domain.enforce;
}
if(change == true && !changing)
{
changing = true;
timeout = setTimeout(()=>{
if(change === true && domain.affectedLinks)
{
let regex = new RegExp(`^(${domain.affectedLinks.join("|")})`);
if(window.location.pathname.match(regex)) change = domain.enforce;
else change = !domain.enforce;
}
if(change) {
console.log("changing");
if(domain.redirect?.length)
{
window.location = domain.redirect;
}else {
eval(domain.execute);
}
}
changing = false;
},domain.delayTime);
}
}
}
checkURL();
let currentPage = location.href;
setInterval(() => {
if (currentPage == location.href)return;
currentPage = location.href;
clearTimeout(timeout);
changing = false;
checkURL();
}, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment