Last active
July 15, 2023 15:20
-
-
Save catherineomega/23dadd8a0f66411fcb111d4201b8c034 to your computer and use it in GitHub Desktop.
Block toots containing links to a certain UK news site on Mastodon.social
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mastodon Filter Links | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Until we get per-domain link filtering, this is our best option. | |
// @author Catherine Winters | |
// @match https://mastodon.social/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=mastodon.social | |
// @grant none | |
// ==/UserScript== | |
(new MutationObserver(check)).observe(document, {childList: true, subtree: true}); | |
function check(changes, observer) { | |
if(document.querySelector(".drawer__tab")) { | |
console.log("More toots loaded."); | |
//var regex = /dailymail.co.uk/g | |
var regex = /https?:\/\/(?:www\.)?(?:mol\.im|dailymail\.co\.uk)\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi; | |
var textLink = document.querySelectorAll("a"); | |
textLink.forEach( e => { | |
if (regex.test(e.href)) { | |
console.log(e); | |
getParentsUntil(e, 'article'); | |
} | |
}); | |
} | |
} | |
const getParentsUntil = (el, selector) => { | |
let parents = [], | |
_el = el.parentNode; | |
while (_el && typeof _el.matches === 'function') { | |
parents.unshift(_el); | |
if (_el.matches(selector)) { | |
_el.remove(); | |
console.log("Removed toot containing a Daily Mail link."); | |
return parents; | |
} | |
else _el = _el.parentNode; | |
} | |
return []; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment