-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreddit_hide_by_subreddit.user.js
81 lines (71 loc) · 2.53 KB
/
reddit_hide_by_subreddit.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ==UserScript==
// @name Hide Posts By Subreddit
// @namespace https://github.com/StaticPH
// @match https://old.reddit.com/*
// @version 1.1
// @createdAt 4/8/2022, 6:34:35 PM
// @author StaticPH
// @description Hide posts from arbitrary subreddits. ONLY FOR old.reddit.com!
// @license MIT
// @updateURL https://raw.githubusercontent.com/StaticPH/Userscripts/master/reddit_hide_by_subreddit.user.js
// @downloadURL https://raw.githubusercontent.com/StaticPH/Userscripts/master/reddit_hide_by_subreddit.user.js
// @homepageURL https://github.com/StaticPH/UserScripts
// @supportURL https://github.com/StaticPH/UserScripts/issues
// @icon https://www.redditstatic.com/desktop2x/img/favicon/favicon-32x32.png
// @grant GM.setValue
// @grant GM_setValue
// @grant GM.getValue
// @grant GM_getValue
// @grant GM.addStyle
// @grant GM_addStyle
// @noframes
// @run-at document-start
// ==/UserScript==
(function(){
"use strict";
const settings = {
/* An array of names (strings) of subreddits from which posts should be hidden. */
hideSubredditsNamed: [],
};
// Prefer asychronous Greasemonkey4-API GM.* methods, but allow use of GM_* methods as a fallback if necessary.
if (typeof GM === 'undefined'){
this.GM = {};
}
Object.entries({
'GM_getValue': 'getValue',
'GM_setValue': 'setValue',
'GM_addStyle': 'addStyle'
}).forEach((gm3Name, gm4Name) => {
if (GM[gm4Name] === 'undefined'){
const fallback = this[gm3Name];
if (!fallback){
throw new ReferenceError('Unable to find ' + gm3Name + ' to use as fallback for the unavailable GM.' + gm4Name + ' method!');
}
console.log('GM.' + gm4Name + ' is not defined. Falling back to ' + gm3Name + ' Promise.');
GM[gm4Name] = function(...args){
return new Promise((onResolve, onReject) => {
try{ onResolve(fallback.apply(this, args)); }
catch(err){ onReject(err); }
});
}
}
});
function readSettings(){
for(let key in settings){
settings[key] = GM.getValue(key, settings[key]);
}
}
function saveSettings(){
for(let key in settings){
GM.setValue(key, settings[key]);
}
}
readSettings();
saveSettings();
if (settings.hideSubredditsNamed.length < 1){
console.log('No subreddits to hide.');
return;
}
const selectorStrBuilder = (subredditName) => 'div.thing[data-subreddit="' + subredditName + '"]';
GM.addStyle(Array.from(settings.hideSubredditsNamed, selectorStrBuilder).join(', ') + '{ display: none; }');
})();