Skip to content

Commit 91bb88c

Browse files
committed
Add reddit blocker
1 parent 181eba4 commit 91bb88c

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

block/reddit.user.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// ==UserScript==
2+
// @name Ad Block: Mobile Reddit
3+
// @namespace xarantolus
4+
// @version 0.0.1
5+
// @description Removes sponsored posts and ads on Reddit
6+
// @author xarantolus
7+
// @match *://reddit.com/*
8+
// @match *://*.reddit.com/*
9+
// @grant none
10+
// @run-at document-end
11+
// @homepage https://userscripts.010.one
12+
// @url_source https://github.com/xarantolus/bromite-userscripts/releases/latest/download/reddit.user.js
13+
// ==/UserScript==
14+
15+
var log = function (...data) {
16+
console.log("[Ad Block: Reddit]:", ...data);
17+
};
18+
19+
var scriptFun = function () {
20+
// Source: http://ryanmorr.com/using-mutation-observers-to-watch-for-element-availability/
21+
(function (win) {
22+
"use strict";
23+
24+
var listeners = [],
25+
doc = win.document,
26+
MutationObserver = win.MutationObserver || win.WebKitMutationObserver,
27+
observer;
28+
29+
function ready(selector, fn) {
30+
// Store the selector and callback to be monitored
31+
listeners.push({
32+
selector: selector,
33+
fn: fn,
34+
});
35+
if (!observer) {
36+
// Watch for changes in the document
37+
observer = new MutationObserver(check);
38+
observer.observe(doc.documentElement, {
39+
childList: true,
40+
subtree: true,
41+
});
42+
}
43+
// Check if the element is currently in the DOM
44+
check();
45+
}
46+
47+
function check() {
48+
// Check the DOM for elements matching a stored selector
49+
for (
50+
var i = 0, len = listeners.length, listener, elements;
51+
i < len;
52+
i++
53+
) {
54+
listener = listeners[i];
55+
// Query for elements matching the specified selector
56+
elements = doc.querySelectorAll(listener.selector);
57+
for (var j = 0, jLen = elements.length, element; j < jLen; j++) {
58+
element = elements[j];
59+
// Make sure the callback isn't invoked with the
60+
// same element more than once
61+
if (!element.ready) {
62+
element.ready = true;
63+
// Invoke the callback with the element
64+
listener.fn.call(element, element);
65+
}
66+
}
67+
}
68+
}
69+
70+
// Expose `ready`
71+
win.ready = ready;
72+
})(this);
73+
74+
function removeWithReason(adType) {
75+
return function (element) {
76+
element.remove();
77+
log("Removed " + adType);
78+
};
79+
}
80+
81+
// removeIfAd returns a function that gets an HTML element and removes it if it's an ad
82+
function removeAd(adType) {
83+
var remove = removeWithReason(adType);
84+
return function (element) {
85+
remove(element);
86+
};
87+
}
88+
89+
ready("shreddit-ad-post", removeAd("feed ad"));
90+
ready("shreddit-comments-page-ad", removeAd("comments page ad"));
91+
};
92+
93+
scriptFun()
94+
95+
96+
// if (document.readyState == "complete") {
97+
// scriptFun();
98+
// log("Ran on completed document");
99+
// } else {
100+
// window.addEventListener("load", scriptFun);
101+
// log("Registered as 'load' event listener");
102+
// }

0 commit comments

Comments
 (0)