From 3cbe4647e186235f86d5f323efeb3172e8a0be9c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 14 Jun 2020 00:32:03 +1200 Subject: [PATCH] Add userscript --- src/site/api/userscripts.js | 30 +++++++ src/site/html/userscripts/unblock.user.js | 101 ++++++++++++++++++++++ src/site/pug/blocked.pug | 13 ++- src/site/sass/includes/_main.sass | 7 ++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/site/api/userscripts.js create mode 100644 src/site/html/userscripts/unblock.user.js diff --git a/src/site/api/userscripts.js b/src/site/api/userscripts.js new file mode 100644 index 0000000..32d5572 --- /dev/null +++ b/src/site/api/userscripts.js @@ -0,0 +1,30 @@ +const fs = require("fs").promises +const constants = require("../../lib/constants") + +// Instances are here rather than loaded dynamically because adding more requires the script to auto-update, which requires a version increase +const instanceList = new Set([ + "https://bibliogram.art", + "https://bibliogram.snopyta.org", + "https://bibliogram.pussthecat.org", + "https://bibliogram.13ad.de", + "https://bibliogram.nixnet.services", + "https://bibliogram.hamster.dance", + "https://bibliogram.ggc-project.de" +]) + +instanceList.add(constants.website_origin) + +module.exports = [ + { + route: "/userscripts/unblock.user.js", methods: ["GET"], code: async () => { + let script = await fs.readFile("html/userscripts/unblock.user.js", "utf8") + script = script.replace(//g, constants.website_origin) + script = script.replace(/\/\/ /g, [...instanceList.values()].map(i => `// @match ${i}/u/*`).join("\n")) + return { + statusCode: 200, + contentType: "application/javascript", + content: script + } + } + } +] diff --git a/src/site/html/userscripts/unblock.user.js b/src/site/html/userscripts/unblock.user.js new file mode 100644 index 0000000..34bccf7 --- /dev/null +++ b/src/site/html/userscripts/unblock.user.js @@ -0,0 +1,101 @@ +// ==UserScript== +// @name Bibliogram unblocker +// @namespace +// +// @downloadURL /userscripts/unblock.user.js +// @updateURL /userscripts/unblock.user.js +// @grant none +// @version 1.0 +// @author cloudrac3r +// ==/UserScript== + + +// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Be sure to press "Confirm installation" to install this script! +// +// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + +function q(s) { + return document.querySelector(s) +} + +function addChild(parent, tag, content, className) { + const e = document.createElement(tag) + e.textContent = content + if (className) e.className = className + parent.appendChild(e) + return e +} + +function applyStyles(element, styles) { + for (const key of Object.keys(styles)) { + element.style[key] = styles[key] + } +} + +if (q("#bibliogram-identifier-blocked")) { + const scriptStatus = addChild(q("#dynamic-status-area"), "p", "Unblocker script is processing...", "explanation") + applyStyles(scriptStatus, {border: "solid orange", borderWidth: "1px 0px", padding: "10px", marginTop: "20px", textAlign: "center"}) + function flashBackground() { + scriptStatus.animate([ + {"background": "rgba(255, 255, 255, 0.15)", easing: "ease-in"}, + {"background": "rgba(255, 255, 255, 0)"} + ], 1000) + } + + const username = q("#data").getAttribute("data-username") + + fetch(`https://www.instagram.com/${username}/`).then(res => { + + if (res.status === 200) { + res.text().then(text => { + const id = (text.match(/"id":"([0-9]+)"/) || [])[1] + + if (id) { + const params = new URLSearchParams() + params.append("username", username) + params.append("user_id", id) + + fetch(`${window.location.origin}/api/suggest_user/v1`, { + headers: { + "content-type": "application/x-www-form-urlencoded" + }, + method: "POST", + body: params.toString() + }).then(res => { + if (res.status === 201) { + scriptStatus.textContent = "Done! Please wait to be redirected..." + flashBackground(); + } else { + res.json().then(data => { + console.log(data) + alert(data.message) + }) + } + }).catch(error => { + scriptStatus.textContent = "Submission request error: " + (error && error.message || error) + }) + } + + else { + scriptStatus.textContent = "Couldn't extract ID from page." + flashBackground(); + } + }) + } + + else if (res.status === 302) { + scriptStatus.textContent = + "Your network is blocked too. To be unblocked, wait several hours without making any more attempts." + +" VPNs, proxies and Tor are always blocked." + flashBackground(); + } + + else if (res.status === 404) { + scriptStatus.textContent = "This profile doesn't exist." + flashBackground(); + } +}) +} diff --git a/src/site/pug/blocked.pug b/src/site/pug/blocked.pug index 2a89a96..0a6131e 100644 --- a/src/site/pug/blocked.pug +++ b/src/site/pug/blocked.pug @@ -19,9 +19,18 @@ html#bibliogram-identifier-blocked .width-block #dynamic-status-area - p If you use Mac or Linux, you can unblock this profile now! Run this in your favourite terminal: - pre curl -Ss #{website_origin}/u/#{username}/unblock.sh | $SHELL ul + li + | #[a(href="#unblock-on-mac-or-linux") Unblock with shell], no download, Mac and Linux only + .hidden-section#unblock-on-mac-or-linux + p Open the Terminal application, then paste this code: + pre curl -Ss #{website_origin}/u/#{username}/unblock.sh | $SHELL + li + | #[a(href="#unblock-with-userscript") Unblock automatically with userscript], any modern browser + .hidden-section#unblock-with-userscript + p If you don't already have a userscript manager, install #[a(href="https://violentmonkey.github.io/get-it/") Violentmonkey] (open source) or #[a(href="https://www.tampermonkey.net/") Tampermonkey] (closed source) + p #[a(href="/userscripts/unblock.user.js") Open this link.] You should be prompted to install the script. Press "Confirm installation" on that page. + p Reload this page. li To learn more, #[a(href="https://github.com/cloudrac3r/bibliogram/wiki/Rate-limits") read about blocking.] li You may be able to avoid this by #[a(href="https://github.com/cloudrac3r/bibliogram/wiki/Instances") browsing on another instance.] li It's good to read scripts to see what they do. #[a(href=`${website_origin}/u/${username}/unblock.sh`) Read ./unblock.sh] diff --git a/src/site/sass/includes/_main.sass b/src/site/sass/includes/_main.sass index 69bbebe..5973e2e 100644 --- a/src/site/sass/includes/_main.sass +++ b/src/site/sass/includes/_main.sass @@ -466,6 +466,13 @@ body margin-top: 10px font-size: 20px + .hidden-section + display: none + padding: 10px + + &:target + display: block + .homepage display: flex flex-direction: column