From c438ea3b2de83bb67728e9faf0b004211fc5723c Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 25 Nov 2021 13:50:57 +0530 Subject: [PATCH] userscript: proxy using post ID --- libmedium.user.js | 61 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/libmedium.user.js b/libmedium.user.js index fa6d586..71b09d4 100644 --- a/libmedium.user.js +++ b/libmedium.user.js @@ -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 = ""; + } } } })(); -