-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgur_top_user.user.js
More file actions
164 lines (155 loc) · 5 KB
/
imgur_top_user.user.js
File metadata and controls
164 lines (155 loc) · 5 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
// ==UserScript==
// @name imgur_top_users
// @namespace someName
// @include http://imgur.com/*
// @version 0.1a
// @grant none
// ==/UserScript==
/*
* BUGS:
*
* TODO:
* - Add flag for active/deactivated state
* - remove "bad replies" label when removed one was the last
*/
var GR_COOKIE_NAME = 'imgur_top_user';
var up_ids = {};
/*
* Patch for GM_getValue and GM_SetValue support for chrome
* credits to: www.devign.me/greasemonkey-gm_getvaluegm_setvalue-functions-for-google-chrome/
*/
if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
this.GM_getValue=function (key,def) {
return localStorage[key] || def;
};
this.GM_setValue=function (key,value) {
return localStorage[key]=value;
};
this.GM_deleteValue=function (key) {
return delete localStorage[key];
};
}
// wait for the helper functions (observer, gui) to be initialised.
function wait_for_helper(){
if(window._imgur_helper_loaded) return run();
window.setTimeout(wait_for_helper, 100);
}
function init_config_GUI(){
var space = $('#mod_settings');
var content = $('<div class="textbox"></div>');
// TODO: add better gui
var table = $('<table style="width:100%;"><tr><th>ID</th><th>Supposed user</th><th>Delete</th></tr>');
for(var id in up_ids){
var btn = $('<button>X</button>');
btn.click(function(){
var uid = $(this).parent().parent().children().first().html();
console.log('remove uid: ', uid);
delete up_ids[uid];
//window.setTimeout(function(){GM_setValue(GR_COOKIE_NAME, JSON.stringify(up_ids))}, 0);
GM_setValue(GR_COOKIE_NAME, JSON.stringify(up_ids));
//console.log(JSON.stringify(up_ids));
$(this).parent().parent().remove();
});
var row = $('<tr><td>'+id+'</td><td><a href="http://imgur.com/user/'+up_ids[id]+'">'+up_ids[id]+'</a></td><td></td></tr>');
row.children().last().append(btn);
table.append(row);
}
table.append('</table>');
content.append(table);
space.append("<h2>User comments to top</h2>");
space.append(content);
space.append("<br />");
}
function add_user(){
var author = $(this).parent().parent().parent();
if(this.id === "au2t"){
up_ids[author.attr('data-author')] = author.find('a').first().html();
GM_setValue(GR_COOKIE_NAME, JSON.stringify(up_ids));
console.log('add user ', author.find('a').html());
$(this).attr('id', 'ru2t');
$(this).html('Del User2Top');
alert('User added');
}else{
delete up_ids[author.attr('data-author')];
GM_setValue(GR_COOKIE_NAME, JSON.stringify(up_ids));
console.log('remove user ', author.find('a').html());
$(this).attr('id', 'au2t');
$(this).html('Add User2Top');
alert('User removed');
}
}
function handleComment(n){
//console.log('handle', n);
n.setAttribute("dirty", "1");
var $n = $(n);
var author = $n.find('.author').filter('[data-author]').first();
var uid = author.attr('data-author');
var add_btn = $('<div class="item"></div>');
add_btn.click(add_user);
author.find('.options').append(add_btn);
if(uid && up_ids[uid] != undefined){
add_btn.attr('id', 'ru2t');
add_btn.html('Del User2Top');
var uname = author.find('a').html();
if(up_ids[uid] != uname){
up_ids[uid] = uname;
console.log("update name for id ", uid, " : ", uname);
GM_setValue(GR_COOKIE_NAME, JSON.stringify(up_ids));
}
return window.location.pathname.indexOf('/user/') != 0;
}else{
add_btn.attr('id', 'au2t');
add_btn.html('Add User2Top');
}
return false;
}
function move2top(n){
n.setAttribute("dirty", "1");
var $n = $(n);
if($n.parent().attr('class') === "bad-captions")
$n.parent().parent().prepend($n);
else
$n.parent().prepend($n);
}
// and here we go
wait_for_helper(); // calls run when ready
function run(){
//TODO: clean this up. Its horrible.
//window.gallery_navigated_listener.push(function(){alert('navigated to new galery');})
up_ids = $.parseJSON(GM_getValue(GR_COOKIE_NAME, '{}'));
if( window.location.pathname.indexOf("/account/settings") == 0){
init_config_GUI();
return;
}
// just in case there are already coments loaded (user/comments for example)
$('.comment').each(function(i){
//console.log('foo ', this);
if(this.dirty === undefined && handleComment(this)){
move2top(this);
//$(this).parent().prepend($(this));
}
});
// register for all dynamically loaded comment divs
window.observer_callbacks.push( {filter:function(n){
//console.log('foo2 ', n);
if ( n.className === undefined || n.className != "comment"){
// galeries appends the comment div, while replies for example wraps them.
//if(n.className === undefined || n.className != "comment-item"){
var childs = $(n).find('.comment');
if(childs.length == 0) return false;
childs.each(function(){
//console.log('Has child ', n, this);
if (! this.hasAttribute('dirty') && handleComment(this)){
move2top(this);
}
});
//}
return false;
}
if (! n.hasAttribute('dirty') )
return handleComment(n);
return false;
}, exec:function(n){
move2top(n);
}, tagNames:{'DIV':true}});
}