Last active
October 25, 2021 20:56
-
-
Save graphnode/fbf9013d3eb589a9da52 to your computer and use it in GitHub Desktop.
Spacebattles & Sufficient Velocity DeWormer
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 Spacebattles & Sufficient Velocity DeWormer | |
// @namespace https://gist.github.com/graphnode/fbf9013d3eb589a9da52 | |
// @version 1.3.0 | |
// @description Hide threads based on a simple filter on some xenforo forums. | |
// @author Diogo Gomes <[email protected]> | |
// @match https://forums.spacebattles.com/* | |
// @match https://forums.sufficientvelocity.com/* | |
// @exclude https://forums.spacebattles.com/watched/threads | |
// @exclude https://forums.sufficientvelocity.com/watched/threads | |
// @exclude http://www.alternatehistory.com/forum/watched/threads | |
// @downloadURL https://gist.github.com/graphnode/fbf9013d3eb589a9da52/raw/dewormer.user.js | |
// @updateURL https://gist.github.com/graphnode/fbf9013d3eb589a9da52/raw/dewormer.user.js | |
// @supportURL https://forums.spacebattles.com/threads/hiding-threads.372550/ | |
// @icon https://www.google.com/s2/favicons?domain=spacebattles.com | |
// @grant none | |
// ==/UserScript== | |
/* jshint esnext: true */ | |
(function() { | |
'use strict'; | |
console.info('Spacebattles & Sufficient Velocity DeWormer 1.3.0'); | |
var filterBar = document.querySelector('.filterBar'); | |
filterBar.insertAdjacentHTML('beforeend', '<a class="filterBar-menuTrigger" data-xf-click="menu" role="button" tabindex="0" aria-expanded="false" aria-haspopup="true">DeWormer</a>'); | |
filterBar.insertAdjacentHTML('beforeend', ` | |
<div class="menu menu--wide" data-menu="menu" aria-hidden="true"> | |
<div class="menu-content"> | |
<h4 class="menu-header">DeWormer</h4> | |
<div class="js-filterMenuBody"> | |
<form method="post" id="dewormer-form"> | |
<div class="menu-row menu-row--separated"> | |
<label for="ctrl_dewormer_text">Filter threads by:</label> | |
<div class="u-inputSpacer"> | |
<input type="text" class="input" data-single="true" name="dewormer_text" maxlength="50" id="ctrl_dewormer_text" placeholder="Words separated by commas or spaces." autocomplete="off"> | |
</div> | |
</div> | |
<div class="menu-footer"> | |
<span class="menu-footer-controls"> | |
<button type="submit" class="button--primary button"><span class="button-text">Filter</span></button> | |
</span> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
`); | |
// Backwards compatibility: | |
if (localStorage.getItem('deworm-keywords') !== null) { | |
var keywords = JSON.parse(localStorage.getItem('deworm-keywords')); | |
localStorage.setItem('deworm-searchstring', keywords.join(', ')); | |
localStorage.removeItem('deworm-keywords'); | |
} | |
var searchString = localStorage.getItem('deworm-searchstring') || ''; | |
var optionsForm = document.querySelector('#dewormer-form'); | |
if (optionsForm !== null) { | |
optionsForm.addEventListener('submit', (e) => { | |
e.preventDefault(); | |
var inputValue = optionsForm.querySelector('#ctrl_dewormer_text').value; | |
localStorage.setItem('deworm-searchstring', inputValue); | |
location.reload(); | |
return false; | |
}); | |
document.querySelector('#ctrl_dewormer_text').value = searchString; | |
} | |
if (searchString === '') { | |
return; | |
} | |
var searchTokens = []; | |
// Tokenize the search string: | |
var tokenizer = /"(.+?)"|(\w+(?=[\s,]|$))/gi; | |
var matches; | |
while ((matches = tokenizer.exec(searchString))) { | |
searchTokens.push(matches[1] || matches[0]); | |
} | |
// Build the search regex: | |
var searchRegexPattern = '\\b' + searchTokens.map(t => t.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')).join('\\b|\\b') + '\\b'; | |
// Do the stuff. | |
var threads = Array.from(document.querySelectorAll('.structItem--thread')).filter((el) => { | |
let titleEl = el.querySelector('.structItem-title'); | |
return titleEl && (new RegExp(searchRegexPattern, 'gi')).test(titleEl.innerText); | |
}); | |
if (threads.length === 0) { | |
return; | |
} | |
threads.forEach((el) => { | |
el.style.display = 'none'; | |
}); | |
let block = document.querySelector('.block-outer.block-outer--after:last-child'); | |
block.insertAdjacentHTML('beforeend', ` | |
<div class="block-outer-opposite"><a href="javascript:" class="deworm-revert showIgnoredLink js-showIgnored" data-xf-init="tooltip" data-original-title="Revert hidden content by DeWormer" id="js-XFUniqueId237">${threads.length} threads were hidden. Click here to unhide.</a></div> | |
`); | |
document.querySelector('.deworm-revert').addEventListener('click', (e) => { | |
e.preventDefault(); | |
threads.forEach((el) => { | |
el.style.display = ''; | |
el.style.opacity = 0.3; | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment