Add script.js

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2023-09-17 16:32:47 +00:00
commit 51f68152a3

88
script.js Normal file
View file

@ -0,0 +1,88 @@
// ==UserScript==
// @name Privacy Redirector
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Redirects sites to privacy-focused frontends
// @author baalajimaestro
// @match https://youtube.com/*
// @match https://youtu.be/*
// @match https://www.youtube.com/*
// @match https://www.youtu.be/*
// @match https://twitter.com/*
// @match https://x.com/*
// @match https://www.x.com/*
// @match https://www.twitter.com/*
// @match https://stackoverflow.com/*
// @match https://www.stackoverflow.com/*
// @match https://reddit.com/*
// @match https://www.reddit.com/*
// @match https://old.reddit.com/*
// @match https://new.reddit.com/*
// @match https://quora.com/*
// @match https://www.quora.com/*
// @icon https://baalajimaestro.me/favicon.ico
// @grant none
// ==/UserScript==
// our document location
let link = document.location.href;
// regexes
const re_yt =
/^(?:(?:https|http):\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be).*(?<=\/|v\/|u\/|embed\/|shorts\/|watch\?v=)(?<!\/user\/)(?<id>[\w\-]{11})(?=\?|&|$)/;
const re_twt =
/^(?:https?:\/\/(?:twitter|x)\.com)(?<url>\/(?:#!\/)?(\w+)\/status(es)?\/(\d+))/;
const re_soflow =
/^https?:\/\/stackoverflow\.com(?<url>\/questions\/\d+\/[^\/\s]*)/;
const re_reddit =
/^https?:\/\/(?:www\.)?(?:old\.)?reddit\.com(?<url>\/r\/\w+\/comments\/\w+\/[^/?#]+)/;
const re_quora = /https:\/\/www\.quora\.com(?<url>\/.*)/;
// redirectors
const inv = "//invidious.baalajimaestro.me";
const nitter = "//nitter.net";
const oflow = "//overflow.baalajimaestro.me";
const lr = "//libreddit.baalajimaestro.me";
const qtr = "//quetre.baalajimaestro.me";
// handle dynamic pages
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (link != document.location.href) {
link = document.location.href;
handler();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
//universal handler to handle all sites
function handler() {
if (re_yt.test(link)) {
window.location.replace(
inv + "/watch?v=" + window.location.href.match(re_yt).groups.id,
);
} else if (re_twt.test(link)) {
window.location.replace(
nitter + window.location.href.match(re_twt).groups.url,
);
} else if (re_reddit.test(link)) {
window.location.replace(
lr + window.location.href.match(re_reddit).groups.url,
);
} else if (re_soflow.test(link)) {
window.location.replace(
oflow + window.location.href.match(re_soflow).groups.url,
);
} else if (re_quora.test(link)) {
window.location.replace(
qtr + window.location.href.match(re_quora).groups.url,
);
}
}
handler();