config =
main:
Enhancing:
'404 Redirect': [true, 'Redirect dead threads']
'Anonymize': [false, 'Make everybody anonymous']
'Keybinds': [false, 'Binds actions to keys']
'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time']
'Report Button': [true, 'Add report buttons']
'Comment Expansion': [true, 'Expand too long comments']
'Thread Expansion': [true, 'View all replies']
'Index Navigation': [true, 'Navigate to previous / next thread']
'Reply Navigation': [false, 'Navigate to top / bottom of thread']
Hiding:
'Reply Hiding': [true, 'Hide single replies']
'Thread Hiding': [true, 'Hide entire threads']
'Show Stubs': [true, 'Of hidden threads / replies']
Imaging:
'Image Auto-Gif': [false, 'Animate gif thumbnails']
'Image Expansion': [true, 'Expand images']
'Image Hover': [false, 'Show full image on mouseover']
'Image Preloading': [false, 'Preload Images']
'Sauce': [true, 'Add sauce to images']
'Reveal Spoilers': [false, 'Replace spoiler thumbnails by the original thumbnail']
Monitoring:
'Thread Updater': [true, 'Update threads. Has more options in its own dialog.']
'Unread Count': [true, 'Show unread post count in tab title']
'Post in Title': [true, 'Show the op\'s post in the tab title']
'Thread Stats': [true, 'Display reply and image count']
'Thread Watcher': [true, 'Bookmark threads']
'Auto Watch': [true, 'Automatically watch threads that you start']
'Auto Watch Reply': [false, 'Automatically watch threads that you reply to']
Posting:
'Auto Noko': [true, 'Always redirect to your post']
'Cooldown': [true, 'Prevent \'flood detected\' errors']
'Quick Reply': [true, 'Reply without leaving the page']
'Persistent QR': [false, 'Quick reply won\'t disappear after posting. Only in replies.']
'Auto Hide QR': [true, 'Automatically auto-hide the quick reply when posting']
Quoting:
'Quote Backlinks': [true, 'Add quote backlinks']
'OP Backlinks': [false, 'Add backlinks to the OP']
'Quote Highlighting': [true, 'Highlight the previewed post']
'Quote Inline': [true, 'Show quoted post inline on quote click']
'Quote Preview': [true, 'Show quote content on hover']
'Indicate OP quote': [true, 'Add \'(OP)\' to OP quotes']
flavors: [
'http://regex.info/exif.cgi?url='
'http://iqdb.org/?url='
'http://google.com/searchbyimage?image_url='
'#http://tineye.com/search?url='
'#http://saucenao.com/search.php?db=999&url='
'#http://imgur.com/upload?url='
'#http://anonym.to/?'
].join '\n'
time: '%m/%d/%y(%a)%H:%M'
hotkeys:
close: 'Esc'
spoiler: 'ctrl+s'
openQR: 'i'
openEmptyQR: 'I'
submit: 'alt+s'
nextReply: 'J'
previousReply: 'K'
nextThread: 'n'
previousThread: 'p'
nextPage: 'L'
previousPage: 'H'
zero: '0'
openThreadTab: 'o'
openThread: 'O'
expandThread: 'e'
watch: 'w'
hide: 'x'
expandImages: 'm'
expandAllImages: 'M'
update: 'u'
unreadCountTo0: 'z'
updater:
checkbox:
'Scrolling': [false, 'Scroll updated posts into view. Only enabled at bottom of page.']
'Verbose': [true, 'Show countdown timer, new post count']
'Auto Update': [true, 'Automatically fetch new posts']
'Interval': 30
# flatten the config
conf = {}
((parent, obj) ->
if obj.length #array
if typeof obj[0] is 'boolean'
conf[parent] = obj[0]
else
conf[parent] = obj
else if typeof obj is 'object'
for key, val of obj
arguments.callee key, val
else #constant
conf[parent] = obj
) null, config
# XXX chrome can't into `{log} = console`
if console?
log = (arg) ->
console.log arg
# XXX opera cannot into Object.keys
if not Object.keys
Object.keys = (o) ->
key for key in o
NAMESPACE = 'AEOS.4chan_x.'
d = document
g = callbacks: []
ui =
dialog: (id, position, html) ->
el = d.createElement 'div'
el.className = 'reply dialog'
el.innerHTML = html
el.id = id
{left, top} = position
left = localStorage["#{NAMESPACE}#{id}Left"] ? left
top = localStorage["#{NAMESPACE}#{id}Top"] ? top
if left then el.style.left = left else el.style.right = 0
if top then el.style.top = top else el.style.bottom = 0
el.querySelector('div.move').addEventListener 'mousedown', ui.dragstart, false
el
dragstart: (e) ->
#prevent text selection
e.preventDefault()
ui.el = el = @parentNode
d.addEventListener 'mousemove', ui.drag, false
d.addEventListener 'mouseup', ui.dragend, false
#distance from pointer to el edge is constant; calculate it here.
# XXX opera reports el.offsetLeft / el.offsetTop as 0
rect = el.getBoundingClientRect()
ui.dx = e.clientX - rect.left
ui.dy = e.clientY - rect.top
#factor out el from document dimensions
ui.width = d.body.clientWidth - el.offsetWidth
ui.height = d.body.clientHeight - el.offsetHeight
drag: (e) ->
{el} = ui
left = e.clientX - ui.dx
if left < 10 then left = '0'
else if ui.width - left < 10 then left = null
right = if left then null else 0
top = e.clientY - ui.dy
if top < 10 then top = '0'
else if ui.height - top < 10 then top = null
bottom = if top then null else 0
#using null instead of '' is 4% faster
#these 4 statements are 40% faster than 1 style.cssText
el.style.top = top
el.style.right = right
el.style.bottom = bottom
el.style.left = left
dragend: ->
#$ coffee -bpe '{a} = {b} = c'
#var a, b;
#a = (b = c.b, c).a;
{el} = ui
{id} = el
localStorage["#{NAMESPACE}#{id}Left"] = el.style.left
localStorage["#{NAMESPACE}#{id}Top"] = el.style.top
d.removeEventListener 'mousemove', ui.drag, false
d.removeEventListener 'mouseup', ui.dragend, false
hover: (e) ->
{clientX, clientY} = e
{el} = ui
{clientHeight, clientWidth} = d.body
height = el.offsetHeight
top = clientY - 120
el.style.top =
if clientHeight < height or top < 0
0
else if top + height > clientHeight
clientHeight - height
else
top
if clientX < clientWidth - 400
el.style.left = clientX + 45
el.style.right = null
else
el.style.left = null
el.style.right = clientWidth - clientX + 45
hoverend: (e) ->
ui.el.parentNode.removeChild ui.el
###
loosely follows the jquery api:
http://api.jquery.com/
not chainable
###
$ = (selector, root=d.body) ->
root.querySelector selector
$.extend = (object, properties) ->
for key, val of properties
object[key] = val
object
$.extend $,
id: (id) ->
d.getElementById id
globalEval: (code) ->
script = $.el 'script',
textContent: "(#{code})()"
$.append d.head, script
$.rm script
get: (url, cb) ->
r = new XMLHttpRequest()
r.onload = cb
r.open 'get', url, true
r.send()
r
cache: (url, cb) ->
if req = $.cache.requests[url]
if req.readyState is 4
cb.call req
else
req.callbacks.push cb
else
req = $.get url, (-> cb.call @ for cb in @callbacks)
req.callbacks = [cb]
$.cache.requests[url] = req
cb:
checked: ->
$.setValue @name, @checked
value: ->
$.setValue @name, @value
addStyle: (css) ->
style = $.el 'style',
textContent: css
$.append d.head, style
style
x: (path, root=d.body) ->
d.evaluate(path, root, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).
singleNodeValue
tn: (s) ->
d.createTextNode s
replace: (root, el) ->
root.parentNode.replaceChild el, root
hide: (el) ->
el.hidden = true
show: (el) ->
el.hidden = false
addClass: (el, className) ->
el.className += ' ' + className
removeClass: (el, className) ->
el.className = el.className.replace ' ' + className, ''
rm: (el) ->
el.parentNode.removeChild el
append: (parent, children...) ->
for child in children
parent.appendChild child
prepend: (parent, child) ->
parent.insertBefore child, parent.firstChild
after: (root, el) ->
root.parentNode.insertBefore el, root.nextSibling
before: (root, el) ->
root.parentNode.insertBefore el, root
el: (tag, properties) ->
el = d.createElement tag
$.extend el, properties if properties
el
bind: (el, eventType, handler) ->
el.addEventListener eventType, handler, false
unbind: (el, eventType, handler) ->
el.removeEventListener eventType, handler, false
isDST: ->
# XXX this should check for DST in NY
###
http://en.wikipedia.org/wiki/Daylight_saving_time_in_the_United_States
Since 2007, daylight saving time starts on the second Sunday of March
and ends on the first Sunday of November, with all time changes taking
place at 2:00 AM (0200) local time.
###
date = new Date()
month = date.getMonth()
#this is the easy part
if month < 2 or 10 < month
return false
if 2 < month < 10
return true
# (sunday's date) = (today's date) - (number of days past sunday)
# date is not zero-indexed
sunday = date.getDate() - date.getDay()
if month is 2
#before second sunday
if sunday < 8
return false
#during second sunday
if sunday < 15 and date.getDay() is 0
if date.getHour() < 1
return false
return true
#after second sunday
return true
if month is 10
# before first sunday
if sunday < 1
return true
# during first sunday
if sunday < 8 and date.getDay() is 0
if date.getHour() < 1
return true
return false
#after first sunday
return false
$.cache.requests = {}
if GM_deleteValue?
$.extend $,
deleteValue: (name) ->
name = NAMESPACE + name
GM_deleteValue name
getValue: (name, defaultValue) ->
name = NAMESPACE + name
if value = GM_getValue name
JSON.parse value
else
defaultValue
openInTab: (url) ->
GM_openInTab url
setValue: (name, value) ->
name = NAMESPACE + name
# for `storage` events
localStorage[name] = JSON.stringify value
GM_setValue name, JSON.stringify value
else
$.extend $,
deleteValue: (name) ->
name = NAMESPACE + name
delete localStorage[name]
getValue: (name, defaultValue) ->
name = NAMESPACE + name
if value = localStorage[name]
JSON.parse value
else
defaultValue
openInTab: (url) ->
window.open url, "_blank"
setValue: (name, value) ->
name = NAMESPACE + name
localStorage[name] = JSON.stringify value
#load values from localStorage
for key, val of conf
conf[key] = $.getValue key, val
$$ = (selector, root=d.body) ->
Array::slice.call root.querySelectorAll selector
expandComment =
init: ->
for a in $$ 'span.abbr a'
$.bind a, 'click', expandComment.expand
expand: (e) ->
e.preventDefault()
[_, threadID, replyID] = @href.match /(\d+)#(\d+)/
@textContent = "Loading #{replyID}..."
threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]/div', @).id
a = @
$.cache @pathname, (-> expandComment.parse @, a, threadID, replyID)
parse: (req, a, threadID, replyID) ->
if req.status isnt 200
a.textContent = "#{req.status} #{req.statusText}"
return
body = $.el 'body',
innerHTML: req.responseText
if threadID is replyID #OP
bq = $ 'blockquote', body
else
#css selectors don't like ids starting with numbers,
# getElementById only works for root document.
for reply in $$ 'td[id]', body
if reply.id == replyID
bq = $ 'blockquote', reply
break
$.replace a.parentNode.parentNode, bq
expandThread =
init: ->
for span in $$ 'span.omittedposts'
a = $.el 'a',
textContent: "+ #{span.textContent}"
className: 'omittedposts'
$.bind a, 'click', expandThread.cb.toggle
$.replace span, a
cb:
toggle: (e) ->
thread = @parentNode
expandThread.toggle thread
toggle: (thread) ->
threadID = thread.firstChild.id
pathname = "/#{g.BOARD}/res/#{threadID}"
a = $ 'a.omittedposts', thread
switch a.textContent[0]
when '+'
$('.op .container', thread)?.innerHTML = ''
a.textContent = a.textContent.replace '+', 'X Loading...'
$.cache pathname, (-> expandThread.parse @, pathname, thread, a)
when 'X'
a.textContent = a.textContent.replace 'X Loading...', '+'
#FIXME this will kill all callbacks
$.cache[pathname].abort()
when '-'
a.textContent = a.textContent.replace '-', '+'
#goddamit moot
num = switch g.BOARD
when 'b' then 3
when 't' then 1
else 5
table = $.x "following::br[@clear][1]/preceding::table[#{num}]", a
while (prev = table.previousSibling) and (prev.nodeName is 'TABLE')
$.rm prev
for backlink in $$ '.op a.backlink'
$.rm backlink if !$.id backlink.hash[1..]
parse: (req, pathname, thread, a) ->
if req.status isnt 200
a.textContent = "#{req.status} #{req.statusText}"
$.unbind a, 'click', expandThread.cb.toggle
return
a.textContent = a.textContent.replace 'X Loading...', '-'
# eat everything, then replace with fresh full posts
while (next = a.nextSibling) and not next.clear #br[clear]
$.rm next
br = next
body = $.el 'body',
innerHTML: req.responseText
for reply in $$ 'td[id]', body
for quote in $$ 'a.quotelink', reply
if quote.getAttribute('href') is quote.hash
quote.pathname = pathname
link = $ 'a.quotejs', reply
link.href = "res/#{thread.firstChild.id}##{reply.id}"
link.nextSibling.href = "res/#{thread.firstChild.id}#q#{reply.id}"
tables = $$ 'form[name=delform] table', body
tables.pop()
for table in tables
$.before br, table
replyHiding =
init: ->
g.callbacks.push (root) ->
return unless dd = $ 'td.doubledash', root
dd.className = 'replyhider'
a = $.el 'a',
textContent: '[ - ]'
$.bind a, 'click', replyHiding.cb.hide
$.replace dd.firstChild, a
reply = dd.nextSibling
id = reply.id
if id of g.hiddenReplies
replyHiding.hide reply
cb:
hide: (e) ->
reply = @parentNode.nextSibling
replyHiding.hide reply
show: (e) ->
div = @parentNode
table = div.nextSibling
replyHiding.show table
$.rm div
hide: (reply) ->
table = reply.parentNode.parentNode.parentNode
$.hide table
if conf['Show Stubs']
name = $('span.commentpostername', reply).textContent
trip = $('span.postertrip', reply)?.textContent or ''
a = $.el 'a',
textContent: "[ + ] #{name} #{trip}"
$.bind a, 'click', replyHiding.cb.show
div = $.el 'div',
className: 'stub'
$.append div, a
$.before table, div
id = reply.id
g.hiddenReplies[id] = Date.now()
$.setValue "hiddenReplies/#{g.BOARD}/", g.hiddenReplies
show: (table) ->
$.show table
id = $('td[id]', table).id
delete g.hiddenReplies[id]
$.setValue "hiddenReplies/#{g.BOARD}/", g.hiddenReplies
keybinds =
init: ->
for node in $$ '[accesskey]'
node.removeAttribute 'accesskey'
$.bind d, 'keydown', keybinds.keydown
keydown: (e) ->
updater.focus = true
return if e.target.nodeName in ['TEXTAREA', 'INPUT'] and not e.altKey and not e.ctrlKey and not (e.keyCode is 27)
return unless key = keybinds.keyCode e
thread = nav.getThread()
switch key
when conf.close
if o = $ '#overlay'
$.rm o
else if qr.el
qr.close()
when conf.spoiler
ta = e.target
return unless ta.nodeName is 'TEXTAREA'
value = ta.value
selStart = ta.selectionStart
selEnd = ta.selectionEnd
valStart = value[0...selStart] + '[spoiler]'
valMid = value[selStart...selEnd]
valEnd = '[/spoiler]' + value[selEnd..]
ta.value = valStart + valMid + valEnd
range = valStart.length + valMid.length
ta.setSelectionRange range, range
when conf.zero
window.location = "/#{g.BOARD}/0#0"
when conf.openEmptyQR
keybinds.qr thread
when conf.nextReply
keybinds.hl.next thread
when conf.previousReply
keybinds.hl.prev thread
when conf.expandAllImages
keybinds.img thread, true
when conf.openThread
keybinds.open thread
when conf.expandThread
expandThread.toggle thread
when conf.openQR
keybinds.qr thread, true
when conf.expandImages
keybinds.img thread
when conf.nextThread
nav.next()
when conf.openThreadTab
keybinds.open thread, true
when conf.previousThread
nav.prev()
when conf.update
updater.update()
when conf.watch
watcher.toggle thread
when conf.hide
threadHiding.toggle thread
when conf.nextPage
$('input[value=Next]')?.click()
when conf.previousPage
$('input[value=Previous]')?.click()
when conf.submit
if qr.el
qr.submit.call $ 'form', qr.el
else
$('.postarea form').submit()
when conf.unreadCountTo0
unread.replies.length = 0
unread.updateTitle()
Favicon.update()
else
return
e.preventDefault()
keyCode: (e) ->
kc = e.keyCode
if 65 <= kc <= 90 #A-Z
key = String.fromCharCode kc
if !e.shiftKey
key = key.toLowerCase()
else if 48 <= kc <= 57 #0-9
key = String.fromCharCode kc
else if kc is 27
key = 'Esc'
else if kc is 8
key = ''
else
key = null
if key
if e.altKey then key = 'alt+' + key
if e.ctrlKey then key = 'ctrl+' + key
key
img: (thread, all) ->
if all
$("#imageExpand").click()
else
root = $('td.replyhl', thread) or thread
thumb = $ 'img[md5]', root
imgExpand.toggle thumb.parentNode
qr: (thread, quote) ->
unless qrLink = $ 'td.replyhl span[id] a:not(:first-child)', thread
qrLink = $ "span[id^=nothread] a:not(:first-child)", thread
if quote
qr.quote qrLink
else
unless qr.el
qr.dialog qrLink
$('textarea', qr.el).focus()
open: (thread, tab) ->
id = thread.firstChild.id
url = "http://boards.4chan.org/#{g.BOARD}/res/#{id}"
if tab
$.openInTab url
else
location.href = url
hl:
next: (thread) ->
if td = $ 'td.replyhl', thread
td.className = 'reply'
rect = td.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
next = $.x 'following::td[@class="reply"]', td
return if $.x('ancestor::div[@class="thread"]', next) isnt thread
rect = next.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the next
next.className = 'replyhl'
return
replies = $$ 'td.reply', thread
for reply in replies
top = reply.getBoundingClientRect().top
if top > 0
reply.className = 'replyhl'
return
prev: (thread) ->
if td = $ 'td.replyhl', thread
td.className = 'reply'
rect = td.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
prev = $.x 'preceding::td[@class="reply"][1]', td
rect = prev.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the prev
prev.className = 'replyhl'
return
replies = $$ 'td.reply', thread
replies.reverse()
height = d.body.clientHeight
for reply in replies
bot = reply.getBoundingClientRect().bottom
if bot < height
reply.className = 'replyhl'
return
nav =
# ◀ ▶
init: ->
span = $.el 'span',
id: 'navlinks'
prev = $.el 'a',
textContent: '▲'
next = $.el 'a',
textContent: '▼'
$.bind prev, 'click', nav.prev
$.bind next, 'click', nav.next
$.append span, prev, $.tn(' '), next
$.append d.body, span
prev: ->
nav.scroll -1
next: ->
nav.scroll +1
threads: []
getThread: (full) ->
nav.threads = $$ 'div.thread:not([hidden])'
for thread, i in nav.threads
rect = thread.getBoundingClientRect()
{bottom} = rect
if bottom > 0 #we have not scrolled past
if full
return [thread, i, rect]
return thread
return null
scroll: (delta) ->
if g.REPLY
if delta is -1
window.scrollTo 0,0
else
window.scrollTo 0, d.body.scrollHeight
return
[thread, i, rect] = nav.getThread true
{top} = rect
#unless we're not at the beginning of the current thread
# (and thus wanting to move to beginning)
# or we're above the first thread and don't want to skip it
unless (delta is -1 and Math.ceil(top) < 0) or (delta is +1 and top > 1)
i += delta
if i is -1
if g.PAGENUM is 0
window.scrollTo 0, 0
else
window.location = "#{g.PAGENUM - 1}#0"
return
if delta is +1
# if we're at the last thread, or we're at the bottom of the page.
# kind of hackish, what we really need to do is make nav.getThread smarter.
if i is nav.threads.length or (innerHeight + pageYOffset == d.body.scrollHeight)
if $ 'table.pages input[value="Next"]'
window.location = "#{g.PAGENUM + 1}#0"
return
#TODO sfx
{top} = nav.threads[i].getBoundingClientRect()
window.scrollBy 0, top
options =
init: ->
home = $ '#navtopr a'
a = $.el 'a',
textContent: '4chan X'
$.bind a, 'click', options.dialog
$.replace home, a
home = $ '#navbotr a'
a = $.el 'a',
textContent: '4chan X'
$.bind a, 'click', options.dialog
$.replace home, a
dialog: ->
hiddenThreads = $.getValue "hiddenThreads/#{g.BOARD}/", {}
hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length
html = "