Skip to content

Instantly share code, notes, and snippets.

@elebetsamer
Last active March 31, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elebetsamer/dd719bbde83a9c54d609fd8b5d8ef6e8 to your computer and use it in GitHub Desktop.
Save elebetsamer/dd719bbde83a9c54d609fd8b5d8ef6e8 to your computer and use it in GitHub Desktop.
User Script - Remove yahoo.com news articles
// ==UserScript==
// @name Remove yahoo.com news articles
// @namespace https://github.com/elebetsamer
// @version 0.3
// @description Remove unwanted news articles from the yahoo.com home page. You can set categories to block or sources.
// @author elebetsamer
// @match http*://*.yahoo.com
// @icon https://mbp.yimg.com/sy/rz/l/favicon.ico
// @grant none
// ==/UserScript==
// For a screen shot with some notes, view here: https://user-images.githubusercontent.com/75610/38160042-eb867558-346a-11e8-8729-d36bcdb784f7.png
// How often do you want to remove blocked articles
var intervalSeconds = 5;
// List the categories you want blocked
// example: ['Celebrity', 'Lifestyle', 'Style']
// example (if you don't want to block any categories): []
var blockedCategories = ['Celebrity', 'Lifestyle', 'Style'];
// List the news sources you want blocked
// example: ['Yahoo Lifestyle', 'Esquire']
// example (if you don't want to block any article sources): []
var blockedSources = ['Yahoo Lifestyle', 'Esquire'];
var styleRules = [];
// Hide the "Sponsored" news
styleRules.push('li.js-stream-ad {display: none;}');
applyStyles();
removeNews();
function applyStyles() {
if (!document.getElementById('custom-yahoo-styles')) {
// An element where we can put in custom styles
const styles = document.createElement('style');
styles.id = 'custom-yahoo-styles';
// Set the content for our style element
styles.textContent = styleRules.join();
// Add the style element to the page
document.body.append(styles);
console.info('The custom styles were added to the page.');
}
}
function removeNews() {
const items = document.querySelectorAll('.js-applet-view-container-main .js-stream-content');
console.info('Running removeNews function');
items.forEach(element => {
const headlineCategoryElement = element.querySelector('.strm-headline-label span, .js-content-label span');
const headlineSourceElement = element.querySelector('.strm-headline-label span + span, .js-content-label span + span');
const headlineTitleElement = element.querySelector('.js-stream-item-title span');
if (headlineCategoryElement && headlineSourceElement && headlineTitleElement) {
const articleCategory = headlineCategoryElement.textContent.trim();
const articleSource = headlineSourceElement.textContent.trim();
const articleTitle = headlineTitleElement.textContent.trim();
const isBlockedCategory = blockedCategories.indexOf(articleCategory) != -1;
const isBlockedSource = blockedSources.indexOf(articleSource) != -1;
if (isBlockedCategory) {
element.remove();
console.info(`Removed a news item because it matched a blocked category. Category: "${articleCategory}"; Article: "${articleTitle}"`);
} else if (isBlockedSource) {
element.remove();
console.info(`Removed a news item because it matched a blocked source. Source: "${articleSource}"; Article: "${articleTitle}"`);
}
}
});
setTimeout(removeNews, (intervalSeconds * 1000));
}
@elebetsamer
Copy link
Author

elebetsamer commented Mar 31, 2018

Here is an image of the yahoo.com page with a few notes related to the script.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment