FC - Hitman

Borra los temas (políticos, fútbol, apuestas, etc..) que quieras de Forocoches (FC)

目前为 2015-12-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         FC - Hitman
// @namespace    https://greasyfork.org/es/scripts/14670-fc-hitman
// @version      0.9
// @description  Borra los temas (políticos, fútbol, apuestas, etc..) que quieras de Forocoches (FC)
// @author       You
// @match        http://*.forocoches.com/*
// @match        https://*.forocoches.com/*
// @match        http://www.forocoches.com/*
// @match        https://www.forocoches.com/*
// @grant        none
// @require 	 http://code.jquery.com/jquery-latest.js
// ==/UserScript==
/* jshint -W097 */
'use strict';

var hitman = function ()
{
	var words = {
		politics: [ "riverita", "peperro","político", "Saenz de Santamaria", "Sáenz de Santamaría", "votantes", "pdr snchz", "partidos politicos", "partidos políticos", "partido político", "partido politico", "Pedro Sánchez", "Pedro Sanchez", "rajoy","soraya", "falange", "pableras", "upyd", "coletas","psoe","debate", "carmena", "voto", "pablemos", "ciudadanos", "ciutadans", "discurso", "albert", "rivera", "iglesias", "elecciones", "votar", "votación", "votacion", "pedro sanchez", "pedro sánchez", "podemos", "podemitas", "pp"],
		football: ["real madrid", "futbol", "fútbol", "porra", "apuesta", "penya culer", "peña culer", "culers", "culeros", "culerdos", "FC Barcelona", "barça", "Barsa", "atleti", "PEÑA VALENCIANISTA", "merengue"]
	},
	censorList = [];
	
	var init = function ()
	{
		getCensoredWords();
		removeThreads();
		setupPanel();
	}
	
	var setupPanel = function ()
	{
		$(".cajasprin:nth-of-type(1) tr").eq(1).append('<td><div id="hitman-config">Hitman</div></d>');
		
		var html = parseTemplate(function()
		{/*
			<div id="hitman-panel">
				Esconder temas de:
				<ul>
					<li>
						<label>
							<input type="checkbox" value="politics"> Política
						</label>
					</li>
					<li>
						<label>
							<input type="checkbox" value="football"> Fútbol
						</label>
					</li>
					<li>
						<br>
						<label>
							Filtro propio:<br>
							<textarea name="hitman-custom" placeholder="gore,movistar"></textarea>
						</label>
					</li>
				</ul>
				<button>Guardar cambios</button>
				<br>
				<a href="https://greasyfork.org/es/scripts/14670-fc-hitman/versions">Ver actualizaciones</a>
			</div>
			<style>
				#hitman-config {
					cursor: pointer;
				}
				#hitman-panel {
					display: none;
					position: fixed;
				    background: white;
				    padding: 5px;
				    border: 1px solid;
				    margin-left: 74%;
				    margin-top: 6%;
				}
				#hitman-panel  ul {
					list-style-type: none;
			    	padding: 0px;
			    	margin-top: 0px;
				}
			</style>
		*/});
		$("body").prepend(html);
		
		$("#hitman-config").on("click", function () {
			$('#hitman-panel').toggle();
		});

		$("#hitman-panel input").on("change", function () {
			updateCensor($(this).val(), $(this).is(":checked"));
		});
		$("#hitman-panel textarea").on("change", function () {
			updateCensor("custom", $(this).val());
		});
		
		$("#hitman-panel button").on("click", function () {
			location.reload();
		});
		loadConfig();
	}
	var loadConfig = function ()
	{
		var config = getConfig(),
			k;
		
		for (k in config) {
			if (config[k]) {
				if (k == "custom") {
					$('#hitman-panel textarea[name="hitman-custom"]').val(config[k]);
				} else {
					$("#hitman-panel input[value=" + k + "]").prop("checked", true);
				}
			}
		}
	}
	var parseTemplate = function(func) {
		return func.toString().replace(/^[^\/]+\/\*!?/, '').replace(/\*\/[^\/]+$/, '');
	}
	
	var updateCensor = function (type, status)
	{
		var config = getConfig();
		
		config[type] = status;
		
		setConfig(config);
	}
	
	var getConfig = function ()
	{
		var config = localStorage.hitman;
		
		if (config === undefined) {
			// default config
			return {
				politics: true,
			};
		}
		
		return JSON.parse(config);
	}
	
	var setConfig = function (config) {
		localStorage.hitman = JSON.stringify(config);
	}
	
	// find and delete threads containing the censored words
	var removeThreads = function ()
	{
		// General section
		$('#threadbits_forum_2 tr').each(function ()
		{
			var text = '',
				title = $(this).find("td[title] a").eq(1).text(),
				pollTitle = $(this).find("td[title] a").eq(2).text(),
				intro = $(this).find("td[title]").attr("title");
			
			text = title + " " + intro;
				
			if (pollTitle.length > 4) {
				text += " " + pollTitle;
			}
			
			if (mustBeRemoved(text)) {
				$(this).remove();
			}
		});
		
		// Home
		$('table[border="0"][cellpadding="2"][cellspacing="0"][width="100%"]').eq(3).find("tr").each(function ()
		{
			var $thread = $(this).find('td[align="left"] a').eq(1),
				text = $thread.text() + " " + $thread.attr("title");
				
			if (mustBeRemoved(text)) {
				$(this).remove();
			}
		});
	}
	
	var getCensoredWords = function ()
	{
		if (censorList.length > 0) {
			return censorList;
		}
		
		var config = getConfig(),
			k;
		
		// setup the word censorship list
		for (k in config) {
			if (config[k]) {
				if (k == "custom" && config[k].length > 0) {
					censorList = censorList.concat(config[k].split(","));
				} else {
					censorList = censorList.concat(words[k]);
				}
			}
		}
		
		return censorList;
	}
	
	var mustBeRemoved = function (text)
	{
		var regex,
			k;
		
		for (k in censorList)
		{
			regex = new RegExp(censorList[k], "i");
			
			if (text.search(regex) !== -1) {
				return true;
			}
		}
		return false;
	}
	
	return {
		init: init
	}
}();

$(document).ready(function() {
	hitman.init();
});