userscript: proxy using post ID

This commit is contained in:
Aravinth Manivannan 2021-11-25 13:50:57 +05:30
parent 9c5342a053
commit c438ea3b2d
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -1,6 +1,6 @@
// ==UserScript==
// @name LibMedium proxy
// @version 0.1.0
// @version 0.1.1
// @description Re-writes medium.com URLs in point to libmedium
// @author Aravinth Manivannan
// @match https://*/*
@ -9,25 +9,62 @@
// ==/UserScript==
// websites to be proxied
const blacklist = ["medium.com", "blog.discord.com", "uxdesign.cc"];
const blacklist = [
"medium.com",
"blog.discord.com",
"uxdesign.cc",
"towardsdatascience.com",
"hackernoon.com",
"medium.freecodecamp.org",
"psiloveyou.xyz",
"betterhumans.coach.me",
"codeburst.io",
"theascent.pub",
"medium.mybridge.co",
"uxdesign.cc",
"levelup.gitconnected.com",
"itnext.io",
"entrepreneurshandbook.co",
"proandroiddev.com",
"blog.prototypr.io",
"thebolditalic.com",
"blog.usejournal.com",
];
// Location of the proxy
const libmediumHost = "https://libmedium.batsense.net";
const libmediumHost = new URL("https://libmedium.batsense.net");
const isMedium = (url) => {
url = new URL(url);
if (blacklist.find((bl) => url.host.includes(bl))) {
return true;
}
let componenets = url.host.split(".");
let len = componenets.length;
if (
len > 1 &&
componenets[len - 1] == "com" &&
componenets[len - 2] == "medium"
) {
return true;
}
};
(function () {
"use strict";
// morty has a button to go to the original site, re-writing that would be stupid
if (!window.location.href.includes(libmediumHost)) {
if (!window.location.href.includes(libmediumHost.host)) {
let urls = document.links;
let lib = new URL(libmediumHost);
for (let i = 0; i < urls.length; i++) {
blacklist.forEach((url) => {
if (urls[i].host.includes(url)) {
urls[i].host = lib.host;
}
});
if (isMedium(urls[i])) {
let url = urls[i];
let path = url.pathname.split("-");
urls[i].pathname = `/utils/post/${path[path.length - 1]}`;
urls[i].host = libmediumHost.host;
urls[i].search = "";
}
}
}
})();