Last active
November 12, 2017 05:10
-
-
Save Patrick-1994-/b3f177186dcf51dab2d14969e22ebf72 to your computer and use it in GitHub Desktop.
Block posts of specific users on speedrun.com
This file contains 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 Speedrun.com post blocker | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description Hides posts (to configure, use the "+" Link next to user names and "Setting -> Block Settings") | |
// @author Patrick | |
// @match *://*.speedrun.com/* | |
// @grant none | |
// @require http://code.jquery.com/jquery-3.2.1.slim.min.js | |
// ==/UserScript== | |
//debugger; | |
// stolen from naomik https://stackoverflow.com/a/17326679 | |
RegExp.escape = function(str) { | |
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | |
}; | |
Array.prototype.removeValue = function(removed) { | |
return this.filter( val => val != removed ); | |
}; | |
Array.prototype.addUnique = function(val) { | |
var tmp = this.removeValue(val); | |
tmp.push(val); | |
return tmp; | |
}; | |
function createBlockSettingsPage(names) { | |
console.log("settings"); | |
$("div.maincontent div.panel").html( createNameList(names) ); | |
$("a.remove_from_block_list").click(function(){ | |
var name = $(this).data("name"); | |
names = names.removeValue(name); | |
$(`li:has( a[data-name='${name}'] )`).remove(); | |
saveBlockList(names); | |
}); | |
} | |
function saveBlockList(names) { | |
localStorage.setItem( "names", JSON.stringify(names) ); | |
} | |
function applyBlocks(names) { | |
if(names.length > 0) { | |
names.forEach(name => { | |
console.log(`${name} blocked`); | |
if( document.location.pathname.match(new RegExp("/(post|thread)")) ) { | |
$(`div.maincontent div.panel:has( div.userinfo:has( span.username:contains('${name}') ) )`).hide(); | |
} | |
/* | |
let notification_regex = new RegExp(`${name} (posted|responded)`); | |
$( "a" ).filter(function(){ | |
var t = $(this).text(); | |
return notification_regex.test( t ); | |
}).html("<i>blocked</i>"); | |
*/ | |
}); | |
} | |
} | |
function createNameList(names) { | |
console.log( names.join(" ") ); | |
var items = []; | |
names.forEach( name => { | |
items = items.addUnique( createListItem(name) ); | |
}); | |
return "<ul>" + items.join("") + "</ul>"; | |
} | |
function createListItem(name) { | |
return `<li>${name} <a class='remove_from_block_list' data-name='${name}' href='javascript:;'>(remove)</a></li>`; | |
} | |
function remove(arr, removed) { | |
return arr.filter( val => val != removed); | |
} | |
(function() { | |
'use strict'; | |
console.log("hi - blocker"); | |
var names; | |
/* Setup */ | |
names = []; | |
if ( localStorage.getItem("names") ) { | |
try { | |
names = JSON.parse( localStorage.getItem("names") ); | |
}catch(e){} | |
} | |
/* Interface */ | |
/* Add user */ | |
$("div.maincontent div.panel span.username").each(function() { | |
var container = $(this); | |
var n = container.text(); | |
var appendTo = container.parent().parent(); | |
appendTo.append(`<a class='add_to_block_list' data-name='${n}' href='javascript:;'>+</a>`); | |
}); | |
/* Manage list */ | |
if( window.location.pathname.includes("/settings") ) { | |
$("div.maincontent ul.nav.nav-tabs").append("<li> <a id='block_settings' href='javascript:;'>Block Settings</a> </li>"); | |
if( window.location.pathname.includes("blocksettings") ) { | |
createBlockSettingsPage(names); | |
} | |
$("#block_settings").click(function(){ | |
createBlockSettingsPage(names); | |
}); | |
} | |
/* Events */ | |
$("a.add_to_block_list").click(function(){ | |
console.log( $(this) ); | |
let name = $(this).data("name"); | |
console.log( "name: " + name ); | |
console.log("_"); | |
console.log("names vorher: " + names); | |
names = names.addUnique(name); | |
saveBlockList(names); | |
applyBlocks(names); | |
}); | |
/* Action */ | |
applyBlocks(names); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment