Skip to content

Instantly share code, notes, and snippets.

@poontology
Created April 20, 2022 10:13
Show Gist options
  • Save poontology/4542efaba51c20ddf9c1885b68883c1d to your computer and use it in GitHub Desktop.
Save poontology/4542efaba51c20ddf9c1885b68883c1d to your computer and use it in GitHub Desktop.
tampermonkey script to filter redgifs tags
// ==UserScript==
// @name redgifs_filter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description remove gifs with specified tags
// @author You
// @match https://www.redgifs.com/*
// @icon https://www.google.com/s2/favicons?domain=redgifs.com
// @grant none
// ==/UserScript==
window._redgifs_filter_removeCount = 0;
function countRemoved() {
window._redgifs_filter_removeCount++;
const cel = document.querySelector("#rf-removecount");
if (cel) {
cel.innerText = window._redgifs_filter_removeCount;
} else {
const css = "background: darkred; border-radius: 18px; padding: 9px; margin: 9px;";
const lel = document.querySelector('.links');
const tag = `<span id="rf-removecount" title="removed by redgifs_filter" style="${css}">1</span>`;
if(lel) lel.insertAdjacentHTML('afterend', tag);
}
}
(function() {
'use strict';
const excludeTags = ["#Cuckold", "#Exhibitionism"];
// /watch dom structure is different from /browse /users etc
// (also location updated dynamically, re-eval on every event)
const isWatchPage = () => location.pathname.startsWith("/watch");
function getTags(el) {
const selector = isWatchPage() ? ".tags a.tag span.name" : ".tags li";
let ret = Array.from(el.querySelectorAll(selector)).map(e => e.innerText);
return ret.map(t => t.startsWith("#") ? t : "#" + t);
}
function removeExcluded(el) {
try {
if (!el || !el.querySelectorAll) return;
if (isWatchPage()) {
el = el.closest('.feed-item');
} else {
el = el.closest('.tile');
}
if (!el || !el.querySelectorAll) return;
const tags = getTags(el);
console.log(tags, el)
if(tags.find(t => excludeTags.includes(t))) {
el.remove();
countRemoved();
}
} catch (ex) {
console.error("redgifs_filter", ex)
}
}
function observeChanges(el) {
new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(removeExcluded);
});
}).observe(el, {
// .tile added first .meta later so need subtree
characterData: false, attributes: false, childList: true, subtree: true
});
el.querySelectorAll(".tile").forEach(removeExcluded);
}
observeChanges(document.querySelector("#app"));
})();
@hazushi3
Copy link

hazushi3 commented Jun 1, 2024

if anyone stumbles on this, I wrote an updated version: https://gist.github.com/hazushi3/3c995294eeb7baff6694a29f9d457e83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment