Greasy Fork

Google 搜索结果屏蔽

根据网站、网址和关键字来屏蔽不想要的搜索结果

当前为 2016-10-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google 搜索结果屏蔽
// @name:en      Google Search Results Block
// @namespace    Cheez.Search
// @version      0.8
// @description  根据网站、网址和关键字来屏蔽不想要的搜索结果
// @author       以茄之名
// @author:en    Cheez
// @include      https://www.google.*/search*
// @include      https://www.google.*.*/search*
// @require      http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
	'use strict';
	var $=jQuery;
	var blacklist=GM_getValue('blacklist')&&JSON.parse(GM_getValue('blacklist'))||{
		host:[],
		url:[],
		word:[]
	}; 
	var logs=[]; 
	console.log(blacklist);
	$('[onmousedown]').attr('onmousedown','');
	$.each(jQuery('[href*=webcache]'),function(i,o){jQuery(o).attr('href',jQuery(o).attr('href').replace('p:','ps:'));});

	function hid(hosts,urls,words){
		$('.g:not(.hiden) .r>a').filter(function(i,o){

			if($.inArray(o.host,hosts)>-1){
				logs.push({'域名':o.host,'地址':o.href,'匹配类型':'域名'});
				return true;
			}
			var flag=false;
			$.each(urls,function(i,oo){
				if( (new RegExp(oo)).test(o.href) ){
					logs.push({'域名':o.host,'地址':o.href,'匹配类型':'地址','匹配':oo});
					flag=true;
					return false;
				}
			});
			$.each(words,function(i,oo){
				if(  o.textContent.indexOf(oo) >-1 ){
					logs.push({'域名':o.host,'地址':o.href,'匹配类型':'关键字','匹配':oo});
					flag=true;
					return false;
				}
			});


			if(!flag){
				var p=$(o).parents('.g');
				var menu=$(p).find('.action-menu-panel>ol');
				if( menu.size() ){ 
					menu.filter(':not(:has(.block))').append(
						'<li class="action-menu-item ab_dropdownitem" role="menuitem" aria-selected="false"><a class="fl block-site"  data-url="'+o.href+'" tabindex="-1">屏蔽该类网址</a></li>'+'<li class="action-menu-item ab_dropdownitem" role="menuitem" aria-selected="false"><a class="fl block"  data-host="'+o.host+'" tabindex="-1">屏蔽该网站</a></li>');
				}else{
					var text='<div class="action-menu ab_ctl"><a class="_Fmb ab_button" href="#"  aria-label="结果详情" aria-expanded="false" aria-haspopup="true" role="button" jsaction="m.tdd;keydown:m.hbke;keypress:m.mskpe" ><span class="mn-dwn-arw"></span></a><div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue"><ol>';
					$(p).find('.kv').append(text+'<li class="action-menu-item ab_dropdownitem" role="menuitem" aria-selected="false"><a class="fl block-site" data-url="'+o.baseURI+'" tabindex=" -1"="">屏蔽该类网址</a></li></ol></div></div>');
					$(p).find('.action-menu-panel>ol').filter(':not(:has(.block))').append('<li class="action-menu-item ab_dropdownitem" role="menuitem" aria-selected="false"><a class="fl block" data-host="'+o.host+'" tabindex=" -1"="">屏蔽该网站</a>');

				}
			}

			return flag;
		}).parents('.g').addClass('hiden').hide();
		if(logs.length){console.table(logs);logs=[];}

	}
	var aThing;
	$(document).on('DOMNodeInserted', function () {
		clearTimeout(aThing);
		aThing = setTimeout(function () {
			$(document).trigger('aThing');
		}, 2);
	});

	$(document).on('aThing', function () { //监听DOM树插入
		hid(blacklist.host,blacklist.url,blacklist.word);
		$('[onmousedown]').attr('onmousedown','');
		$.each(jQuery('[href*=webcache]'),function(i,o){jQuery(o).attr('href',jQuery(o).attr('href').replace('p:','ps:'));});
	});
	hid(blacklist.host,blacklist.url,blacklist.word);
	$('#rso').delegate('a.block','click',function(e){
		$(this).parents('.g').addClass('hiden').hide();
		blacklist.host.push($(this).data('host'));
		GM_setValue('blacklist',JSON.stringify(blacklist));
		hid([$(this).data('host')],[],[]);
	});
	$('#rso').delegate('a.block-site','click',function(e){
		var mch=prompt("请输入对应的匹配",$(this).data('url'));
		if(mch){
			mch=str2reg(mch);
			$(this).parents('.g').addClass('hiden').hide();
			blacklist.url.push(mch);
			GM_setValue('blacklist',JSON.stringify(blacklist));
			hid([],[mch],[]);
		}
	});


	function str2reg(str){
		return str.replace(/(?!\\)\$\(\)\*\+\.\[\]\?\^\{\}\|\\/g,/\\$1/);
	}

})();