Skip to content

Instantly share code, notes, and snippets.

/gist:10413
Created Sep 12, 2008

Embed
What would you like to do?
// ==UserScript==
// @name NicoNicoFilterWordFilter
// @namespace http://web.zgo.jp/
// @include http://www.nicovideo.jp/watch/*
// ==/UserScript==
(function(){
var commentXML;
var wrapper;//Wrraper使用してるかhttp://tinyurl.com/6fyyqv
var results = new Array();//投稿者フィルターの配列
var flvp = getFlashMovieObject("flvplayer");
var footer = getFirstElementByXPath('//div[@id="flvplayer_container"]',document);
function getFilter(){
var player_info = unsafeWindow.document.getElementById('flvplayer').GetVariable('o');;
/* for flvplayer_wrapper */
if (player_info == "base"){
wrapper = true;
player_info = unsafeWindow.document.getElementById('flvplayer').GetVariable('nico.o');
}
var user = {responseText : decodeURIComponent(player_info)};
if(user.responseText){
return user.responseText;
}else{setTimeout(function() { getFilter(); }, 300);}
}
//コメントデータの取得
function getxml(res){
if(/thread_id=(.+?)&l=(.+?)&.+&ms=(.+?)&/.test(res.responseText)){
var thread_id = RegExp.$1;
var l = RegExp.$2;
var from = -250;
if (l >= 300) {
from = -500;
if (l >= 600) {
from = -1000;
}
}
var url = decodeURIComponent(RegExp.$3);
GM_xmlhttpRequest({
method: 'POST',
headers: { 'Content-type': 'text/xml' },
url: url,
data: '<thread res_from="'+from+'" version="20061206" thread="' + thread_id + '" />',
onload: xmlParser,
onerror: function(res){ GM_log(res.status + ':' + res.statusText); }
});
}
}
//フィルターワード抽出
function xmlParser(res){
commentXML = (new DOMParser).parseFromString(res.responseText, "application/xml");
//GM_log(commentXML);
if(commentXML){
//フィルターワードを含む情報の受け取り
var o = getFilter();
//GM_log(o);
if (/ng_up=(.+)&ng/.test(o)) {
filters = decodeURIComponent(RegExp.$1).split('&');
}
//alert(filters);
for (var j = 0; j < filters.length; j++) {
filters[j].match(/(.+)=(.+)/);
results[j] = RegExp.$1;
//resultにフィルターにかかるワードが入る。
}
}
//GM_log(results);//フィルターにかかるワード配列
commentDel(results);
}
function commentDel(filterWords){
//受け取るのはNGワードの配列
//GM_log("受け取る配列" + filterWords);
var chat = commentXML.getElementsByTagName("chat");
var num = chat.length;//コメントの個数
//GM_log("コメントの取得総数は" + num);
var flag = false;
//GM_log("フィルタの数は" + filterWords.length);
for(var i = 0;i < num;i++){
flag = false;
for(var j = 0;j < filterWords.length;j++){
chatText = chat[i].textContent;
chatWord = new RegExp(filterWords[j], "g");
//GM_log("chatWord " + chatWord);
if(chatWord.test(chatText)){
flag = true;
//GM_log("flag"+ i + flag);
break;
}
}
if(flag){
//GM_log(wrapper);
if(wrapper) {
flvp.SetVariable('nico.Messages.'+ i +'._deleted',1);
} else {
flvp.SetVariable('Messages.'+ i +'._deleted',1);
}
//GM_log("ignore "+num+" no "+no+ " mail " +mail);
}
}
}
//フィルター開始のボタンの関数
function getFirstElementByXPath(xpath, node) {
var node = node || document;
var result = document.evaluate(xpath, node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue ? result.singleNodeValue : null;
}
function getFlashMovieObject(movieName){
if (unsafeWindow.document[movieName]) {
return unsafeWindow.document[movieName];
}
}
function addCheckBox2(elm, chkbox, text){
chkbox.type = 'checkbox';
span = document.createElement('span');
span.setAttribute('style','background:#CCCCCC');
span.appendChild(chkbox);
span.appendChild(document.createTextNode(text));
elm.parentNode.insertBefore(span ,elm);
}
//ボタンの作成
var button = document.createElement('input');
button.type = "submit";
button.value = "filter";
button.id = "filter";
button.addEventListener("click", function(){startFlag();}, false);
footer.parentNode.insertBefore(button,footer);
//実行の始まり
function startFlag(){
if(/watch\/([^/]+)$/.test(location.href)){
var video_id = RegExp.$1;
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.nicovideo.jp/api/getflv/' + video_id,
onload: getxml,
onerror: function(res){ GM_log(res.status + ':' + res.statusText); }
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.