// ==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 2.0.0
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
$(function() {
function google_news_filter() {
// 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'
}
},
'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;} textarea{width:-moz-calc(100% - 1em - 2px)!important;width:calc(100% - 1em - 2px)!important;height:6em;margin:0!important;padding:0.25em 0.5em;} #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 {float:left !important;margin-bottom:0.618em !important;} #gnf_settings_dialog_position_var{margin-top:1em!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();
// **************** //
// Create Google News Filter element
function gnf_el() {
var $gnf_el = '<div id="gnf_wrapper" style="background-color:white;position:fixed;top:0;right:0;padding:0.125em 1em;z-index:1000;cursor:pointer;box-shadow:0 0 12px #AAAAAA;color:#888888;"><span style="display:block;font-weight:bold;">Google News Filter</span><div id="gnf_statistics" title="Click to show matched terms."></div><div id="gnf_settings" title="Click to show settings.">Settings</div></div>';
$('#main-wrapper').prepend($gnf_el);
$('#gnf_statistics,#gnf_settings').hide();
}
gnf_el();
// **************** //
// define variables
var $gnf_wrapper = $('#gnf_wrapper');
var $gnf_statistics = $('#gnf_statistics');
var $gnf_settings = $('#gnf_settings');
var $count = 0;
var $matched = '';
var $story = '';
// 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();
$('#gnf_settings_dialog_closeBtn').text('Cancel');
return false;
});
// **************** //
// Main Google News Filter function: the magic happens here!
function gnf_filter_stories() {
// 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
function remove_blacklisted_stories() {
$('.titletext,.source-pref,.esc-lead-snippet-wrapper').each(function() {
var $titletext = $(this).text().toLowerCase();
for (var i=0; i < $blacklist.length; i++) {
if ( $titletext.indexOf( $blacklist[i] ) > -1 ) {
$(this).parents('.blended-wrapper,.small-story').remove();
$count = $count + 1;
$matched += $blacklist[i] + '<br>';
break
}
$count == 1 ? $story = " story" : $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_stories();
// remove blacklisted sources
function remove_blacklisted_sources() {
$('.al-attribution-source').each(function() {
var $sourcetext = $(this).text().toLowerCase();
var $parentContainer = $(this).parents('.esc-wrapper');
var $related = $(this).parents('.esc-lead-article-source-wrapper')
.siblings('.esc-extension-wrapper');
for (var i=0; i < $sources.length; i++) {
if ( $sourcetext.indexOf( $sources[i] ) > -1 ) {
$related.css('display','block').remove();
$parentContainer.find('.esc-body').empty().append($related).prepend('<div class="esc-lead-article-title-wrapper"><h2><span class="titletext">[ blacklisted story source removed: '+ $sourcetext +' ]</span></h2></div>');
break
}
}
});
// remove secondary articles and right-column links;
// doesn't work with "editor's picks" because they are JSON requests
$('.esc-secondary-article-source,.esc-diversity-article-source,.small-story .source-pref').each(function() {
var $sourcetext = $(this).text().toLowerCase();
var $parentContainer = $(this).closest('.esc-secondary-article-wrapper,.esc-diversity-article-wrapper,.small-story');
for (var i=0; i < $sources.length; i++) {
if ( $sourcetext.indexOf( $sources[i] ) > -1 ) {
$parentContainer.remove();
break
}
}
});
}
remove_blacklisted_sources();
}
gnf_filter_stories();
// **************** //
// Events
// show statistics and settings labels on hover
$gnf_wrapper.on('mouseenter mouseleave',function() {
$('#gnf_statistics,#gnf_settings').toggle();
});
// show/hide matched terms on click:
$gnf_wrapper.on('click',$gnf_statistics,function() {
$('#matched').toggle(500);
});
// 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'});
});
// **************** //
// 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();
}; // end
google_news_filter();
// **************** //
});