From 4a5607a395e12a8937a64e248cc1892303b61a5f Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 9 May 2021 04:12:30 +0000 Subject: [PATCH] add script template --- remote_instance_actions.user.js | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 remote_instance_actions.user.js diff --git a/remote_instance_actions.user.js b/remote_instance_actions.user.js new file mode 100644 index 0000000..984f5fb --- /dev/null +++ b/remote_instance_actions.user.js @@ -0,0 +1,79 @@ +// ==UserScript== +// @name Remote Instance Actions +// @namespace https://hollymcfarland.com +// @version 1.0 +// @description Easily limit a remote instance from your Mastodon instance via the timeline +// @author monorail + +// @grant GM_openInTab +// @run-at context-menu +// ==/UserScript== + +(function() { + 'use strict'; + + // Check whether the domain is already limited on the instance, and open the relevant form if not + // This is imperfect, it just does a search of limited domains in the admin backend + // This means it handles substrings weirdly, and interacts weirdly with suspending subdomains + // But there's an override so it's not so bad + function checkStatus(domain, callback) { + let url = `https://${window.location.host}/admin/instances?utf8=%E2%9C%93&limited=1&by_domain=${domain}`; + let request = new XMLHttpRequest(); + request.open('GET', url); + + request.addEventListener('load', function() { + if (request.response.includes("You don't have permission to view this page.")) { + alert("You are not logged in to an administrator account."); + return; + } + if (request.response.includes("No domains found.") || confirm(`${domain} may already be limited. Continue anyway?`)) { + callback(domain); + } + }); + + request.send(); + } + + // Open a new tab to the form for applying moderation actions to the selected domain + function newBlock(domain) { + GM_openInTab(`https://${window.location.host}/admin/domain_blocks/new?_domain=${domain}`, false); + } + + // Clean up the selected domain + function sanitize(domain) { + // clear out accidental whitespace + domain = domain.trim(); + + // in case a full link was provided + domain = domain.replace(/https?:\/\//, ""); + + // in case the domain is being copied from a user@domain string + domain = domain.split("@").slice(-1)[0]; + + // in case the domain has a path included + domain = domain.split("/")[0]; + + return domain; + } + + // Make sure a domain was actually selected + // This is imperfect, but can be overridden in case of a false negative + function validate(domain) { + // For now just check for a domain + tld and no whitespace + return domain.includes(".") && !domain.match(/\s/); + } + + let domain = sanitize(document.getSelection().toString()); + if (!domain.trim()) { + alert("Nothing selected."); + return; + } + + if (validate(domain) || confirm(`Are you sure you meant to select "${domain}"?`)) { + checkStatus(domain, newBlock); + } +})();