don't rely on english text on page

This commit is contained in:
Holly 2021-05-09 21:00:41 +00:00
parent 24e1942cbd
commit 334c6e4629
2 changed files with 20 additions and 4 deletions

View File

@ -25,5 +25,6 @@ For the same reason as the last question. That's how the `@match` directives are
- ~~Figure out why it doesn't work on Firefox~~
- ~~It's because the `@run-at context-menu` header only works in Chrome based browsers. The context menu item *appears* in Firefox but doesn't do anything. Find a workaraound~~
- It's actually because of something extremely different and much, much weirder. `@run-at context-menu` actually does exist in Firefox now, but there's a bizarre CSP enforcement bug that causes Firefox issues. Details on the installation page
- Don't rely on parsing human-readable text, won't work in other languages
- ~~Don't rely on parsing human-readable text, won't work in other languages~~
- There's no good API or anything for this, going to need to parse the document structure
- Done

View File

@ -1,7 +1,7 @@
// ==UserScript==
// @name Remote Instance Actions
// @namespace https://hollymcfarland.com
// @version 0.9
// @version 1.0
// @description Easily limit a remote instance from your Mastodon instance via the timeline
// @author monorail
<?php
@ -21,16 +21,31 @@
// 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) {
function resultsFound(response) {
// reconstruct the results page in memory
const parser = new DOMParser();
const resultsPage = parser.parseFromString(response, "text/html");
// .directory__tag is the class for the div with results, if it exists
// so its presense indicates that a result was found, and the current domain is likely limited
return resultsPage.getElementsByClassName("directory__tag").length;
}
// perform a search for limited instances that match the highlighted text
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&#39;t have permission to view this page.")) {
if (request.status === 403) {
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?`)) {
if (request.status !== 200 && !confirm("Recieved unexpected HTML status code. Continue anyway?")) {
return;
}
if (!resultsFound(request.response) || confirm(`${domain} may already be limited. Continue anyway?`)) {
callback(domain);
}
});