Skip to content

Instantly share code, notes, and snippets.

@dd0754
Last active November 13, 2021 09:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dd0754/0eff32627de15b24eeb6251e08338aab to your computer and use it in GitHub Desktop.
Save dd0754/0eff32627de15b24eeb6251e08338aab to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name v2ex屏蔽关键字
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author dd0754
// @include *://*.v2ex.com/
// @include *://*.v2ex.com/?tab=*
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
GM_addStyle('#fucker {position: relative;font-size: 14px;}#fucker #block-input {border: 1px solid #ccc;border-radius: 20px;width: 100%;height: 26px;padding: 6px 10px;font-size: 14px;line-height: 1;box-sizing: border-box;outline: 0;}#fucker #block-submit {cursor: pointer;width: 30px;height: 26px;padding: 6px 0;font-size: 14px;line-height: 1;text-align: center;position: absolute;right: 0;top: 0;}#fucker .list {width: 100%;position: absolute;left: 0;top: 30px;border-radius: 3px;padding: 5px 5px 0;background: #fff;border: 1px solid #ccc;box-sizing: border-box;display: none;}#fucker .list span {float: left;padding: 0 5px;border-radius: 5px;margin: 0 5px 5px 0;background: #444;color: #fff;cursor: pointer;}');
var blockTpl = '<div class="box"><div class="inner"><div id="fucker"><input type="text" id="block-input" placeholder="输入要屏蔽的关键词"><div class="list"></div><span id="block-submit"><i class="fa fa-plus-circle"></i></span></div></div></div><div class="sep20"></div>';
$('#Rightbar .box:eq(1)').before(blockTpl);
function update() {
$('.cell').show().filter(function(i, el) {
var title = $(this).text().toLowerCase();
return title && isBlocked(title);
}).hide();
}
function isBlocked(str) {
if (!str) return false;
return blockedList.some(function(el) {
return el && str.indexOf(el) !== -1;
});
}
function block(q) {
if (!q || blockedList.indexOf(q) >= 0) return;
$('#fucker .list').append(`<span>${q} <i class="fa fa-ban"></i></span>`);
$('#block-input').val('');
blockedList.push(q);
localStorage.setItem('blockedList', JSON.stringify(blockedList));
}
function unblock(q) {
if (!q) return;
var index = blockedList.indexOf(q);
if (index >= 0) {
blockedList.splice(index, 1);
localStorage.setItem('blockedList', JSON.stringify(blockedList));
}
}
var blockedList = localStorage.getItem("blockedList");
blockedList = blockedList && JSON.parse(blockedList) || [];
blockedList.forEach(function(el) {
$('#fucker .list').append(`<span>${el} <i class="fa fa-ban"></i></span>`);
});
update();
$('#fucker .list').on('click', 'span', function(event) {
event.stopPropagation();
var q = $(this).text().trim().toLowerCase();
$(this).remove();
unblock(q);
update();
});
$('#fucker').on('click', '#block-submit', function(event) {
event.stopPropagation();
var q = $('#block-input').val().trim().toLowerCase();
if (!q) return;
block(q);
update();
});
$('#fucker').on('focus', '#block-input', function(event) {
event.stopPropagation();
$('#fucker .list').slideDown();
});
$(document).on("click", function(e) {
var target = $(e.target);
if (target.closest("#fucker").length == 0) {
$('#fucker .list').hide();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment