augustl (owner)

Revisions

gist: 50561 Download_button fork
public
Public Clone URL: git://gist.github.com/50561.git
Embed All Files: show embed
twitterhashtagignore.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Twitter Hashtag Ignore
// version 0.3
// 2008-01-25
// Copyright (c) 2009, August Lilleaas
// Released under the MIT license
// http://www.opensource.org/licenses/mit-license.php
//
// ==UserScript==
// @name Twitter Hashtag Ignore
// @namespace http://www.twitter.com/*
// @description Ignore tweets that contains hashtags you specify.
// @include http://twitter.com/
// @include http://twitter.com/*
// @include http://www.twitter.com/
// @include http://www.twitter.com/*
// ==/UserScript==
 
 
function TwitterHashtagIgnore(){
var timeline = document.getElementById('timeline')
this.statuses = timeline.getElementsByClassName('hentry status')
this.ignores = []
this.cookieName = 'twitter_hashtag_ignore'
 
this.initView()
this.initDatabase()
}
 
TwitterHashtagIgnore.prototype = {
initDatabase: function(){
if (document.cookie && document.cookie != '') {
if (!this.readCookie()) {
this.writeCookie()
}
this.readDatabase()
} else {
alert("There was an error initializing the cookie used for data storage by the Twitter Hashtag Ignore greasemonkey extension.")
}
},
 
readDatabase: function(){
var cookieData = this.readCookie()
if (cookieData != '') {
this.ignores = cookieData.split('||')
}
this.rebuildView()
},
 
removeIgnore: function(ignore){
var newIgnores = []
for (var i = 0; i < this.ignores.length; i++) {
var oldIgnore = this.ignores[i]
if (ignore == oldIgnore) {
} else {
newIgnores.push(oldIgnore)
}
}
this.ignores = newIgnores
 
this.writeCookie()
this.readDatabase()
},
 
addIgnore: function(ignore){
if (this.isIgnoring(ignore)) {
alert("Already ignoring that hashtag.")
} else {
this.ignores.push(ignore)
this.writeCookie()
}
this.readDatabase()
},
 
readCookie: function(){
var ourCookie
var cookies = document.cookie.split(';')
 
for (var i = 0; i < cookies.length; i++) {
var cookie = this.trimString(cookies[i])
if (cookie.substring(0, this.cookieName.length + 1) == (this.cookieName + '=')) {
ourCookie = decodeURIComponent(cookie.substring(this.cookieName.length + 1))
break
}
}
 
return ourCookie
},
 
writeCookie: function(){
var date = new Date()
date.setTime(Date.parse('Jan 1, 2020'))
var expires = '; expires =' + date.toUTCString()
var path = '; path=/'
var domain = '; domain=twitter.com'
document.cookie = [this.cookieName, '=', this.ignores.join('||'), expires, path, domain].join('')
 
this.readCookie()
},
 
initView: function(){
this.addCSS()
this.drawSettingsPanel()
},
 
addCSS: function(){
var css = '.status.ignored { color:#ccc; background-color:#eee; } ' +
'.status.ignored a { color:#ccc; } ' +
'#side ul#ignore_list { margin-bottom: 1em; overflow:hidden; } ' +
'#side ul#ignore_list li { clear:both } ' +
'#side ul#ignore_list li span { float:left; clear:right; } ' +
'#side ul#ignore_list li .unignore { float:right; } '
 
var styleTag = document.createElement('style')
styleTag.setAttribute('type', 'text/css')
styleTag.appendChild(document.createTextNode(css))
document.getElementsByTagName('head')[0].appendChild(styleTag)
},
 
ignoreEntries: function(){
for (var i = 0; i < this.statuses.length; i++) {
var status = this.statuses[i]
var entry = status.getElementsByClassName('entry-content')[0]
var hashtags = entry.innerHTML.match(/#[a-z]+/ig)
 
status.className = status.className.replace(/ ignored/, '')
 
if (hashtags) {
for (var i2 = 0; i2 < hashtags.length; i2++) {
var hashtag = hashtags[i2]
// Ideally, the capture shoudln't contain the hash.
hashtag = hashtag.slice(1, hashtag.length)
 
if (this.isIgnoring(hashtag)) {
status.className += ' ignored'
}
}
}
}
},
 
drawSettingsPanel: function(){
var self = this
 
var sectionHeader = document.createElement('div')
sectionHeader .setAttribute('class', 'section-header')
var txt = document.createElement('h1')
txt .appendChild(document.createTextNode('Hashtag Ignores'))
sectionHeader .appendChild(txt)
 
var ignoreList = document.createElement('ul')
ignoreList .setAttribute('id', 'ignore_list')
 
var entryForm = document.createElement('form')
var input = document.createElement('input')
input .setAttribute('type', 'text')
input .setAttribute('size', '15')
var button = document.createElement('button')
button .appendChild(document.createTextNode('Add'))
 
entryForm .appendChild(input)
entryForm .appendChild(button)
 
this.hookEvent(entryForm, 'submit', function(){
self.addIgnore(input.value)
input.value = ''
})
 
var settings = document.createElement('div')
settings .setAttribute('class', 'section last')
 
settings .appendChild(sectionHeader)
settings .appendChild(ignoreList)
settings .appendChild(entryForm)
 
document .getElementById('side').appendChild(settings)
},
 
fillSettingsPanel: function(){
var self = this
 
 
var ignoreList = document.getElementById('ignore_list')
ignoreList.innerHTML = ''
 
for (var i = 0; i < this.ignores.length; i++) {
var ignoreText = this.ignores[i]
 
var ignoreEntry = document.createElement('li')
 
var unignore = document.createElement('a')
unignore .setAttribute('href', '#') // gief colorz
unignore .setAttribute('class', 'unignore')
unignore .setAttribute('title', ignoreText)
unignore .appendChild(document.createTextNode('x'))
self.hookEvent(unignore, 'click', function(){
self.removeIgnore(this.getAttribute('title'))
})
 
var spanWrap = document.createElement('span')
spanWrap .appendChild(document.createTextNode(ignoreText))
 
ignoreEntry .appendChild(spanWrap)
ignoreEntry .appendChild(unignore)
 
ignoreList.appendChild(ignoreEntry)
}
},
 
rebuildView: function(){
this.ignoreEntries()
this.fillSettingsPanel()
},
 
isIgnoring: function(ignore){
for (var i = 0; i < this.ignores.length; i++) {
if (ignore == this.ignores[i]) {
return true
}
}
},
 
hookEvent: function(target, eventName, func){
target.addEventListener(eventName, function(e){
if (e) { // Returning false won't cut it.
try {
e.stopPropagation();
e.preventDefault();
} catch (ex) {}
}
func.call(target)
}, false)
},
 
trimString: function(string) {
// Stolen from jQuery
return string.replace(/^\s+|\s+$/g, "")
}
}
 
new TwitterHashtagIgnore()