forked from Wizek/reddit-collapse-by-default
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-collapse-by-default.user.js
151 lines (133 loc) · 4.3 KB
/
reddit-collapse-by-default.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ==UserScript==
// @name Reddit collapse by default
// @namespace http://github.com/Wizek/
// @version 0.2.3
// @description Second level Reddit comments collapsed by default
// @author Milan Nagy
// @contributors joeytwiddle
// @match https://www.reddit.com/*
// @match https://old.reddit.com/*
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle(" \
.sitetable.nestedlisting .comment .tagline .expand-children {\
display: inline-block; \
}\
.child .sitetable .comment .tagline .expand-children {\
display: none; \
}\
.important-show {\
display: inline-block !important; \
}\
");
//$('.noncollapsed > .entry > .tagline > .expand').click()
// Add expand-children comments link
$('<a href="javascript:void(0)" class="expand-children collapsedc" onclick="">[+++]</a><span> </span>').insertAfter('.expand');
// Do not collapse top-level comments
$(document).ready(function(){
var url = window.location.href;
if (localStorage.getItem("redditURL") == url)
{
$('.noncollapsed').click();
var stat1, i = 0, stat2;
stat1 = localStorage.getItem("reddit1State");
if (stat1 != null)
{
$('.expand-children').each(function () {
if (stat1[i] == '1')
{
$(this).click();
}
i++;
});
}
i = 0;
stat2 = localStorage.getItem("reddit2State");
if (stat2 != null)
{
$('.comment').each(function () {
if (stat2[i] == '1')
{
var te = $(this).thing().find('.expand:first');
te.click();
}
i++;
});
}
}
else
{
$('.noncollapsed .noncollapsed > .entry > .tagline > .expand').click();
}
});
$('.expand').click(function(){
var state2='';
$('.comment').each(function(){
if ($(this).hasClass('noncollapsed')){
state2 += '0';
}
else state2 += '1';
});
localStorage.setItem("reddit2State", state2);
});
// definition of expand-children click function
$('.expand-children').click(function(){
var t, n, r, k, count;
$(this).toggleClass("collapsedc noncollapsedc");
if ($(this).text() == '[+++]'){
$(this).text("[---]");
t=$(this).prev().thing(); n=t.find(".expand:first"); r=t.hasClass("collapsed");
// if (r)
// t.toggleClass("collapsed noncollapsed");
k=$(this).parent().parent().next();
t = k.children().children();
count = t.size();
for (obj_index=0;obj_index<count-1;obj_index+=2)
{
n = t.eq(obj_index);
k = n.children(".entry");
n = k.children('.tagline');
k = n.children('.expand');
if (k.thing().hasClass('collapsed'))
n.children(".expand").click();
n.children('.expand-children').addClass('important-show');
}
}
else{
$(this).text("[+++]");
t=$(this).prev().thing(); n=t.find(".expand:first"); r=t.hasClass("noncollapsed");
// if (r)
// t.toggleClass("collapsed noncollapsed");
k=$(this).parent().parent().next();
t = k.children().children();
count = t.size();
for (obj_index=0;obj_index<count-1;obj_index+=2)
{
n = t.eq(obj_index);
k = n.children(".entry");
n = k.children('.tagline');
k = n.children('.expand');
if (k.thing().hasClass('noncollapsed'))
n.children(".expand").click();
n.children('.expand-children').removeClass('important-show');
}
}
var state1='';
$('.expand-children').each(function(){
if ($(this).hasClass('noncollapsedc')){
state1 += '0';
}
else state1 += '1';
});
var state2='';
$('.comment').each(function(){
if ($(this).hasClass('noncollapsed')){
state2 += '0';
}
else state2 += '1';
});
var url = window.location.href;
localStorage.setItem("reddit1State", state1);
localStorage.setItem("reddit2State", state2);
localStorage.setItem("redditURL", url);
});