Skip to content

Instantly share code, notes, and snippets.

@Sasquire
Created April 14, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sasquire/66396e08818f363a56751a18ed1c828b to your computer and use it in GitHub Desktop.
Save Sasquire/66396e08818f363a56751a18ed1c828b to your computer and use it in GitHub Desktop.
A simple userscript that can hide forum threads.
// ==UserScript==
// @name e621 Thread hider
// @namespace https://github.com/sasquire
// @version 0.00001
// @description Allows the ability to hide forum threads
// @author Sasquire
// @match *://*.e621.net/forum_topics
// @match *://*.e621.net/forum_topics?*
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_getValue
// @grant GM_setValue
// @license Unlicense <http://unlicense.org/>
// ==/UserScript==
(() => {
let gm_object = null;
try {
if (window.GM) {
// We are not in a sandbox
gm_object = window.GM;
} else {
// We are in a sandbox
// eslint-disable-next-line no-undef
gm_object = GM;
}
} catch (e) { // e should be a ReferenceError
// We have no access to the GM object. Something is wrong
// Attempt to recover and hope we can make something out of it
gm_object = {};
}
const transitions = [
// Values
['GM_getValue', 'getValue'],
['GM_setValue', 'setValue'],
['GM_listValues', 'listValues'],
['GM_deleteValue', 'deleteValue'],
// GM_getResourceText and GM_getResourceURL
// are not supported in this version because of the
// incompatibility between the GM_* functions and GM 4
// Other
['GM_notification', 'notification'],
['GM_openInTab', 'openInTab'],
['GM_setClipboard', 'setClipboard'],
['GM_xmlhttpRequest', 'xmlHttpRequest'],
['GM_addStyle', 'addStyle']
];
transitions.forEach(([old_id, new_id]) => {
// If this is ever true, we are not in a sandbox. I do not think
// there is a userscript manager that uses both the old GM_* functions
// and sandboxes the code. The day I am wrong is the day this breaks.
const old_function = window[old_id];
if (old_function === undefined) {
return;
}
// Why would we overwrite the new function with a worse old one
if (gm_object[new_id] !== undefined) {
return;
}
// Again, there should not be a time when we get here that the
gm_object[new_id] = (...args) => new Promise((resolve, reject) => {
try {
resolve(old_function(...args));
} catch (error) {
reject(error);
}
});
});
function add_style (css) {
const node = document.createElement('style');
node.type = 'text/css';
node.textContent = css;
const head = document.head;
const body = document.body;
head ? head.appendChild(node) : body.appendChild(node);
}
// If its not present, overwrite with our own function
if (gm_object.addStyle === undefined) {
gm_object.addStyle = add_style;
}
const new_title = document.createElement('th');
new_title.textContent = 'Hide';
const header_row = document.querySelector('#a-index > table > thead > tr');
header_row.insertBefore(new_title, header_row.firstElementChild);
get_rows().forEach(e => {
const data = document.createElement('td');
const hide_button = document.createElement('button');
hide_button.textContent = 'X';
hide_button.addEventListener('click', async p => {
console.log('yes');
await gm_object.setValue(`${get_id_from_row(e)}_hidden`, true);
hide_rows();
});
data.appendChild(hide_button);
e.insertBefore(data, e.firstElementChild);
});
gm_object.addStyle('.hidden { display: none; }');
hide_rows();
function get_rows () {
return Array.from(document.getElementsByClassName('forum-topic-row'));
}
function hide_rows () {
get_rows().forEach(async e => {
const should_hide = await gm_object.getValue(`${get_id_from_row(e)}_hidden`);
if (should_hide === true) {
e.classList.add('hidden');
}
});
}
function get_id_from_row (row) {
return parseInt((/\d+$/u).exec(row.querySelector('a.forum-post-link').href)[0], 10);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment