Google News Filter

Removes blacklisted articles and news sources from Google News.

当前为 2017-11-05 提交的版本,查看 最新版本

// ==UserScript==
// @name        Google News Filter
// @namespace   http://www.google-news-filter.com
// @description Removes blacklisted articles and news sources from Google News.
// @include     *//news.google.com/*
// @grant       none
// @version     3.0.0b
// @require     http://code.jquery.com/jquery-latest.min.js
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant   GM_getValue
// @grant   GM_setValue
// ==/UserScript==

$(function() {

    // **************** //

        // Initialize user settings window
        // Persistent user settings for greasemonkey scripts using local storage are made possible by the GM_config library:
        // https://github.com/sizzlemctwizzle/GM_config
        function gnf_settings() {

		// define gm_config_panel div
		var gm_config_panel = document.createElement('div');
        // gm_config_panel.draggable=true;
		document.body.appendChild(gm_config_panel);

		GM_config.init({
  		'id': 'gnf_settings_dialog', // id for this instance of GM_config
	  	'title': 'Google News Filter settings',
		  'fields':
  		{
	    'blacklist_terms': // field id
	    	{
  		    'label': '<div style="margin:1em 0 0;padding:0;">Blacklisted Terms <span style="font-weight:normal;color:gray;">(<i>comma-separated, case-insensitive</i>)</span></div>',
	  	    'type': 'textarea',
      		'default': ''
	    	},
  	  	'blacklist_sources': // field id
    		{
  		    'label': '<div style="margin:1em 0 0;padding:0;">Blacklisted News Sources <span style="font-weight:normal;color:gray;">(<i>comma-separated, case-insensitive</i>)</span></div>',
	      	'type': 'textarea',
	      	'default': ''
  	  	},
  	  	'position': // field id
    		{
  		    'label': 'Position:',
	      	'type': 'radio',
	      	'options': ['Left','Right'],
	      	'default': 'Right'
  	  	},
  	  	'disable': // field id
    		{
  		    'label': 'Disable Filtering:',
	      	'type': 'checkbox',
	      	'default': false
  	  	}
  		},
  		 'events': // Callback functions object
		  {
//	  	  'init': function() { alert('onInit()'); },
//		    'open': function() { alert('onOpen()'); },
    		'save': function() { location.reload(); },
//  	  	'close': function() { alert('onClose()'); },
//		    'reset': function() {  }
		  },
            'css': '#gnf_settings_dialog{width:70% !important;top:4em !important;left:15% !important;border:0!important;border-radius:3px!important;padding:3em 1em 1em !important;height:auto !important;box-shadow:0 0 32px #000000 !important;line-height:unset; font-size:1em;} textarea{width:-moz-calc(100% - 1em - 2px)!important;width:calc(100% - 1em - 2px)!important;height:6em;margin:0!important;padding:0.25em 0.5em;font-family:monospace !important;}    #gnf_settings_dialog_header{ font-size:1.25em !important;position:absolute;top:0;right:0;left:0;height:1.618em;background: -moz-linear-gradient(top,#cfcfcf 0%,#a8a8a8 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#cfcfcf), color-stop(100%,#a8a8a8)); background: -webkit-linear-gradient(top,#cfcfcf 0%,#a8a8a8 100%); background: -ms-linear-gradient(top,  #cfcfcf 0%,#a8a8a8 100%); background: linear-gradient(top,#cfcfcf 0%,#a8a8a8 100%);box-shadow:0px 1px 0px rgba(255,255,255,0.5) inset,0px 1px 0px #515151; padding:0.5em 0 0;text-shadow:0px 1px 0px rgba(255,255,255,0.25);color:#444444;}     #gnf_settings_dialog_blacklist_terms_field_label,#gnf_settings_dialog_blacklist_sources_field_label,#gnf_settings_dialog_disable_var {float:left !important;margin-bottom:0.618em !important;}           #gnf_settings_dialog_position_var,#gnf_settings_dialog_disable_var{margin-top:1em!important;margin-right:2em !important;} #gnf_settings_dialog_position_var,#gnf_settings_dialog_position_field_label,#gnf_settings_dialog_field_position{float:left;}      #gnf_settings_dialog_field_position label {margin-right:1em;} #gnf_settings_dialog_buttons_holder {float:right;} #gnf_settings_dialog_saveBtn,#gnf_settings_dialog_closeBtn{width:6em;text-align:center;color:white!important;border-radius:3px;padding:0.5em 1em!important;}  #gnf_settings_dialog_saveBtn{background:cornflowerblue !important;border:solid 1px cornflowerblue!important;}  #gnf_settings_dialog_closeBtn{background:white!important;color:#444444 !important;border:solid 1px #444444 !important;}  #gnf_settings_dialog_saveBtn:hover{background:#2e8b57!important;border:solid 1px #2e8b57!important;} #gnf_settings_dialog_saveBtn:active{border:solid 1px #2e8b57!important;}  #gnf_settings_dialog_closeBtn:active{background:cornflowerblue!important;color:white!important;border:solid 1px cornflowerblue!important;}  .reset_holder {display:none!important;}',
  		'frame':gm_config_panel
		});
	}
	gnf_settings();

	// **************** //

        $('#gnf_wrapper').remove();
    
        // Create Google News Filter settings element
        var $gnf_el = '<div id="gnf_wrapper" style="background-color:white;position:absolute;top:0;right:0;padding:0.125em 1em;z-index:1000;cursor:pointer;border-bottom-left-radius:3px;border-bottom-right-radius:3px;box-shadow:0 0 12px #AAAAAA;color:#888888;"><div id="gnf_title" style="display:block;font-weight:bold;position:relative;z-index:1001;">Google News Filter</div><div id="gnf_statistics" title="Click to show matched terms."></div><div id="gnf_settings" title="Click to show settings.">Settings</div></div>';
		$('.pGxpHc').css('position','relative').after($gnf_el);
		$('#gnf_statistics,#gnf_settings').hide();

        // define variables
        var $gnf_wrapper = $('#gnf_wrapper');
        var $gnf_statistics = $('#gnf_statistics');
        var $gnf_settings = $('#gnf_settings');
        var $count = 0;
        var $matched = '';
        var $story = '';

    function google_news_filter() {

        // Open GM_config window, focus blacklist textarea, change default "close window" text
        $gnf_settings.on('click',function() {
            GM_config.open();
            $('#gnf_settings_dialog_field_blacklist_terms').focus().add('#gnf_settings_dialog_field_blacklist_sources').css({'font-family':'monospace'});
            $('#gnf_settings_dialog_closeBtn').text('Cancel');
            return false;
        });

        // Main Google News Filter function: the magic happens here!
        if ( GM_config.get('disable') == 1 ) {
			$gnf_statistics.attr('title','').append('<span style="color:red">! Filtering disabled</span>');
		} else {
		// get blacklist terms from GM_config local storage, convert to lowercase, trim whitespace, escape punctuation, convert to array, remove empty elements:
            var	$blacklist_terms = GM_config.get('blacklist_terms').toLowerCase().trim().replace(/\s+,\s+|,\s+|\s+,|,,|\r/g,',').replace(/,$/,'').replace(/'/g,"\'").replace(/"/g,'\"');
            var $blacklist = $blacklist_terms.split(',');
            $blacklist = $blacklist.filter(function(n,i) { return n !== ''; });

            var	$blacklist_sources = GM_config.get('blacklist_sources').toLowerCase().trim().replace(/\s+,\s+|,\s+|\s+,|,,|\r/g,',').replace(/,$/,'').replace(/'/g,"\'").replace(/"/g,'\"');
            var $sources = $blacklist_sources.split(',');
            $sources = $sources.filter(function(n,i) { return n !== ''; });

            // remove blacklisted stories
            $('.v4IxVd').each(function() {
              var $titletext = $(this).text().toLowerCase();
			  for (var i=0; i < $blacklist.length; i++) {
                 if ( $titletext.indexOf( $blacklist[i] ) > -1 ) {
		    	   $(this).parents('.PaqQNc.f2t20b').remove();
                   $count = $count + 1;
			       $matched += $blacklist[i] + '<br>';
                   break;
		    	 }
                 if ($count == 1) { $story = " story";} else { $story = " stories"; }
              }
              // update statistics and add matched terms:
              $gnf_statistics.text($count + $story +' removed').append('<p id="matched" style="display:none;margin:0 0 0.5em 1em;-moz-columns:2;columns:2;"><i>Matched terms:</i><br>'+$matched+'</p>');
            });

            // remove blacklisted sources
			$('.IH8C7b.Pc0Wt').each(function() {
			  var $sourcetext = $(this).text().toLowerCase();
              var $parentContainer = $(this).closest('c-wiz');
			  for (var i=0; i < $sources.length; i++) {
                if ( $sourcetext.indexOf( $sources[i] ) > -1 ) {
                    $parentContainer.remove(); //.append($related).prepend('<div  class="esc-lead-article-title-wrapper"><p><span class="titletext">[ blacklisted story source removed: '+ $sourcetext +' ]</span></p></div>');
                    break;
                  }
                }
			});
			// remove secondary articles and right-column links;
			// doesn't work with "editor's picks" because they are JSON requests
			//$('.M1Uqc.z1uJ2c,.M1Uqc.hvZwjc').each(function() {
		  	//var $sourcetext = $(this).find('a').text().toLowerCase();
		  	//for (var i=0; i < $sources.length; i++) {
		    //	if ( $sourcetext.indexOf( $sources[i] ) > -1 ) {
		    //  	$(this).remove();
			//      break;
		  	//  }
		  	//}
			//});
		} // end else

        // Events
        // show statistics and settings labels on hover
        $gnf_wrapper.on('click','#gnf_title',function(e) {
            e.stopPropagation();
        });
        // hover colors
        $gnf_wrapper.on('mouseenter',function() {
            $(this).css({'color':'#444444'});
        });
        $gnf_wrapper.on('mouseleave',function() {
            $('#gnf_statistics p').hide();
            $(this).css({'color':'#888888'});
        });
        $gnf_wrapper.find('#gnf_settings,#gnf_statistics').hover(function() {
            $(this).css({'color':'#000000'});
        }, function() {
            $(this).css({'color':'unset'});
        });

    } // end google_news_filter function

    google_news_filter();


    // trying to fire gnf function after loading new stories
    // for now, we have to click the "Google News Filter" title -- but this only works once.
    $gnf_wrapper.find('#gnf_title').on('click',function(e) {
        $count = 0;
        $matched = '';
   //     $story = '';
        google_news_filter();
    });



    var oldOpen = XMLHttpRequest.prototype.open;
    var $incr = 1;

    function onStateChange(event) {
        if ( this.readyState !== 4 || this.status !== 200 ) {
            return;
        }
        if ( this.readyState == 4 && this.status == 200 && $incr == 1 ) {
            //google_news_filter();
         //   alert("CLICK READY!");
            $incr = $incr + 1 ;
            //document.getElementById("gnf_wrapper").innerHTML = this.responseText;
            //document.getElementById("gnf_wrapper").innerHTML = this.getAllResponseHeaders();
        }
    }

    XMLHttpRequest.prototype.open = function() {
    // when an XHR object is opened, add a listener for its readystatechange events
        this.addEventListener("readystatechange", onStateChange);

        // run the real `open`
        oldOpen.apply(this, arguments);
    };

    
    // **************** //

        $gnf_wrapper.on('mouseenter mouseleave',function(e) {
            e.stopPropagation();
            $('#gnf_statistics,#gnf_settings').toggle();
        });
        // show/hide matched terms on click:
        $gnf_wrapper.on('click',$gnf_statistics,function(e) {
            e.stopPropagation();
            if ( $('#matched:visible').length ) {
                $('#matched').hide(500);
            } else {
                $('#matched').show(500);
            }
//            $('#matched').toggle(500);
        });

    // Google News Filter options

 	// position Google News Filter element
 	function gnf_el_position() {
	 	var	$position = GM_config.get('position');
		if ( $position == 'Left' ) {
			$gnf_wrapper.css({'left':0,
			'right':'unset'});
		}
	}
	gnf_el_position();



});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址