Last active
November 29, 2019 07:25
-
-
Save beshur/c2cf7de0cdb60ad311d9a4761a24af42 to your computer and use it in GitHub Desktop.
9gag-pics-only userscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 9gag-pics-only | |
// @namespace https://buznik.net/ | |
// @version 1.0.0 | |
// @description Hide videos and GIFs from 9gag | |
// @author Alex Buznik | |
// @match https://9gag.com/ | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
/* 9gag-pics-only Content JS */ | |
'use strict'; | |
class NineGagPics { | |
constructor() { | |
this.LOG = '9GAG_PICS_ONLY'; | |
this.postSelector = '.post-view'; | |
this.deferTimer = null; | |
this.singlePost = !!document.querySelectorAll('#individual-post').length; | |
this.active = true; | |
this.getNativeAds = this.getNativeAds.bind(this); | |
console.info(this.LOG, 'Init'); | |
this.setScrollHandler(); | |
this.hideByPostSelector(); | |
} | |
setScrollHandler() { | |
window.addEventListener('scroll', this.onScroll.bind(this)); | |
} | |
onScroll(event) { | |
if (this.deferTimer) { | |
clearTimeout(this.deferTimer); | |
} | |
this.deferTimer = setTimeout(this.hideByPostSelector.bind(this), 100); | |
} | |
hideArticle(item) { | |
if (!item) { | |
return; | |
} | |
return item.closest('article').style.display = "none"; | |
} | |
getNativeAds() { | |
let articles = [...document.querySelectorAll('.list-stream article')]; | |
let articles_no_ids = articles.filter(item => !item.getAttribute('id')); | |
return articles_no_ids; | |
} | |
hideByPostSelector() { | |
if (this.singlePost || !this.active) { | |
return; | |
} | |
let nonImagePosts = [...document.querySelectorAll(this.postSelector)]; | |
let nativeAds = this.getNativeAds(); | |
nonImagePosts.concat(nativeAds).forEach(this.hideArticle.bind(this)); | |
clearTimeout(this.deferTimer); | |
} | |
} | |
const NineGagPicsInstance = new NineGagPics(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment