Last active
September 9, 2017 08:28
-
-
Save biaocy/4ccbac82a99949ea49fdd062491f442c to your computer and use it in GitHub Desktop.
block weibo keyword
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 weibo4b | |
// @namespace http://tampermonkey.net | |
// @version 0.3 | |
// @description 屏蔽匹配关键字的微博 | |
// @author biaocy | |
// @match https://weibo.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var NOT_ALLOW_KW = ['高考', '送考', '备考']; | |
var observeDOM = (function() { | |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver, | |
eventListenerSupported = window.addEventListener; | |
return function(obj, callback) { | |
if( MutationObserver ) { | |
var obs = new MutationObserver(function(mutations, observer) { | |
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ) | |
callback(mutations); | |
}); | |
obs.observe( obj, { childList:true, subtree:true }); | |
} else if( eventListenerSupported ) { | |
obj.addEventListener('DOMNodeInserted', callback, false); | |
obj.addEventListener('DOMNodeRemoved', callback, false); | |
} | |
}; | |
})(); | |
observeDOM( document.body, function(mutations) { | |
for(let mutation of mutations) { | |
for(let node of mutation.addedNodes) { | |
if( node.nodeType === Node.ELEMENT_NODE) { | |
var textEls = node.querySelectorAll('.WB_text'); | |
for(let el of textEls) { | |
for(let kw of NOT_ALLOW_KW) { | |
if(el.textContent.indexOf(kw) > -1) { | |
let p = parent(el, 'WB_cardwrap WB_feed_type S_bg2'); | |
if( p ) { | |
let nickname = p.querySelector('a[nick-name]'); | |
let lnk = p.querySelector('a[node-type="feed_list_item_date"]'); | |
console.log('Deleted | %s | %s | %s', nickname, el.textContent, lnk.href); | |
p.remove(); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
function parent(el, className) { | |
var pEl; | |
while(el) { | |
if(el.className && el.className.indexOf(className) > -1) { | |
pEl = el; | |
break; | |
} | |
el = el.parentElement; | |
} | |
return pEl; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment