Config =
main:
Enhancing:
'Disable 4chan\'s extension': [true, 'Avoid conflicts between 4chan X and 4chan\'s inline extension']
'Catalog Links': [true, 'Turn Navigation links into links to each board\'s catalog']
'404 Redirect': [true, 'Redirect dead threads and images']
'Keybinds': [true, 'Binds actions to keys']
'Time Formatting': [true, 'Arbitrarily formatted timestamps, using your local time']
'Relative Post Dates': [false, 'Display dates as "3 minutes ago" f.e., tooltip shows the timestamp']
'File Info Formatting': [true, 'Reformats the file information']
'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']
'Check for Updates': [true, 'Check for updated versions of 4chan X']
Filtering:
'Anonymize': [false, 'Make everybody anonymous']
'Filter': [true, 'Self-moderation placebo']
'Recursive Filtering': [true, 'Filter replies of filtered posts, recursively']
'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']
'Sauce': [true, 'Add sauce to images']
'Reveal Spoilers': [false, 'Replace spoiler thumbnails by the original thumbnail']
'Expand From Current': [false, 'Expand images from current position to thread end']
Menu:
'Menu': [true, 'Add a drop-down menu in posts']
'Report Link': [true, 'Add a report link to the menu']
'Delete Link': [true, 'Add post and image deletion links to the menu']
'Download Link': [true, 'Add a download with original filename link to the menu (Chrome only)']
'Archive Link': [true, 'Add an archive link to the menu']
Monitoring:
'Thread Updater': [true, 'Update threads. Has more options in its own dialog.']
'Unread Count': [true, 'Show unread post count in tab title']
'Unread Favicon': [true, 'Show a different favicon when there are unread posts']
'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:
'Quick Reply': [true, 'Reply without leaving the page']
'Cooldown': [true, 'Prevent "flood detected" errors']
'Persistent QR': [false, 'The Quick reply won\'t disappear after posting']
'Auto Hide QR': [true, 'Automatically hide the quick reply when posting']
'Open Reply in New Tab': [false, 'Open replies in a new tab that are made from the main board']
'Remember QR size': [false, 'Remember the size of the Quick reply (Firefox only)']
'Remember Subject': [false, 'Remember the subject field, instead of resetting after posting']
'Remember Spoiler': [false, 'Remember the spoiler state, instead of resetting after posting']
'Hide Original Post Form': [true, 'Replace the normal post form with a shortcut to open the QR']
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']
'Resurrect Quotes': [true, 'Linkify dead quotes to archives']
'Indicate OP quote': [true, 'Add \'(OP)\' to OP quotes']
'Indicate Cross-thread Quotes': [true, 'Add \'(Cross-thread)\' to cross-threads quotes']
'Forward Hiding': [true, 'Hide original posts of inlined backlinks']
filter:
name: [
'# Filter any namefags:'
'#/^(?!Anonymous$)/'
].join '\n'
uniqueid: [
'# Filter a specific ID:'
'#/Txhvk1Tl/'
].join '\n'
tripcode: [
'# Filter any tripfags'
'#/^!/'
].join '\n'
mod: [
'# Set a custom class for mods:'
'#/Mod$/;highlight:mod;op:yes'
'# Set a custom class for moot:'
'#/Admin$/;highlight:moot;op:yes'
].join '\n'
email: [
'# Filter any e-mails that are not `sage` on /a/ and /jp/:'
'#/^(?!sage$)/;boards:a,jp'
].join '\n'
subject: [
'# Filter Generals on /v/:'
'#/general/i;boards:v;op:only'
].join '\n'
comment: [
'# Filter Stallman copypasta on /g/:'
'#/what you\'re refer+ing to as linux/i;boards:g'
].join '\n'
country: [
''
].join '\n'
filename: [
''
].join '\n'
dimensions: [
'# Highlight potential wallpapers:'
'#/1920x1080/;op:yes;highlight;top:no;boards:w,wg'
].join '\n'
filesize: [
''
].join '\n'
md5: [
''
].join '\n'
sauces: [
'http://iqdb.org/?url=$1'
'http://www.google.com/searchbyimage?image_url=$1'
'#http://tineye.com/search?url=$1'
'#http://saucenao.com/search.php?db=999&url=$1'
'#http://3d.iqdb.org/?url=$1'
'#http://regex.info/exif.cgi?imgurl=$2'
'# uploaders:'
'#http://imgur.com/upload?url=$2;text:Upload to imgur'
'#http://omploader.org/upload?url1=$2;text:Upload to omploader'
'# "View Same" in archives:'
'#http://archive.foolz.us/_/search/image/$3/;text:View same on foolz'
'#http://archive.foolz.us/$4/search/image/$3/;text:View same on foolz /$4/'
'#https://archive.installgentoo.net/$4/image/$3;text:View same on installgentoo /$4/'
].join '\n'
time: '%m/%d/%y(%a)%H:%M'
backlink: '>>%id'
fileInfo: '%l (%p%s, %r)'
favicon: 'ferongr'
hotkeys:
# QR & Options
openQR: ['i', 'Open QR with post number inserted']
openEmptyQR: ['I', 'Open QR without post number inserted']
openOptions: ['ctrl+o', 'Open Options']
close: ['Esc', 'Close Options or QR']
spoiler: ['ctrl+s', 'Quick spoiler tags']
code: ['alt+c', 'Quick code tags']
submit: ['alt+s', 'Submit post']
# Thread related
watch: ['w', 'Watch thread']
update: ['u', 'Update now']
unreadCountTo0: ['z', 'Mark thread as read']
# Images
expandImage: ['m', 'Expand selected image']
expandAllImages: ['M', 'Expand all images']
# Board Navigation
zero: ['0', 'Jump to page 0']
nextPage: ['L', 'Jump to the next page']
previousPage: ['H', 'Jump to the previous page']
# Thread Navigation
nextThread: ['n', 'See next thread']
previousThread: ['p', 'See previous thread']
expandThread: ['e', 'Expand thread']
openThreadTab: ['o', 'Open thread in current tab']
openThread: ['O', 'Open thread in new tab']
# Reply Navigation
nextReply: ['J', 'Select next reply']
previousReply: ['K', 'Select previous reply']
hide: ['x', 'Hide thread']
updater:
checkbox:
'Beep': [false, 'Beep on new post to completely read thread']
'Scrolling': [false, 'Scroll updated posts into view. Only enabled at bottom of page.']
'Scroll BG': [false, 'Scroll background tabs']
'Verbose': [true, 'Show countdown timer, new post count']
'Auto Update': [true, 'Automatically fetch new posts']
'Interval': 30
Conf = {}
d = document
g = {}
UI =
dialog: (id, position, html) ->
el = d.createElement 'div'
el.className = 'reply dialog'
el.innerHTML = html
el.id = id
el.style.cssText = localStorage.getItem("#{Main.namespace}#{id}.position") or position
el.querySelector('.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.
rect = el.getBoundingClientRect()
UI.dx = e.clientX - rect.left
UI.dy = e.clientY - rect.top
UI.width = d.documentElement.clientWidth - rect.width
UI.height = d.documentElement.clientHeight - rect.height
drag: (e) ->
left = e.clientX - UI.dx
top = e.clientY - UI.dy
left =
if left < 10 then '0px'
else if UI.width - left < 10 then null
else left + 'px'
top =
if top < 10 then '0px'
else if UI.height - top < 10 then null
else top + 'px'
#using null instead of '' is 4% faster
#these 4 statements are 40% faster than 1 style.cssText
{style} = UI.el
style.left = left
style.top = top
style.right = if left is null then '0px' else null
style.bottom = if top is null then '0px' else null
dragend: ->
localStorage.setItem "#{Main.namespace}#{UI.el.id}.position", UI.el.style.cssText
d.removeEventListener 'mousemove', UI.drag, false
d.removeEventListener 'mouseup', UI.dragend, false
delete UI.el
hover: (e) ->
{clientX, clientY} = e
{style} = UI.el
{clientHeight, clientWidth} = d.documentElement
height = UI.el.offsetHeight
top = clientY - 120
style.top =
if clientHeight <= height or top <= 0
'0px'
else if top + height >= clientHeight
clientHeight - height + 'px'
else
top + 'px'
if clientX <= clientWidth - 400
style.left = clientX + 45 + 'px'
style.right = null
else
style.left = null
style.right = clientWidth - clientX + 45 + 'px'
hoverend: ->
$.rm UI.el
delete 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
return
$.extend $,
SECOND: 1000
MINUTE: 1000*60
HOUR : 1000*60*60
DAY : 1000*60*60*24
log:
# XXX GreaseMonkey can't into console.log.bind
console.log.bind? console
engine: /WebKit|Presto|Gecko/.exec(navigator.userAgent)[0].toLowerCase()
ready: (fc) ->
if /interactive|complete/.test d.readyState
# Execute the functions in parallel.
# If one fails, do not stop the others.
return setTimeout fc
cb = ->
$.off d, 'DOMContentLoaded', cb
fc()
$.on d, 'DOMContentLoaded', cb
sync: (key, cb) ->
key = Main.namespace + key
$.on window, 'storage', (e) ->
cb JSON.parse e.newValue if e.key is key
id: (id) ->
d.getElementById id
formData: (arg) ->
if arg instanceof HTMLFormElement
fd = new FormData arg
else
fd = new FormData()
for key, val of arg
fd.append key, val if val
fd
ajax: (url, callbacks, opts={}) ->
{type, headers, upCallbacks, form} = opts
r = new XMLHttpRequest()
type or= form and 'post' or 'get'
r.open type, url, true
for key, val of headers
r.setRequestHeader key, val
$.extend r, callbacks
$.extend r.upload, upCallbacks
r.withCredentials = true if type is 'post'
r.send form
r
cache: (url, cb) ->
if req = $.cache.requests[url]
if req.readyState is 4
cb.call req
else
req.callbacks.push cb
else
req = $.ajax url,
onload: -> cb.call @ for cb in @callbacks
onabort: -> delete $.cache.requests[url]
onerror: -> delete $.cache.requests[url]
req.callbacks = [cb]
$.cache.requests[url] = req
cb:
checked: ->
$.set @name, @checked
Conf[@name] = @checked
value: ->
$.set @name, @value.trim()
Conf[@name] = @value
addStyle: (css) ->
style = $.el 'style',
textContent: css
# That's terrible.
# XXX tmp fix for scriptish:
# https://github.com/scriptish/scriptish/issues/16
f = ->
# XXX Only Chrome has no d.head on document-start.
if root = d.head or d.documentElement
$.add root, style
else
setTimeout f, 20
f()
style
x: (path, root=d.body) ->
# XPathResult.ANY_UNORDERED_NODE_TYPE is 8
d.evaluate(path, root, null, 8, null).
singleNodeValue
addClass: (el, className) ->
el.classList.add className
rmClass: (el, className) ->
el.classList.remove className
rm: (el) ->
el.parentNode.removeChild el
tn: (s) ->
d.createTextNode s
nodes: (nodes) ->
# In (at least) Chrome, elements created inside different
# scripts/window contexts inherit from unequal prototypes.
# window_ext1.Node !== window_ext2.Node
unless nodes instanceof Array
return nodes
frag = d.createDocumentFragment()
for node in nodes
frag.appendChild node
frag
add: (parent, children) ->
parent.appendChild $.nodes children
prepend: (parent, children) ->
parent.insertBefore $.nodes(children), parent.firstChild
after: (root, el) ->
root.parentNode.insertBefore $.nodes(el), root.nextSibling
before: (root, el) ->
root.parentNode.insertBefore $.nodes(el), root
replace: (root, el) ->
root.parentNode.replaceChild $.nodes(el), root
el: (tag, properties) ->
el = d.createElement tag
$.extend el, properties if properties
el
on: (el, events, handler) ->
for event in events.split ' '
el.addEventListener event, handler, false
return
off: (el, events, handler) ->
for event in events.split ' '
el.removeEventListener event, handler, false
return
open: (url) ->
(GM_openInTab or window.open) location.protocol + url, '_blank'
event: (el, e) ->
el.dispatchEvent e
globalEval: (code) ->
script = $.el 'script', textContent: code
$.add d.head, script
$.rm script
bytesToString: (size) ->
unit = 0 # Bytes
while size >= 1024
size /= 1024
unit++
# Remove trailing 0s.
size =
if unit > 1
# Keep the size as a float if the size is greater than 2^20 B.
# Round to hundredth.
Math.round(size * 100) / 100
else
# Round to an integer otherwise.
Math.round size
"#{size} #{['B', 'KB', 'MB', 'GB'][unit]}"
# a function that will execute at most every 'wait' ms. executes immediately
# if possible, else discards invocation
debounce: (wait, fn) ->
timeout = null
return ->
if timeout
# stop current reset
clearTimeout timeout
else
fn.apply this, arguments
# after wait, let next invocation execute immediately
timeout = setTimeout (-> timeout = null), wait
$.cache.requests = {}
$.extend $,
if GM_deleteValue?
delete: (name) ->
name = Main.namespace + name
GM_deleteValue name
get: (name, defaultValue) ->
name = Main.namespace + name
if value = GM_getValue name
JSON.parse value
else
defaultValue
set: (name, value) ->
name = Main.namespace + name
# for `storage` events
localStorage.setItem name, JSON.stringify value
GM_setValue name, JSON.stringify value
else
delete: (name) ->
localStorage.removeItem Main.namespace + name
get: (name, defaultValue) ->
if value = localStorage.getItem Main.namespace + name
JSON.parse value
else
defaultValue
set: (name, value) ->
localStorage.setItem Main.namespace + name, JSON.stringify value
$$ = (selector, root=d.body) ->
Array::slice.call root.querySelectorAll selector
Filter =
filters: {}
init: ->
for key of Config.filter
@filters[key] = []
for filter in Conf[key].split '\n'
continue if filter[0] is '#'
unless regexp = filter.match /\/(.+)\/(\w*)/
continue
# Don't mix up filter flags with the regular expression.
filter = filter.replace regexp[0], ''
# Do not add this filter to the list if it's not a global one
# and it's not specifically applicable to the current board.
# Defaults to global.
boards = filter.match(/boards:([^;]+)/)?[1].toLowerCase() or 'global'
if boards isnt 'global' and boards.split(',').indexOf(g.BOARD) is -1
continue
if key is 'md5'
# MD5 filter will use strings instead of regular expressions.
regexp = regexp[1]
else
try
# Please, don't write silly regular expressions.
regexp = RegExp regexp[1], regexp[2]
catch err
# I warned you, bro.
alert err.message
continue
# Filter OPs along with their threads, replies only, or both.
# Defaults to replies only.
op = filter.match(/[^t]op:(yes|no|only)/)?[1] or 'no'
# Overrule the `Show Stubs` setting.
# Defaults to stub showing.
stub = switch filter.match(/stub:(yes|no)/)?[1]
when 'yes'
true
when 'no'
false
else
Conf['Show Stubs']
# Highlight the post, or hide it.
# If not specified, the highlight class will be filter_highlight.
# Defaults to post hiding.
if hl = /highlight/.test filter
hl = filter.match(/highlight:(\w+)/)?[1] or 'filter_highlight'
# Put highlighted OP's thread on top of the board page or not.
# Defaults to on top.
top = filter.match(/top:(yes|no)/)?[1] or 'yes'
top = top is 'yes' # Turn it into a boolean
@filters[key].push @createFilter regexp, op, stub, hl, top
# Only execute filter types that contain valid filters.
unless @filters[key].length
delete @filters[key]
if Object.keys(@filters).length
Main.callbacks.push @node
createFilter: (regexp, op, stub, hl, top) ->
test =
if typeof regexp is 'string'
# MD5 checking
(value) -> regexp is value
else
(value) -> regexp.test value
settings =
hide: !hl
stub: stub
class: hl
top: top
(value, isOP) ->
if isOP and op is 'no' or !isOP and op is 'only'
return false
unless test value
return false
settings
node: (post) ->
return if post.isInlined
isOP = post.ID is post.threadID
{root} = post
for key of Filter.filters
value = Filter[key] post
if value is false
# Continue if there's nothing to filter (no tripcode for example).
continue
for filter in Filter.filters[key]
unless result = filter value, isOP
continue
# Hide
if result.hide
if isOP
unless g.REPLY
ThreadHiding.hide root.parentNode, result.stub
else
continue
else
ReplyHiding.hide root, result.stub
return
# Highlight
$.addClass root, result.class
if isOP and result.top and not g.REPLY
# Put the highlighted OPs' thread on top of the board page...
thisThread = root.parentNode
# ...before the first non highlighted thread.
if firstThread = $ 'div[class="postContainer opContainer"]'
unless firstThread is root
$.before firstThread.parentNode, [thisThread, thisThread.nextElementSibling]
name: (post) ->
$('.name', post.el).textContent
uniqueid: (post) ->
if uid = $ '.posteruid', post.el
return uid.textContent[5...-1]
false
tripcode: (post) ->
if trip = $ '.postertrip', post.el
return trip.textContent
false
mod: (post) ->
if mod = $ '.capcode', post.el
return mod.textContent
false
email: (post) ->
if mail = $ '.useremail', post.el
# remove 'mailto:'
# decode %20 into space for example
return decodeURIComponent mail.href[7..]
false
subject: (post) ->
if subject = $ '.postInfo .subject', post.el
return subject.textContent
false
comment: (post) ->
text = []
# XPathResult.ORDERED_NODE_SNAPSHOT_TYPE is 7
nodes = d.evaluate './/br|.//text()', post.blockquote, null, 7, null
for i in [0...nodes.snapshotLength]
text.push if data = nodes.snapshotItem(i).data then data else '\n'
text.join ''
country: (post) ->
if flag = $ '.countryFlag', post.el
return flag.title
false
filename: (post) ->
{fileInfo} = post
if fileInfo
if file = $ '.fileText > span', fileInfo
return file.title
else
return fileInfo.firstElementChild.dataset.filename
false
dimensions: (post) ->
{fileInfo} = post
if fileInfo and match = fileInfo.textContent.match /\d+x\d+/
return match[0]
false
filesize: (post) ->
{img} = post
if img
return img.alt.replace 'Spoiler Image, ', ''
false
md5: (post) ->
{img} = post
if img
return img.dataset.md5
false
menuInit: ->
div = $.el 'div',
textContent: 'Filter'
entry =
el: div
open: -> true
children: []
for type in [
['Name', 'name']
['Unique ID', 'uniqueid']
['Tripcode', 'tripcode']
['Admin/Mod', 'mod']
['E-mail', 'email']
['Subject', 'subject']
['Comment', 'comment']
['Country', 'country']
['Filename', 'filename']
['Image dimensions', 'dimensions']
['Filesize', 'filesize']
['Image MD5', 'md5']
]
# Add a sub entry for each filter type.
entry.children.push Filter.createSubEntry type[0], type[1]
Menu.addEntry entry
createSubEntry: (text, type) ->
el = $.el 'a',
href: 'javascript:;'
textContent: text
# Define the onclick var outside of open's scope to $.off it properly.
onclick = null
open = (post) ->
value = Filter[type] post
return false if value is false
$.off el, 'click', onclick
onclick = ->
# Convert value -> regexp, unless type is md5
re = if type is 'md5' then value else value.replace ///
/
| \\
| \^
| \$
| \n
| \.
| \(
| \)
| \{
| \}
| \[
| \]
| \?
| \*
| \+
| \|
///g, (c) ->
if c is '\n'
'\\n'
else if c is '\\'
'\\\\'
else
"\\#{c}"
re =
if type is 'md5'
"/#{value}/"
else
"/^#{re}$/"
if /\bop\b/.test post.class
re += ';op:yes'
# Add a new line before the regexp unless the text is empty.
save = if save = $.get type, '' then "#{save}\n#{re}" else re
$.set type, save
# Open the options and display & focus the relevant filter textarea.
Options.dialog()
select = $ 'select[name=filter]', $.id 'options'
select.value = type
$.event select, new Event 'change'
$.id('filter_tab').checked = true
ta = select.nextElementSibling
tl = ta.textLength
ta.setSelectionRange tl, tl
ta.focus()
$.on el, 'click', onclick
true
return el: el, open: open
StrikethroughQuotes =
init: ->
Main.callbacks.push @node
node: (post) ->
return if post.isInlined
for quote in post.quotes
continue unless (el = $.id quote.hash[1..]) and quote.hostname is 'boards.4chan.org' and !/catalog$/.test(quote.pathname) and el.hidden
$.addClass quote, 'filtered'
if Conf['Recursive Filtering'] and post.ID isnt post.threadID
show_stub = !!$.x 'preceding-sibling::div[contains(@class,"stub")]', el
ReplyHiding.hide post.root, show_stub
return
ExpandComment =
init: ->
for a in $$ '.abbr'
$.on a.firstElementChild, 'click', ExpandComment.expand
return
expand: (e) ->
e.preventDefault()
[_, threadID, replyID] = @href.match /(\d+)#p(\d+)/
@textContent = "Loading No.#{replyID}..."
a = @
$.cache "//api.4chan.org#{@pathname}.json", -> ExpandComment.parse @, a, threadID, replyID
parse: (req, a, threadID, replyID) ->
if req.status isnt 200
a.textContent = "#{req.status} #{req.statusText}"
return
posts = JSON.parse(req.response).posts
if spoilerRange = posts[0].custom_spoiler
Build.spoilerRange[g.BOARD] = spoilerRange
replyID = +replyID
for post in posts
break if post.no is replyID
if post.no isnt replyID
a.textContent = 'No.#{replyID} not found.'
return
bq = $.id "m#{replyID}"
clone = bq.cloneNode false
clone.innerHTML = post.com
quotes = clone.getElementsByClassName 'quotelink'
for quote in quotes
href = quote.getAttribute 'href'
continue if href[0] is '/' # Cross-board quote
quote.href = "res/#{href}" # Fix pathnames
post =
blockquote: clone
threadID: threadID
quotes: quotes
backlinks: []
if Conf['Resurrect Quotes']
Quotify.node post
if Conf['Quote Preview']
QuotePreview.node post
if Conf['Quote Inline']
QuoteInline.node post
if Conf['Indicate OP quote']
QuoteOP.node post
if Conf['Indicate Cross-thread Quotes']
QuoteCT.node post
$.replace bq, clone
Main.prettify clone
ExpandThread =
init: ->
for span in $$ '.summary'
a = $.el 'a',
textContent: "+ #{span.textContent}"
className: 'summary desktop'
href: 'javascript:;'
$.on a, 'click', -> ExpandThread.toggle @parentNode
$.replace span, a
toggle: (thread) ->
url = "//api.4chan.org/#{g.BOARD}/res/#{thread.id[1..]}.json"
a = $ '.summary', thread
switch a.textContent[0]
when '+'
a.textContent = a.textContent.replace '+', '× Loading...'
$.cache url, -> ExpandThread.parse @, thread, a
when '×'
a.textContent = a.textContent.replace '× Loading...', '+'
$.cache.requests[url].abort()
when '-'
a.textContent = a.textContent.replace '-', '+'
#goddamit moot
num = switch g.BOARD
when 'b', 'vg', 'q' then 3
when 't' then 1
else 5
replies = $$ '.replyContainer', thread
replies.splice replies.length - num, num
for reply in replies
$.rm reply
return
parse: (req, thread, a) ->
if req.status isnt 200
a.textContent = "#{req.status} #{req.statusText}"
$.off a, 'click', ExpandThread.cb.toggle
return
a.textContent = a.textContent.replace '× Loading...', '-'
posts = JSON.parse(req.response).posts
if spoilerRange = posts[0].custom_spoiler
Build.spoilerRange[g.BOARD] = spoilerRange
replies = posts[1..]
threadID = thread.id[1..]
nodes = []
for reply in replies
post = Build.postFromObject reply, g.BOARD
id = reply.no
link = $ 'a[title="Highlight this post"]', post
link.href = "res/#{threadID}#p#{id}"
link.nextSibling.href = "res/#{threadID}#q#{id}"
nodes.push post
# eat everything, then replace with fresh full posts
for post in $$ '.summary ~ .replyContainer', a.parentNode
$.rm post
for backlink in $$ '.backlink', a.previousElementSibling
# Keep backlinks from other threads.
$.rm backlink unless $.id backlink.hash[1..]
$.after a, nodes
ThreadHiding =
init: ->
hiddenThreads = ThreadHiding.sync()
return if g.CATALOG
for thread in $$ '.thread'
a = $.el 'a',
className: 'hide_thread_button'
innerHTML: '[ - ]'
href: 'javascript:;'
$.on a, 'click', ThreadHiding.cb
$.prepend thread, a
if thread.id[1..] of hiddenThreads
ThreadHiding.hide thread
return
sync: ->
hiddenThreads = $.get "hiddenThreads/#{g.BOARD}/", {}
hiddenThreadsCatalog = JSON.parse(localStorage.getItem "4chan-hide-t-#{g.BOARD}") or {}
if g.CATALOG
for id of hiddenThreads
hiddenThreadsCatalog[id] = true
localStorage.setItem "4chan-hide-t-#{g.BOARD}", JSON.stringify hiddenThreadsCatalog
else
for id of hiddenThreadsCatalog
unless id of hiddenThreads
hiddenThreads[id] = Date.now()
$.set "hiddenThreads/#{g.BOARD}/", hiddenThreads
hiddenThreads
cb: ->
ThreadHiding.toggle $.x 'ancestor::div[parent::div[@class="board"]]', @
toggle: (thread) ->
hiddenThreads = $.get "hiddenThreads/#{g.BOARD}/", {}
id = thread.id[1..]
if thread.hidden or /\bhidden_thread\b/.test thread.firstChild.className
ThreadHiding.show thread
delete hiddenThreads[id]
else
ThreadHiding.hide thread
hiddenThreads[id] = Date.now()
$.set "hiddenThreads/#{g.BOARD}/", hiddenThreads
hide: (thread, show_stub=Conf['Show Stubs']) ->
unless show_stub
thread.hidden = true
thread.nextElementSibling.hidden = true
return
return if /\bhidden_thread\b/.test thread.firstChild.className # already hidden once by the filter
num = 0
if span = $ '.summary', thread
num = Number span.textContent.match /\d+/
num += $$('.opContainer ~ .replyContainer', thread).length
text = if num is 1 then '1 reply' else "#{num} replies"
opInfo = $('.desktop > .nameBlock', thread).textContent
stub = $.el 'div',
className: 'hide_thread_button hidden_thread'
innerHTML: '[ + ]'
a = stub.firstChild
$.on a, 'click', ThreadHiding.cb
$.add a, $.tn "#{opInfo} (#{text})"
if Conf['Menu']
menuButton = Menu.a.cloneNode true
$.on menuButton, 'click', Menu.toggle
$.add stub, [$.tn(' '), menuButton]
$.prepend thread, stub
show: (thread) ->
if stub = $ '.hidden_thread', thread
$.rm stub
thread.hidden = false
thread.nextElementSibling.hidden = false
ReplyHiding =
init: ->
Main.callbacks.push @node
node: (post) ->
return if post.isInlined or post.ID is post.threadID
side = $ '.sideArrows', post.root
$.addClass side, 'hide_reply_button'
side.innerHTML = '[ - ]'
$.on side.firstChild, 'click', ReplyHiding.toggle
if post.ID of g.hiddenReplies
ReplyHiding.hide post.root
toggle: ->
button = @parentNode
root = button.parentNode
id = root.id[2..]
quotes = $$ ".quotelink[href$='#p#{id}'], .backlink[href$='#p#{id}']"
if /\bstub\b/.test button.className
ReplyHiding.show root
for quote in quotes
$.rmClass quote, 'filtered'
delete g.hiddenReplies[id]
else
ReplyHiding.hide root
for quote in quotes
$.addClass quote, 'filtered'
g.hiddenReplies[id] = Date.now()
$.set "hiddenReplies/#{g.BOARD}/", g.hiddenReplies
hide: (root, show_stub=Conf['Show Stubs']) ->
side = $ '.sideArrows', root
return if side.hidden # already hidden once by the filter
side.hidden = true
el = side.nextElementSibling
el.hidden = true
return unless show_stub
stub = $.el 'div',
className: 'hide_reply_button stub'
innerHTML: '[ + ]'
a = stub.firstChild
$.on a, 'click', ReplyHiding.toggle
$.add a, $.tn if Conf['Anonymize'] then 'Anonymous' else $('.desktop > .nameBlock', el).textContent
if Conf['Menu']
menuButton = Menu.a.cloneNode true
$.on menuButton, 'click', Menu.toggle
$.add stub, [$.tn(' '), menuButton]
$.prepend root, stub
show: (root) ->
if stub = $ '.stub', root
$.rm stub
$('.sideArrows', root).hidden = false
$('.post', root).hidden = false
Menu =
entries: []
init: ->
@a = $.el 'a',
className: 'menu_button'
href: 'javascript:;'
innerHTML: '[]'
@el = $.el 'div',
className: 'reply dialog'
id: 'menu'
tabIndex: 0
$.on @el, 'click', (e) -> e.stopPropagation()
$.on @el, 'keydown', @keybinds
# Doc here: https://github.com/MayhemYDG/4chan-x/wiki/Menu-API
$.on d, 'AddMenuEntry', (e) -> Menu.addEntry e.detail
Main.callbacks.push @node
node: (post) ->
if post.isInlined and !post.isCrosspost
a = $ '.menu_button', post.el
else
a = Menu.a.cloneNode true
# \u00A0 is nbsp
$.add $('.postInfo', post.el), [$.tn('\u00A0'), a]
$.on a, 'click', Menu.toggle
toggle: (e) ->
e.preventDefault()
e.stopPropagation()
if Menu.el.parentNode
# Close if it's already opened.
# Reopen if we clicked on another button.
{lastOpener} = Menu
Menu.close()
return if lastOpener is @
Menu.lastOpener = @
post =
if /\bhidden_thread\b/.test @parentNode.className
$.x 'ancestor::div[parent::div[@class="board"]]/child::div[contains(@class,"opContainer")]', @
else
$.x 'ancestor::div[contains(@class,"postContainer")][1]', @
Menu.open @, Main.preParse post
open: (button, post) ->
{el} = Menu
# XXX GM/Scriptish require setAttribute
el.setAttribute 'data-id', post.ID
el.setAttribute 'data-rootid', post.root.id
funk = (entry, parent) ->
{children} = entry
return unless entry.open post
$.add parent, entry.el
return unless children
if subMenu = $ '.subMenu', entry.el
# Reset sub menu, remove irrelevant entries.
$.rm subMenu
subMenu = $.el 'div',
className: 'reply dialog subMenu'
$.add entry.el, subMenu
for child in children
funk child, subMenu
return
for entry in Menu.entries
funk entry, el
Menu.focus $ '.entry', Menu.el
$.on d, 'click', Menu.close
$.add d.body, el
# Position
mRect = el.getBoundingClientRect()
bRect = button.getBoundingClientRect()
bTop = d.documentElement.scrollTop + d.body.scrollTop + bRect.top
bLeft = d.documentElement.scrollLeft + d.body.scrollLeft + bRect.left
el.style.top =
if bRect.top + bRect.height + mRect.height < d.documentElement.clientHeight
bTop + bRect.height + 2 + 'px'
else
bTop - mRect.height - 2 + 'px'
el.style.left =
if bRect.left + mRect.width < d.documentElement.clientWidth
bLeft + 'px'
else
bLeft + bRect.width - mRect.width + 'px'
el.focus()
close: ->
{el} = Menu
$.rm el
for focused in $$ '.focused.entry', el
$.rmClass focused, 'focused'
el.innerHTML = null
el.removeAttribute 'style'
delete Menu.lastOpener
delete Menu.focusedEntry
$.off d, 'click', Menu.close
keybinds: (e) ->
el = Menu.focusedEntry
switch Keybinds.keyCode(e) or e.keyCode
when 'Esc'
Menu.lastOpener.focus()
Menu.close()
when 13, 32 # 'Enter', 'Space'
el.click()
when 'Up'
if next = el.previousElementSibling
Menu.focus next
when 'Down'
if next = el.nextElementSibling
Menu.focus next
when 'Right'
if (subMenu = $ '.subMenu', el) and next = subMenu.firstElementChild
Menu.focus next
when 'Left'
if next = $.x 'parent::*[contains(@class,"subMenu")]/parent::*', el
Menu.focus next
else
return
e.preventDefault()
e.stopPropagation()
focus: (el) ->
if focused = $.x 'parent::*/child::*[contains(@class,"focused")]', el
$.rmClass focused, 'focused'
for focused in $$ '.focused', el
$.rmClass focused, 'focused'
Menu.focusedEntry = el
$.addClass el, 'focused'
addEntry: (entry) ->
funk = (entry) ->
{el, children} = entry
$.addClass el, 'entry'
$.on el, 'focus mouseover', (e) ->
e.stopPropagation()
Menu.focus @
return unless children
$.addClass el, 'hasSubMenu'
for child in children
funk child
return
funk entry
Menu.entries.push entry
Keybinds =
init: ->
for node in $$ '[accesskey]'
node.removeAttribute 'accesskey'
$.on d, 'keydown', Keybinds.keydown
keydown: (e) ->
return unless key = Keybinds.keyCode e
{target} = e
if /TEXTAREA|INPUT/.test target.nodeName
return unless (key is 'Esc') or (/\+/.test key)
thread = Nav.getThread()
switch key
# QR & Options
when Conf.openQR
Keybinds.qr thread, true
when Conf.openEmptyQR
Keybinds.qr thread
when Conf.openOptions
Options.dialog() unless $.id 'overlay'
when Conf.close
if o = $.id 'overlay'
Options.close.call o
else if QR.el
QR.close()
when Conf.submit
QR.submit() if QR.el and !QR.status()
when Conf.spoiler
return if target.nodeName isnt 'TEXTAREA'
Keybinds.tags 'spoiler', target
when Conf.code
return if target.nodeName isnt 'TEXTAREA'
Keybinds.tags 'code', target
# Thread related
when Conf.watch
Watcher.toggle thread
when Conf.update
Updater.update()
when Conf.unreadCountTo0
Unread.replies = []
Unread.update true
# Images
when Conf.expandImage
Keybinds.img thread
when Conf.expandAllImages
Keybinds.img thread, true
# Board Navigation
when Conf.zero
window.location = "/#{g.BOARD}/0#delform"
when Conf.nextPage
if form = $ '.next form'
window.location = form.action
when Conf.previousPage
if form = $ '.prev form'
window.location = form.action
# Thread Navigation
when Conf.nextThread
return if g.REPLY
Nav.scroll +1
when Conf.previousThread
return if g.REPLY
Nav.scroll -1
when Conf.expandThread
ExpandThread.toggle thread
when Conf.openThread
Keybinds.open thread
when Conf.openThreadTab
Keybinds.open thread, true
# Reply Navigation
when Conf.nextReply
Keybinds.hl +1, thread
when Conf.previousReply
Keybinds.hl -1, thread
when Conf.hide
ThreadHiding.toggle thread if /\bthread\b/.test thread.className
else
return
e.preventDefault()
keyCode: (e) ->
key = switch kc = e.keyCode
when 8
''
when 13
'Enter'
when 27
'Esc'
when 37
'Left'
when 38
'Up'
when 39
'Right'
when 40
'Down'
when 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 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 #0-9, A-Z
c = String.fromCharCode kc
if e.shiftKey then c else c.toLowerCase()
else
null
if key
if e.altKey then key = 'alt+' + key
if e.ctrlKey then key = 'ctrl+' + key
if e.metaKey then key = 'meta+' + key
key
tags: (tag, ta) ->
value = ta.value
selStart = ta.selectionStart
selEnd = ta.selectionEnd
ta.value =
value[...selStart] +
"[#{tag}]" + value[selStart...selEnd] + "[/#{tag}]" +
value[selEnd..]
range = "[#{tag}]".length + selEnd
# Move the caret to the end of the selection.
ta.setSelectionRange range, range
# Fire the 'input' event
$.event ta, new Event 'input'
img: (thread, all) ->
if all
$.id('imageExpand').click()
else
thumb = $ 'img[data-md5]', $('.post.highlight', thread) or thread
ImageExpand.toggle thumb.parentNode
qr: (thread, quote) ->
if quote
QR.quote.call $ 'a[title="Quote this post"]', $('.post.highlight', thread) or thread
else
QR.open()
$('textarea', QR.el).focus()
open: (thread, tab) ->
return if g.REPLY
id = thread.id[1..]
url = "//boards.4chan.org/#{g.BOARD}/res/#{id}"
if tab
$.open url
else
location.href = url
hl: (delta, thread) ->
if post = $ '.reply.highlight', thread
$.rmClass post, 'highlight'
post.removeAttribute 'tabindex'
rect = post.getBoundingClientRect()
if rect.bottom >= 0 and rect.top <= d.documentElement.clientHeight # We're at least partially visible
next = $.x 'child::div[contains(@class,"post reply")]',
if delta is +1 then post.parentNode.nextElementSibling else post.parentNode.previousElementSibling
unless next
@focus post
return
return unless g.REPLY or $.x('ancestor::div[parent::div[@class="board"]]', next) is thread
rect = next.getBoundingClientRect()
if rect.top < 0 or rect.bottom > d.documentElement.clientHeight
next.scrollIntoView delta is -1
@focus next
return
replies = $$ '.reply', thread
replies.reverse() if delta is -1
for reply in replies
rect = reply.getBoundingClientRect()
if delta is +1 and rect.top >= 0 or delta is -1 and rect.bottom <= d.documentElement.clientHeight
@focus reply
return
focus: (post) ->
$.addClass post, 'highlight'
post.tabIndex = 0
post.focus()
Nav =
# ◀ ▶
init: ->
span = $.el 'span',
id: 'navlinks'
prev = $.el 'a',
textContent: '▲'
href: 'javascript:;'
next = $.el 'a',
textContent: '▼'
href: 'javascript:;'
$.on prev, 'click', @prev
$.on next, 'click', @next
$.add span, [prev, $.tn(' '), next]
$.add d.body, span
prev: ->
if g.REPLY
window.scrollTo 0, 0
else
Nav.scroll -1
next: ->
if g.REPLY
window.scrollTo 0, d.body.scrollHeight
else
Nav.scroll +1
getThread: (full) ->
Nav.threads = $$ '.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 $ '.board'
scroll: (delta) ->
[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
{top} = Nav.threads[i]?.getBoundingClientRect()
window.scrollBy 0, top
QR =
init: ->
return unless $.id 'postForm'
Main.callbacks.push @node
setTimeout @asyncInit
asyncInit: ->
if Conf['Hide Original Post Form']
link = $.el 'h1', innerHTML: "#{if g.REPLY then 'Reply to Thread' else 'Start a Thread'}"
$.on link.firstChild, 'click', ->
QR.open()
unless g.REPLY
QR.threadSelector.value =
if g.BOARD is 'f'
'9999'
else
'new'
$('textarea', QR.el).focus()
$.before $.id('postForm'), link
if Conf['Persistent QR']
QR.dialog()
QR.hide() if Conf['Auto Hide QR']
$.on d, 'dragover', QR.dragOver
$.on d, 'drop', QR.dropFile
$.on d, 'dragstart dragend', QR.drag
node: (post) ->
$.on $('a[title="Quote this post"]', $('.postInfo', post.el)), 'click', QR.quote
open: ->
if QR.el
QR.el.hidden = false
QR.unhide()
else
QR.dialog()
close: ->
QR.el.hidden = true
QR.abort()
d.activeElement.blur()
$.rmClass QR.el, 'dump'
for i in QR.replies
QR.replies[0].rm()
QR.cooldown.auto = false
QR.status()
QR.resetFileInput()
if not Conf['Remember Spoiler'] and (spoiler = $.id 'spoiler').checked
spoiler.click()
QR.cleanError()
hide: ->
d.activeElement.blur()
$.addClass QR.el, 'autohide'
$.id('autohide').checked = true
unhide: ->
$.rmClass QR.el, 'autohide'
$.id('autohide').checked = false
toggleHide: ->
@checked and QR.hide() or QR.unhide()
error: (err) ->
el = $ '.warning', QR.el
if typeof err is 'string'
el.textContent = err
else
el.innerHTML = null
$.add el, err
QR.open()
if QR.captcha.isEnabled and /captcha|verification/i.test el.textContent
# Focus the captcha input on captcha error.
$('[autocomplete]', QR.el).focus()
alert el.textContent if d.hidden
cleanError: ->
$('.warning', QR.el).textContent = null
status: (data={}) ->
return unless QR.el
if g.dead
value = 404
disabled = true
QR.cooldown.auto = false
value = data.progress or QR.cooldown.seconds or value
{input} = QR.status
input.value =
if QR.cooldown.auto and Conf['Cooldown']
if value then "Auto #{value}" else 'Auto'
else
value or 'Submit'
input.disabled = disabled or false
cooldown:
init: ->
return unless Conf['Cooldown']
QR.cooldown.types =
thread: switch g.BOARD
when 'q' then 86400
when 'b', 'soc', 'r9k' then 600
else 300
sage: if g.BOARD is 'q' then 600 else 60
file: if g.BOARD is 'q' then 300 else 30
post: if g.BOARD is 'q' then 60 else 30
QR.cooldown.cooldowns = $.get "#{g.BOARD}.cooldown", {}
QR.cooldown.start()
$.sync "#{g.BOARD}.cooldown", QR.cooldown.sync
start: ->
return if QR.cooldown.isCounting
QR.cooldown.isCounting = true
QR.cooldown.count()
sync: (cooldowns) ->
# Add each cooldowns, don't overwrite everything in case we
# still need to purge one in the current tab to auto-post.
for id of cooldowns
QR.cooldown.cooldowns[id] = cooldowns[id]
QR.cooldown.start()
set: (data) ->
return unless Conf['Cooldown']
start = Date.now()
if data.delay
cooldown = delay: data.delay
else
isSage = /sage/i.test data.post.email
hasFile = !!data.post.file
isReply = data.isReply
type =
unless isReply
'thread'
else if isSage
'sage'
else if hasFile
'file'
else
'post'
cooldown =
isReply: isReply
isSage: isSage
hasFile: hasFile
timeout: start + QR.cooldown.types[type] * $.SECOND
QR.cooldown.cooldowns[start] = cooldown
$.set "#{g.BOARD}.cooldown", QR.cooldown.cooldowns
QR.cooldown.start()
unset: (id) ->
delete QR.cooldown.cooldowns[id]
$.set "#{g.BOARD}.cooldown", QR.cooldown.cooldowns
count: ->
if Object.keys(QR.cooldown.cooldowns).length
setTimeout QR.cooldown.count, 1000
else
$.delete "#{g.BOARD}.cooldown"
delete QR.cooldown.isCounting
delete QR.cooldown.seconds
QR.status()
return
if (isReply = if g.REPLY then true else QR.threadSelector.value isnt 'new')
post = QR.replies[0]
isSage = /sage/i.test post.email
hasFile = !!post.file
now = Date.now()
seconds = null
{types, cooldowns} = QR.cooldown
for start, cooldown of cooldowns
if 'delay' of cooldown
if cooldown.delay
seconds = Math.max seconds, cooldown.delay--
else
seconds = Math.max seconds, 0
QR.cooldown.unset start
continue
if isReply is cooldown.isReply
# Only cooldowns relevant to this post can set the seconds value.
# Unset outdated cooldowns that can no longer impact us.
type =
unless isReply
'thread'
else if isSage and cooldown.isSage
'sage'
else if hasFile and cooldown.hasFile
'file'
else
'post'
elapsed = Math.floor (now - start) / 1000
if elapsed >= 0 # clock changed since then?
seconds = Math.max seconds, types[type] - elapsed
unless start <= now <= cooldown.timeout
QR.cooldown.unset start
# Update the status when we change posting type.
# Don't get stuck at some random number.
# Don't interfere with progress status updates.
update = seconds isnt null or !!QR.cooldown.seconds
QR.cooldown.seconds = seconds
QR.status() if update
QR.submit() if seconds is 0 and QR.cooldown.auto
quote: (e) ->
e?.preventDefault()
QR.open()
ta = $ 'textarea', QR.el
unless g.REPLY or ta.value
QR.threadSelector.value = $.x('ancestor::div[parent::div[@class="board"]]', @).id[1..]
# Make sure we get the correct number, even with XXX censors
id = @previousSibling.hash[2..]
text = ">>#{id}\n"
sel = d.getSelection()
if (s = sel.toString().trim()) and id is $.x('ancestor-or-self::blockquote', sel.anchorNode)?.id.match(/\d+$/)[0]
# XXX Opera doesn't retain `\n`s?
s = s.replace /\n/g, '\n>'
text += ">#{s}\n"
caretPos = ta.selectionStart
# Replace selection for text.
ta.value = ta.value[...caretPos] + text + ta.value[ta.selectionEnd..]
# Move the caret to the end of the new quote.
range = caretPos + text.length
ta.setSelectionRange range, range
ta.focus()
# Fire the 'input' event
$.event ta, new Event 'input'
characterCount: ->
counter = QR.charaCounter
count = @textLength
counter.textContent = count
counter.hidden = count < 1000
(if count > 1500 then $.addClass else $.rmClass) counter, 'warning'
drag: (e) ->
# Let it drag anything from the page.
toggle = if e.type is 'dragstart' then $.off else $.on
toggle d, 'dragover', QR.dragOver
toggle d, 'drop', QR.dropFile
dragOver: (e) ->
e.preventDefault()
e.dataTransfer.dropEffect = 'copy' # cursor feedback
dropFile: (e) ->
# Let it only handle files from the desktop.
return unless e.dataTransfer.files.length
e.preventDefault()
QR.open()
QR.fileInput.call e.dataTransfer
$.addClass QR.el, 'dump'
fileInput: ->
QR.cleanError()
# Set or change current reply's file.
if @files.length is 1
file = @files[0]
if file.size > @max
QR.error 'File too large.'
QR.resetFileInput()
else if -1 is QR.mimeTypes.indexOf file.type
QR.error 'Unsupported file type.'
QR.resetFileInput()
else
QR.selected.setFile file
return
# Create new replies with these files.
for file in @files
if file.size > @max
QR.error "File #{file.name} is too large."
break
else if -1 is QR.mimeTypes.indexOf file.type
QR.error "#{file.name}: Unsupported file type."
break
unless QR.replies[QR.replies.length - 1].file
# set last reply's file
QR.replies[QR.replies.length - 1].setFile file
else
new QR.reply().setFile file
$.addClass QR.el, 'dump'
QR.resetFileInput() # reset input
resetFileInput: ->
$('[type=file]', QR.el).value = null
replies: []
reply: class
constructor: ->
# set values, or null, to avoid 'undefined' values in inputs
prev = QR.replies[QR.replies.length-1]
persona = $.get 'QR.persona', {}
@name = if prev then prev.name else persona.name or null
@email = if prev and !/^sage$/.test prev.email then prev.email else persona.email or null
@sub = if prev and Conf['Remember Subject'] then prev.sub else if Conf['Remember Subject'] then persona.sub else null
@spoiler = if prev and Conf['Remember Spoiler'] then prev.spoiler else false
@com = null
@el = $.el 'a',
className: 'thumbnail'
draggable: true
href: 'javascript:;'
innerHTML: '×'
$('input', @el).checked = @spoiler
$.on @el, 'click', => @select()
$.on $('.remove', @el), 'click', (e) =>
e.stopPropagation()
@rm()
$.on $('label', @el), 'click', (e) => e.stopPropagation()
$.on $('input', @el), 'change', (e) =>
@spoiler = e.target.checked
$.id('spoiler').checked = @spoiler if @el.id is 'selected'
$.before $('#addReply', QR.el), @el
$.on @el, 'dragstart', @dragStart
$.on @el, 'dragenter', @dragEnter
$.on @el, 'dragleave', @dragLeave
$.on @el, 'dragover', @dragOver
$.on @el, 'dragend', @dragEnd
$.on @el, 'drop', @drop
QR.replies.push @
setFile: (@file) ->
@el.title = "#{file.name} (#{$.bytesToString file.size})"
$('label', @el).hidden = false if QR.spoiler
unless /^image/.test file.type
@el.style.backgroundImage = null
return
# XXX Opera does not support window.URL
return unless url = window.URL or window.webkitURL
url.revokeObjectURL @url
# Create a redimensioned thumbnail.
fileUrl = url.createObjectURL file
img = $.el 'img'
$.on img, 'load', =>
# Generate thumbnails only if they're really big.
# Resized pictures through canvases look like ass,
# so we generate thumbnails `s` times bigger then expected
# to avoid crappy resized quality.
s = 90*3
if img.height < s or img.width < s
@url = fileUrl
@el.style.backgroundImage = "url(#{@url})"
return
if img.height <= img.width
img.width = s / img.height * img.width
img.height = s
else
img.height = s / img.width * img.height
img.width = s
c = $.el 'canvas'
c.height = img.height
c.width = img.width
c.getContext('2d').drawImage img, 0, 0, img.width, img.height
# Support for toBlob fucking when?
data = atob c.toDataURL().split(',')[1]
# DataUrl to Binary code from Aeosynth's 4chan X repo
l = data.length
ui8a = new Uint8Array l
for i in [0...l]
ui8a[i] = data.charCodeAt i
@url = url.createObjectURL new Blob [ui8a], type: 'image/png'
@el.style.backgroundImage = "url(#{@url})"
url.revokeObjectURL? fileUrl
img.src = fileUrl
rmFile: ->
QR.resetFileInput()
delete @file
@el.title = null
@el.style.backgroundImage = null
$('label', @el).hidden = true if QR.spoiler
(window.URL or window.webkitURL).revokeObjectURL? @url
select: ->
QR.selected?.el.id = null
QR.selected = @
@el.id = 'selected'
# Scroll the list to center the focused reply.
rectEl = @el.getBoundingClientRect()
rectList = @el.parentNode.getBoundingClientRect()
@el.parentNode.scrollLeft += rectEl.left + rectEl.width/2 - rectList.left - rectList.width/2
# Load this reply's values.
for data in ['name', 'email', 'sub', 'com']
$("[name=#{data}]", QR.el).value = @[data]
QR.characterCount.call $ 'textarea', QR.el
$('#spoiler', QR.el).checked = @spoiler
dragStart: ->
$.addClass @, 'drag'
dragEnter: ->
$.addClass @, 'over'
dragLeave: ->
$.rmClass @, 'over'
dragOver: (e) ->
e.preventDefault()
e.dataTransfer.dropEffect = 'move'
drop: ->
el = $ '.drag', @parentNode
index = (el) -> Array::slice.call(el.parentNode.children).indexOf el
oldIndex = index el
newIndex = index @
if oldIndex < newIndex
$.after @, el
else
$.before @, el
reply = QR.replies.splice(oldIndex, 1)[0]
QR.replies.splice newIndex, 0, reply
dragEnd: ->
$.rmClass @, 'drag'
if el = $ '.over', @parentNode
$.rmClass el, 'over'
rm: ->
QR.resetFileInput()
$.rm @el
index = QR.replies.indexOf @
if QR.replies.length is 1
new QR.reply().select()
else if @el.id is 'selected'
(QR.replies[index-1] or QR.replies[index+1]).select()
QR.replies.splice index, 1
(window.URL or window.webkitURL).revokeObjectURL? @url
captcha:
init: ->
return if -1 isnt d.cookie.indexOf 'pass_enabled='
return unless @isEnabled = !!$.id 'captchaFormPart'
if $.id 'recaptcha_challenge_field_holder'
@ready()
else
@onready = => @ready()
$.on $.id('recaptcha_widget_div'), 'DOMNodeInserted', @onready
ready: ->
if @challenge = $.id 'recaptcha_challenge_field_holder'
$.off $.id('recaptcha_widget_div'), 'DOMNodeInserted', @onready
delete @onready
else
return
$.addClass QR.el, 'captcha'
$.after $('.textarea', QR.el), $.el 'div',
className: 'captchaimg'
title: 'Reload'
innerHTML: ''
$.after $('.captchaimg', QR.el), $.el 'div',
className: 'captchainput'
innerHTML: ''
@img = $ '.captchaimg > img', QR.el
@input = $ '.captchainput > input', QR.el
$.on @img.parentNode, 'click', @reload
$.on @input, 'keydown', @keydown
$.on @challenge, 'DOMNodeInserted', => @load()
$.sync 'captchas', (arr) => @count arr.length
@count $.get('captchas', []).length
# start with an uncached captcha
@reload()
save: ->
return unless response = @input.value
captchas = $.get 'captchas', []
# Remove old captchas.
while (captcha = captchas[0]) and captcha.time < Date.now()
captchas.shift()
captchas.push
challenge: @challenge.firstChild.value
response: response
time: @timeout
$.set 'captchas', captchas
@count captchas.length
@reload()
load: ->
# Timeout is available at RecaptchaState.timeout in seconds.
# We use 5-1 minutes to give upload some time.
@timeout = Date.now() + 4*$.MINUTE
challenge = @challenge.firstChild.value
@img.alt = challenge
@img.src = "//www.google.com/recaptcha/api/image?c=#{challenge}"
@input.value = null
count: (count) ->
@input.placeholder = switch count
when 0
'Verification (Shift + Enter to cache)'
when 1
'Verification (1 cached captcha)'
else
"Verification (#{count} cached captchas)"
@input.alt = count # For XTRM RICE.
reload: (focus) ->
# the "t" argument prevents the input from being focused
$.globalEval 'javascript:Recaptcha.reload("t")'
# Focus if we meant to.
QR.captcha.input.focus() if focus
keydown: (e) ->
c = QR.captcha
if e.keyCode is 8 and not c.input.value
c.reload()
else if e.keyCode is 13 and e.shiftKey
c.save()
else
return
e.preventDefault()
dialog: ->
QR.el = UI.dialog 'qr', 'top:0;right:0;', '
'
if Conf['Remember QR size'] and $.engine is 'gecko'
$.on ta = $('textarea', QR.el), 'mouseup', ->
$.set 'QR.size', @style.cssText
ta.style.cssText = $.get 'QR.size', ''
# Allow only this board's supported files.
mimeTypes = $('ul.rules').firstElementChild.textContent.trim().match(/: (.+)/)[1].toLowerCase().replace /\w+/g, (type) ->
switch type
when 'jpg'
'image/jpeg'
when 'pdf'
'application/pdf'
when 'swf'
'application/x-shockwave-flash'
else
"image/#{type}"
QR.mimeTypes = mimeTypes.split ', '
# Add empty mimeType to avoid errors with URLs selected in Window's file dialog.
QR.mimeTypes.push ''
fileInput = $ 'input[type=file]', QR.el
fileInput.max = $('input[name=MAX_FILE_SIZE]').value
fileInput.accept = mimeTypes if $.engine isnt 'presto' # Opera's accept attribute is fucked up
QR.spoiler = !!$ 'input[name=spoiler]'
spoiler = $ '#spoilerLabel', QR.el
spoiler.hidden = !QR.spoiler
QR.charaCounter = $ '#charCount', QR.el
ta = $ 'textarea', QR.el
unless g.REPLY
# Make a list with visible threads and an option to create a new one.
threads = ''
for thread in $$ '.thread'
id = thread.id[1..]
threads += ""
QR.threadSelector =
if g.BOARD is 'f'
$('select[name=filetag]').cloneNode true
else
$.el 'select',
innerHTML: threads
title: 'Create a new thread / Reply to a thread'
$.prepend $('.move > span', QR.el), QR.threadSelector
$.on QR.threadSelector, 'mousedown', (e) -> e.stopPropagation()
$.on $('#autohide', QR.el), 'change', QR.toggleHide
$.on $('.close', QR.el), 'click', QR.close
$.on $('#dump', QR.el), 'click', -> QR.el.classList.toggle 'dump'
$.on $('#addReply', QR.el), 'click', -> new QR.reply().select()
$.on $('form', QR.el), 'submit', QR.submit
$.on ta, 'input', -> QR.selected.el.lastChild.textContent = @value
$.on ta, 'input', QR.characterCount
$.on fileInput, 'change', QR.fileInput
$.on fileInput, 'click', (e) -> if e.shiftKey then QR.selected.rmFile() or e.preventDefault()
$.on spoiler.firstChild, 'change', -> $('input', QR.selected.el).click()
$.on $('.warning', QR.el), 'click', QR.cleanError
new QR.reply().select()
# save selected reply's data
for name in ['name', 'email', 'sub', 'com']
# The input event replaces keyup, change and paste events.
$.on $("[name=#{name}]", QR.el), 'input', ->
QR.selected[@name] = @value
# Disable auto-posting if you're typing in the first reply
# during the last 5 seconds of the cooldown.
if QR.cooldown.auto and QR.selected is QR.replies[0] and 0 < QR.cooldown.seconds <= 5
QR.cooldown.auto = false
QR.status.input = $ 'input[type=submit]', QR.el
QR.status()
QR.cooldown.init()
QR.captcha.init()
$.add d.body, QR.el
# Create a custom event when the QR dialog is first initialized.
# Use it to extend the QR's functionalities, or for XTRM RICE.
$.event QR.el, new CustomEvent 'QRDialogCreation',
bubbles: true
submit: (e) ->
e?.preventDefault()
if QR.cooldown.seconds
QR.cooldown.auto = !QR.cooldown.auto
QR.status()
return
QR.abort()
reply = QR.replies[0]
if g.BOARD is 'f' and not g.REPLY
filetag = QR.threadSelector.value
threadID = 'new'
else
threadID = g.THREAD_ID or QR.threadSelector.value
# prevent errors
if threadID is 'new'
threadID = null
if g.BOARD in ['vg', 'q'] and !reply.sub
err = 'New threads require a subject.'
else unless reply.file or textOnly = !!$ 'input[name=textonly]', $.id 'postForm'
err = 'No file selected.'
else if g.BOARD is 'f' and filetag is '9999'
err = 'Invalid tag specified.'
else unless reply.com or reply.file
err = 'No file selected.'
if QR.captcha.isEnabled and !err
# get oldest valid captcha
captchas = $.get 'captchas', []
# remove old captchas
while (captcha = captchas[0]) and captcha.time < Date.now()
captchas.shift()
if captcha = captchas.shift()
challenge = captcha.challenge
response = captcha.response
else
challenge = QR.captcha.img.alt
if response = QR.captcha.input.value then QR.captcha.reload()
$.set 'captchas', captchas
QR.captcha.count captchas.length
unless response
err = 'No valid captcha.'
else
response = response.trim()
# one-word-captcha:
# If there's only one word, duplicate it.
response = "#{response} #{response}" unless /\s/.test response
if err
# stop auto-posting
QR.cooldown.auto = false
QR.status()
QR.error err
return
QR.cleanError()
# Enable auto-posting if we have stuff to post, disable it otherwise.
QR.cooldown.auto = QR.replies.length > 1
if Conf['Auto Hide QR'] and not QR.cooldown.auto
QR.hide()
if not QR.cooldown.auto and $.x 'ancestor::div[@id="qr"]', d.activeElement
# Unfocus the focused element if it is one within the QR and we're not auto-posting.
d.activeElement.blur()
# Starting to upload might take some time.
# Provide some feedback that we're starting to submit.
QR.status progress: '...'
post =
resto: threadID
name: reply.name
email: reply.email
sub: reply.sub
com: reply.com
upfile: reply.file
filetag: filetag
spoiler: reply.spoiler
textonly: textOnly
mode: 'regist'
pwd: if m = d.cookie.match(/4chan_pass=([^;]+)/) then decodeURIComponent m[1] else $('input[name=pwd]').value
recaptcha_challenge_field: challenge
recaptcha_response_field: response
callbacks =
onload: ->
QR.response @response
onerror: ->
# Connection error, or
# CORS disabled error on www.4chan.org/banned
QR.cooldown.auto = false
QR.status()
QR.error $.el 'a',
href: '//www.4chan.org/banned',
target: '_blank',
textContent: 'Connection error, or you are banned.'
opts =
form: $.formData post
upCallbacks:
onload: ->
# Upload done, waiting for response.
QR.status progress: '...'
onprogress: (e) ->
# Uploading...
QR.status progress: "#{Math.round e.loaded / e.total * 100}%"
QR.ajax = $.ajax $.id('postForm').parentNode.action, callbacks, opts
response: (html) ->
doc = d.implementation.createHTMLDocument ''
doc.documentElement.innerHTML = html
if ban = $ '.banType', doc # banned/warning
board = $('.board', doc).innerHTML
err = $.el 'span', innerHTML:
if ban.textContent.toLowerCase() is 'banned'
"You are banned on #{board}! ;_; " +
"Click here to see the reason."
else
"You were issued a warning on #{board} as #{$('.nameBlock', doc).innerHTML}. " +
"Reason: #{$('.reason', doc).innerHTML}"
else if err = doc.getElementById 'errmsg' # error!
$('a', err)?.target = '_blank' # duplicate image link
else if doc.title isnt 'Post successful!'
err = 'Connection error with sys.4chan.org.'
if err
if /captcha|verification/i.test(err.textContent) or err is 'Connection error with sys.4chan.org.'
# Remove the obnoxious 4chan Pass ad.
if /mistyped/i.test err.textContent
err = 'Error: You seem to have mistyped the CAPTCHA.'
# Enable auto-post if we have some cached captchas.
QR.cooldown.auto =
if QR.captcha.isEnabled
!!$.get('captchas', []).length
else if err is 'Connection error with sys.4chan.org.'
true
else
# Something must've gone terribly wrong if you get captcha errors without captchas.
# Don't auto-post indefinitely in that case.
false
# Too many frequent mistyped captchas will auto-ban you!
# On connection error, the post most likely didn't go through.
QR.cooldown.set delay: 2
else # stop auto-posting
QR.cooldown.auto = false
QR.status()
QR.error err
return
reply = QR.replies[0]
persona = $.get 'QR.persona', {}
persona =
name: reply.name
email: if /^sage$/.test reply.email then persona.email else reply.email
sub: if Conf['Remember Subject'] then reply.sub else null
$.set 'QR.persona', persona
[_, threadID, postID] = doc.body.lastChild.textContent.match /thread:(\d+),no:(\d+)/
# Post/upload confirmed as successful.
$.event QR.el, new CustomEvent 'QRPostSuccessful',
bubbles: true
detail:
threadID: threadID
postID: postID
QR.cooldown.set
post: reply
isReply: threadID isnt '0'
if threadID is '0' # new thread
# auto-noko
location.pathname = "/#{g.BOARD}/res/#{postID}"
else
# Enable auto-posting if we have stuff to post, disable it otherwise.
QR.cooldown.auto = QR.replies.length > 1
if Conf['Open Reply in New Tab'] and !g.REPLY and !QR.cooldown.auto
$.open "//boards.4chan.org/#{g.BOARD}/res/#{threadID}#p#{postID}"
if Conf['Persistent QR'] or QR.cooldown.auto
reply.rm()
else
QR.close()
QR.status()
QR.resetFileInput()
abort: ->
QR.ajax?.abort()
delete QR.ajax
QR.status()
Options =
init: ->
$.ready Options.initReady
initReady: ->
for settings in ['navtopright', 'navbotright']
a = $.el 'a',
href: 'javascript:;'
className: 'settingsWindowLink'
textContent: '4chan X Settings'
$.on a, 'click', Options.dialog
setting = $.id settings
if Conf['Disable 4chan\'s extension']
$.replace setting.childNodes[1], a
continue
$.prepend setting, [$.tn('['), a, $.tn('] ')]
unless $.get 'firstrun'
$.set 'firstrun', true
# Prevent race conditions
Favicon.init() unless Favicon.el
Options.dialog()
dialog: ->
dialog = $.el 'div',
id: 'options'
className: 'reply dialog'
innerHTML: '
'
$.on $('#main_tab + div .export', dialog), 'click', Options.export
$.on $('#main_tab + div .import', dialog), 'click', Options.import
$.on $('#main_tab + div input', dialog), 'change', Options.onImport
#main
for key, obj of Config.main
ul = $.el 'ul',
textContent: key
for key, arr of obj
checked = if $.get(key, Conf[key]) then 'checked' else ''
description = arr[1]
li = $.el 'li',
innerHTML: ": #{description}"
$.on $('input', li), 'click', $.cb.checked
$.add ul, li
$.add $('#main_tab + div', dialog), ul
hiddenThreads = $.get "hiddenThreads/#{g.BOARD}/", {}
hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length
li = $.el 'li',
innerHTML: " : Forget all hidden posts. Useful if you accidentally hide a post and have \"Show Stubs\" disabled."
$.on $('button', li), 'click', Options.clearHidden
$.add $('ul:nth-child(2)', dialog), li
#filter
filter = $ 'select[name=filter]', dialog
$.on filter, 'change', Options.filter
#sauce
sauce = $ '#sauces', dialog
sauce.value = $.get sauce.name, Conf[sauce.name]
$.on sauce, 'change', $.cb.value
#rice
(back = $ '[name=backlink]', dialog).value = $.get 'backlink', Conf['backlink']
(time = $ '[name=time]', dialog).value = $.get 'time', Conf['time']
(fileInfo = $ '[name=fileInfo]', dialog).value = $.get 'fileInfo', Conf['fileInfo']
$.on back, 'input', $.cb.value
$.on back, 'input', Options.backlink
$.on time, 'input', $.cb.value
$.on time, 'input', Options.time
$.on fileInfo, 'input', $.cb.value
$.on fileInfo, 'input', Options.fileInfo
favicon = $ 'select[name=favicon]', dialog
favicon.value = $.get 'favicon', Conf['favicon']
$.on favicon, 'change', $.cb.value
$.on favicon, 'change', Options.favicon
#keybinds
for key, arr of Config.hotkeys
tr = $.el 'tr',
innerHTML: "
#{arr[1]}
"
input = $ 'input', tr
input.value = $.get key, Conf[key]
$.on input, 'keydown', Options.keybind
$.add $('#keybinds_tab + div tbody', dialog), tr
#indicate if the settings require a feature to be enabled
indicators = {}
for indicator in $$ '.warning', dialog
key = indicator.firstChild.textContent
indicator.hidden = $.get key, Conf[key]
indicators[key] = indicator
$.on $("[name='#{key}']", dialog), 'click', ->
indicators[@name].hidden = @checked
overlay = $.el 'div', id: 'overlay'
$.on overlay, 'click', Options.close
$.on dialog, 'click', (e) -> e.stopPropagation()
$.add overlay, dialog
$.add d.body, overlay
d.body.style.setProperty 'width', "#{d.body.clientWidth}px", null
$.addClass d.body, 'unscroll'
Options.filter.call filter
Options.backlink.call back
Options.time.call time
Options.fileInfo.call fileInfo
Options.favicon.call favicon
close: ->
$.rm this
d.body.style.removeProperty 'width'
$.rmClass d.body, 'unscroll'
clearHidden: ->
#'hidden' might be misleading; it's the number of IDs we're *looking* for,
# not the number of posts actually hidden on the page.
$.delete "hiddenReplies/#{g.BOARD}/"
$.delete "hiddenThreads/#{g.BOARD}/"
@textContent = "hidden: 0"
g.hiddenReplies = {}
keybind: (e) ->
return if e.keyCode is 9
e.preventDefault()
e.stopPropagation()
return unless (key = Keybinds.keyCode e)?
@value = key
$.cb.value.call @
filter: ->
el = @nextSibling
if (name = @value) isnt 'guide'
ta = $.el 'textarea',
name: name
className: 'field'
value: $.get name, Conf[name]
$.on ta, 'change', $.cb.value
$.replace el, ta
return
$.rm el if el
$.after @, $.el 'article',
innerHTML: '
Use regular expressions, one per line.
Lines starting with a # will be ignored.
For example, /weeaboo/i will filter posts containing the string `weeaboo`, case-insensitive.
You can use these settings with each regular expression, separate them with semicolons:
Per boards, separate them with commas. It is global if not specified.
For example: boards:a,jp;.
Filter OPs only along with their threads (`only`), replies only (`no`, this is default), or both (`yes`).
For example: op:only;, op:no; or op:yes;.
Overrule the `Show Stubs` setting if specified: create a stub (`yes`) or not (`no`).
For example: stub:yes; or stub:no;.
Highlight instead of hiding. You can specify a class name to use with a userstyle.
For example: highlight; or highlight:wallpaper;.
Highlighted OPs will have their threads put on top of board pages by default.
For example: top:yes; or top:no;.
'
time: ->
Time.foo()
Time.date = new Date()
$.id('timePreview').textContent = Time.funk Time
backlink: ->
$.id('backlinkPreview').textContent = Conf['backlink'].replace /%id/, '123456789'
fileInfo: ->
FileInfo.data =
link: '//images.4chan.org/g/src/1334437723720.jpg'
spoiler: true
size: '276'
unit: 'KB'
resolution: '1280x720'
fullname: 'd9bb2efc98dd0df141a94399ff5880b7.jpg'
shortname: 'd9bb2efc98dd0df141a94399ff5880(...).jpg'
FileInfo.setFormats()
$.id('fileInfoPreview').innerHTML = FileInfo.funk FileInfo
favicon: ->
Favicon.switch()
Unread.update true
@nextElementSibling.innerHTML = ""
export: ->
now = Date.now()
data =
version: Main.version
date: now
Conf: Conf
WatchedThreads: $.get('watched', {})
a = $.el 'a',
className: 'warning'
textContent: 'Save me!'
download: "4chan X v#{Main.version}-#{now}.json"
href: "data:application/json;base64,#{btoa unescape encodeURIComponent JSON.stringify data}"
target: '_blank'
if $.engine isnt 'gecko'
a.click()
return
# XXX Firefox won't let us download automatically.
output = @parentNode.nextElementSibling
output.innerHTML = null
$.add output, a
import: ->
@nextElementSibling.click()
onImport: ->
return unless file = @files[0]
output = @parentNode.nextElementSibling
unless confirm 'Your current settings will be entirely overwritten, are you sure?'
output.textContent = 'Import aborted.'
return
reader = new FileReader()
reader.onload = (e) ->
try
data = JSON.parse decodeURIComponent escape e.target.result
Options.loadSettings data
if confirm 'Import successful. Refresh now?'
window.location.reload()
catch err
output.textContent = 'Import failed due to an error.'
reader.readAsText file
loadSettings: (data) ->
for key, val of data.Conf
$.set key, val
$.set 'watched', data.WatchedThreads
Updater =
init: ->
html = '
'
{checkbox} = Config.updater
for name of checkbox
title = checkbox[name][1]
checked = if Conf[name] then 'checked' else ''
html += ""
checked = if Conf['Auto Update'] then 'checked' else ''
html += "
"
dialog = UI.dialog 'updater', 'bottom: 0; right: 0;', html
@count = $ '#count', dialog
@timer = $ '#timer', dialog
@thread = $.id "t#{g.THREAD_ID}"
@unsuccessfulFetchCount = 0
@lastModified = '0'
for input in $$ 'input', dialog
if input.type is 'checkbox'
$.on input, 'click', $.cb.checked
switch input.name
when 'Scroll BG'
$.on input, 'click', @cb.scrollBG
@cb.scrollBG.call input
when 'Verbose'
$.on input, 'click', @cb.verbose
@cb.verbose.call input
when 'Auto Update This'
$.on input, 'click', @cb.autoUpdate
@cb.autoUpdate.call input
when 'Interval'
input.value = Conf['Interval']
$.on input, 'change', @cb.interval
@cb.interval.call input
when 'Update Now'
$.on input, 'click', @update
$.add d.body, dialog
$.on d, 'QRPostSuccessful', @cb.post
$.on d, 'visibilitychange', @cb.visibility
###
http://freesound.org/people/pierrecartoons1979/sounds/90112/
cc-by-nc-3.0
###
audio: $.el('audio', src: 'data:audio/wav;base64,UklGRjQDAABXQVZFZm10IBAAAAABAAEAgD4AAIA+AAABAAgAc21wbDwAAABBAAADAAAAAAAAAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkYXRhzAIAAGMms8em0tleMV4zIpLVo8nhfSlcPR102Ki+5JspVEkdVtKzs+K1NEhUIT7DwKrcy0g6WygsrM2k1NpiLl0zIY/WpMrjgCdbPhxw2Kq+5Z4qUkkdU9K1s+K5NkVTITzBwqnczko3WikrqM+l1NxlLF0zIIvXpsnjgydZPhxs2ay95aIrUEkdUdC3suK8N0NUIjq+xKrcz002WioppdGm091pK1w0IIjYp8jkhydXPxxq2K295aUrTkoeTs65suK+OUFUIzi7xqrb0VA0WSoootKm0t5tKlo1H4TYqMfkiydWQBxm16+85actTEseS8y7seHAPD9TIza5yKra01QyWSson9On0d5wKVk2H4DYqcfkjidUQB1j1rG75KsvSkseScu8seDCPz1TJDW2yara1FYxWSwnm9Sn0N9zKVg2H33ZqsXkkihSQR1g1bK65K0wSEsfR8i+seDEQTxUJTOzy6rY1VowWC0mmNWoz993KVc3H3rYq8TklSlRQh1d1LS647AyR0wgRMbAsN/GRDpTJTKwzKrX1l4vVy4lldWpzt97KVY4IXbUr8LZljVPRCxhw7W3z6ZISkw1VK+4sMWvXEhSPk6buay9sm5JVkZNiLWqtrJ+TldNTnquqbCwilZXU1BwpKirrpNgWFhTaZmnpquZbFlbVmWOpaOonHZcXlljhaGhpZ1+YWBdYn2cn6GdhmdhYGN3lp2enIttY2Jjco+bnJuOdGZlZXCImJqakHpoZ2Zug5WYmZJ/bGlobX6RlpeSg3BqaW16jZSVkoZ0bGtteImSk5KIeG5tbnaFkJKRinxxbm91gY2QkIt/c3BwdH6Kj4+LgnZxcXR8iI2OjIR5c3J0e4WLjYuFe3VzdHmCioyLhn52dHR5gIiKioeAeHV1eH+GiYqHgXp2dnh9hIiJh4J8eHd4fIKHiIeDfXl4eHyBhoeHhH96eHmA')
cb:
post: ->
return unless Conf['Auto Update This']
Updater.unsuccessfulFetchCount = 0
setTimeout Updater.update, 500
visibility: ->
return if d.hidden
# Reset the counter when we focus this tab.
Updater.unsuccessfulFetchCount = 0
if Updater.timer.textContent < -Conf['Interval']
Updater.set 'timer', -Updater.getInterval()
interval: ->
val = parseInt @value, 10
@value = if val > 5 then val else 5
$.cb.value.call @
Updater.set 'timer', -Updater.getInterval()
verbose: ->
if Conf['Verbose']
Updater.set 'count', '+0'
Updater.timer.hidden = false
else
Updater.set 'count', 'Thread Updater'
Updater.count.className = ''
Updater.timer.hidden = true
autoUpdate: ->
if Conf['Auto Update This'] = @checked
Updater.timeoutID = setTimeout Updater.timeout, 1000
else
clearTimeout Updater.timeoutID
scrollBG: ->
Updater.scrollBG =
if @checked
-> true
else
-> ! d.hidden
load: ->
switch @status
when 404
Updater.set 'timer', ''
Updater.set 'count', 404
Updater.count.className = 'warning'
clearTimeout Updater.timeoutID
g.dead = true
if Conf['Unread Count']
Unread.title = Unread.title.match(/^.+-/)[0] + ' 404'
else
d.title = d.title.match(/^.+-/)[0] + ' 404'
Unread.update true
QR.abort()
# XXX 304 -> 0 in Opera
when 0, 304
###
Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response.
This saves bandwidth for both the user and the servers and avoid unnecessary computation.
###
Updater.unsuccessfulFetchCount++
Updater.set 'timer', -Updater.getInterval()
if Conf['Verbose']
Updater.set 'count', '+0'
Updater.count.className = null
when 200
Updater.lastModified = @getResponseHeader 'Last-Modified'
Updater.cb.update JSON.parse(@response).posts
Updater.set 'timer', -Updater.getInterval()
else
Updater.unsuccessfulFetchCount++
Updater.set 'timer', -Updater.getInterval()
if Conf['Verbose']
Updater.set 'count', @statusText
Updater.count.className = 'warning'
delete Updater.request
update: (posts) ->
if spoilerRange = posts[0].custom_spoiler
Build.spoilerRange[g.BOARD] = spoilerRange
lastPost = Updater.thread.lastElementChild
id = +lastPost.id[2..]
nodes = []
for post in posts.reverse()
break if post.no <= id # Make sure to not insert older posts.
nodes.push Build.postFromObject post, g.BOARD
count = nodes.length
if Conf['Verbose']
Updater.set 'count', "+#{count}"
Updater.count.className = if count then 'new' else null
if count
if Conf['Beep'] and d.hidden and (Unread.replies.length is 0)
Updater.audio.play()
Updater.unsuccessfulFetchCount = 0
else
Updater.unsuccessfulFetchCount++
return
scroll = Conf['Scrolling'] and Updater.scrollBG() and
lastPost.getBoundingClientRect().bottom - d.documentElement.clientHeight < 25
$.add Updater.thread, nodes.reverse()
if scroll
nodes[0].scrollIntoView()
set: (name, text) ->
el = Updater[name]
if node = el.firstChild
# Prevent the creation of a new DOM Node
# by setting the text node's data.
node.data = text
else
el.textContent = text
getInterval: ->
i = +Conf['Interval']
j = Math.min @unsuccessfulFetchCount, 9
unless d.hidden
# Don't increase the refresh rate too much on visible tabs.
j = Math.min j, 6
Math.max i, [5, 10, 15, 20, 30, 60, 90, 120, 240, 300][j]
timeout: ->
Updater.timeoutID = setTimeout Updater.timeout, 1000
n = 1 + Number Updater.timer.firstChild.data
if n is 0
Updater.update()
else if n >= Updater.getInterval()
Updater.unsuccessfulFetchCount++
Updater.set 'count', 'Retry'
Updater.count.className = null
Updater.update()
else
Updater.set 'timer', n
update: ->
Updater.set 'timer', 0
{request} = Updater
if request
# Don't reset the counter when aborting.
request.onloadend = null
request.abort()
url = "//api.4chan.org/#{g.BOARD}/res/#{g.THREAD_ID}.json"
Updater.request = $.ajax url, onloadend: Updater.cb.load,
headers: 'If-Modified-Since': Updater.lastModified
Watcher =
init: ->
html = '
Thread Watcher
'
@dialog = UI.dialog 'watcher', 'top: 50px; left: 0px;', html
$.add d.body, @dialog
#add watch buttons
for input in $$ '.op input'
favicon = $.el 'img',
className: 'favicon'
$.on favicon, 'click', @cb.toggle
$.before input, favicon
if g.THREAD_ID is $.get 'autoWatch', 0
@watch g.THREAD_ID
$.delete 'autoWatch'
else
#populate watcher, display watch buttons
@refresh()
$.on d, 'QRPostSuccessful', @cb.post
$.sync 'watched', @refresh
refresh: (watched) ->
watched or= $.get 'watched', {}
nodes = []
for board of watched
for id, props of watched[board]
x = $.el 'a',
textContent: '×'
href: 'javascript:;'
$.on x, 'click', Watcher.cb.x
link = $.el 'a', props
link.title = link.textContent
div = $.el 'div'
$.add div, [x, $.tn(' '), link]
nodes.push div
for div in $$ 'div:not(.move)', Watcher.dialog
$.rm div
$.add Watcher.dialog, nodes
watchedBoard = watched[g.BOARD] or {}
for favicon in $$ '.favicon'
id = favicon.nextSibling.name
if id of watchedBoard
favicon.src = Favicon.default
else
favicon.src = Favicon.empty
return
cb:
toggle: ->
Watcher.toggle @parentNode
x: ->
thread = @nextElementSibling.pathname.split '/'
Watcher.unwatch thread[3], thread[1]
post: (e) ->
{postID, threadID} = e.detail
if threadID is '0'
if Conf['Auto Watch']
$.set 'autoWatch', postID
else if Conf['Auto Watch Reply']
Watcher.watch threadID
toggle: (thread) ->
id = $('.favicon + input', thread).name
Watcher.watch(id) or Watcher.unwatch id, g.BOARD
unwatch: (id, board) ->
watched = $.get 'watched', {}
delete watched[board][id]
$.set 'watched', watched
Watcher.refresh()
watch: (id) ->
thread = $.id "t#{id}"
return false if $('.favicon', thread).src is Favicon.default
watched = $.get 'watched', {}
watched[g.BOARD] or= {}
watched[g.BOARD][id] =
href: "/#{g.BOARD}/res/#{id}"
textContent: Get.title thread
$.set 'watched', watched
Watcher.refresh()
true
Anonymize =
init: ->
Main.callbacks.push @node
node: (post) ->
return if post.isInlined and not post.isCrosspost
name = $ '.postInfo .name', post.el
name.textContent = 'Anonymous'
if (trip = name.nextElementSibling) and trip.className is 'postertrip'
$.rm trip
if (parent = name.parentNode).className is 'useremail' and not /^mailto:sage$/i.test parent.href
$.replace parent, name
Sauce =
init: ->
return if g.BOARD is 'f'
@links = []
for link in Conf['sauces'].split '\n'
continue if link[0] is '#'
# XXX .trim() is there to fix Opera reading two different line breaks.
@links.push @createSauceLink link.trim()
return unless @links.length
Main.callbacks.push @node
createSauceLink: (link) ->
link = link.replace /(\$\d)/g, (parameter) ->
switch parameter
when '$1'
"' + (isArchived ? img.firstChild.src : 'http://thumbs.4chan.org' + img.pathname.replace(/src(\\/\\d+).+$/, 'thumb$1s.jpg')) + '"
when '$2'
"' + img.href + '"
when '$3'
"' + encodeURIComponent(img.firstChild.dataset.md5) + '"
when '$4'
g.BOARD
else
parameter
domain = if m = link.match(/;text:(.+)$/) then m[1] else link.match(/(\w+)\.\w+\//)[1]
href = link.replace /;text:.+$/, ''
href = Function 'img', 'isArchived', "return '#{href}'"
el = $.el 'a',
target: '_blank'
textContent: domain
(img, isArchived) ->
a = el.cloneNode true
a.href = href img, isArchived
a
node: (post) ->
{img} = post
return if post.isInlined and not post.isCrosspost or not img
img = img.parentNode
nodes = []
for link in Sauce.links
# \u00A0 is nbsp
nodes.push $.tn('\u00A0'), link img, post.isArchived
$.add post.fileInfo, nodes
RevealSpoilers =
init: ->
Main.callbacks.push @node
node: (post) ->
{img} = post
if not (img and /^Spoiler/.test img.alt) or post.isInlined and not post.isCrosspost or post.isArchived
return
img.removeAttribute 'style'
# revealed spoilers do not have height/width set, this fixes auto-gifs dimensions.
s = img.style
s.maxHeight = s.maxWidth = if /\bop\b/.test post.class then '250px' else '125px'
img.src = "//thumbs.4chan.org#{img.parentNode.pathname.replace /src(\/\d+).+$/, 'thumb$1s.jpg'}"
Time =
init: ->
Time.foo()
Main.callbacks.push @node
node: (post) ->
return if post.isInlined and not post.isCrosspost
node = $ '.postInfo > .dateTime', post.el
Time.date = new Date node.dataset.utc * 1000
node.textContent = Time.funk Time
foo: ->
code = Conf['time'].replace /%([A-Za-z])/g, (s, c) ->
if c of Time.formatters
"' + Time.formatters.#{c}() + '"
else
s
Time.funk = Function 'Time', "return '#{code}'"
day: [
'Sunday'
'Monday'
'Tuesday'
'Wednesday'
'Thursday'
'Friday'
'Saturday'
]
month: [
'January'
'February'
'March'
'April'
'May'
'June'
'July'
'August'
'September'
'October'
'November'
'December'
]
zeroPad: (n) -> if n < 10 then '0' + n else n
formatters:
a: -> Time.day[Time.date.getDay()][...3]
A: -> Time.day[Time.date.getDay()]
b: -> Time.month[Time.date.getMonth()][...3]
B: -> Time.month[Time.date.getMonth()]
d: -> Time.zeroPad Time.date.getDate()
e: -> Time.date.getDate()
H: -> Time.zeroPad Time.date.getHours()
I: -> Time.zeroPad Time.date.getHours() % 12 or 12
k: -> Time.date.getHours()
l: -> Time.date.getHours() % 12 or 12
m: -> Time.zeroPad Time.date.getMonth() + 1
M: -> Time.zeroPad Time.date.getMinutes()
p: -> if Time.date.getHours() < 12 then 'AM' else 'PM'
P: -> if Time.date.getHours() < 12 then 'am' else 'pm'
S: -> Time.zeroPad Time.date.getSeconds()
y: -> Time.date.getFullYear() - 2000
RelativeDates =
INTERVAL: $.MINUTE
init: ->
Main.callbacks.push @node
# flush when page becomes visible again
$.on d, 'visibilitychange', @flush
node: (post) ->
dateEl = $ '.postInfo > .dateTime', post.el
# Show original absolute time as tooltip so users can still know exact times
# Since "Time Formatting" runs `node` before us, the title tooltip will
# pick up the user-formatted time instead of 4chan time when enabled.
dateEl.title = dateEl.textContent
# convert data-utc to milliseconds
utc = dateEl.dataset.utc * 1000
diff = Date.now() - utc
dateEl.textContent = RelativeDates.relative diff
RelativeDates.setUpdate dateEl, utc, diff
# Main calls @node whenever a DOM node is added (update, inline post,
# whatever), so use also this reflow opportunity to flush any other dates
# flush is debounced, so this won't burn too much cpu
RelativeDates.flush()
# diff is milliseconds from now
relative: (diff) ->
unit = if (number = (diff / $.DAY)) > 1
'day'
else if (number = (diff / $.HOUR)) > 1
'hour'
else if (number = (diff / $.MINUTE)) > 1
'minute'
else
number = diff / $.SECOND
'second'
rounded = Math.round number
unit += 's' if rounded isnt 1 # pluralize
"#{rounded} #{unit} ago"
# changing all relative dates as soon as possible incurs many annoying
# redraws and scroll stuttering. Thus, sacrifice accuracy for UX/CPU economy,
# and perform redraws when the DOM is otherwise being manipulated (and scroll
# stuttering won't be noticed), falling back to INTERVAL while the page
# is visible.
#
# each individual dateTime element will add its update() function to the stale list
# when it to be called.
stale: []
flush: $.debounce($.SECOND, ->
# no point in changing the dates until the user sees them
return if d.hidden
now = Date.now()
update now for update in RelativeDates.stale
RelativeDates.stale = []
# reset automatic flush
clearTimeout RelativeDates.timeout
RelativeDates.timeout = setTimeout RelativeDates.flush, RelativeDates.INTERVAL)
# create function `update()`, closed over dateEl and diff, that, when called
# from `flush()`, updates the element, and re-calls `setOwnTimeout()` to
# re-add `update()` to the stale list later.
setUpdate: (dateEl, utc, diff) ->
setOwnTimeout = (diff) ->
delay = if diff < $.MINUTE
$.SECOND - (diff + $.SECOND / 2) % $.SECOND
else if diff < $.HOUR
$.MINUTE - (diff + $.MINUTE / 2) % $.MINUTE
else
$.HOUR - (diff + $.HOUR / 2) % $.HOUR
setTimeout markStale, delay
update = (now) ->
if d.contains dateEl # not removed from DOM
diff = now - utc
dateEl.textContent = RelativeDates.relative diff
setOwnTimeout diff
markStale = -> RelativeDates.stale.push update
# kick off initial timeout with current diff
setOwnTimeout diff
FileInfo =
init: ->
return if g.BOARD is 'f'
@setFormats()
Main.callbacks.push @node
node: (post) ->
return if post.isInlined and not post.isCrosspost or not post.fileInfo
node = post.fileInfo.firstElementChild
alt = post.img.alt
filename = $('span', node)?.title or node.title
FileInfo.data =
link: post.img.parentNode.href
spoiler: /^Spoiler/.test alt
size: alt.match(/\d+\.?\d*/)[0]
unit: alt.match(/\w+$/)[0]
resolution: node.textContent.match(/\d+x\d+|PDF/)[0]
fullname: filename
shortname: Build.shortFilename filename, post.ID is post.threadID
# XXX GM/Scriptish
node.setAttribute 'data-filename', filename
node.innerHTML = FileInfo.funk FileInfo
setFormats: ->
code = Conf['fileInfo'].replace /%(.)/g, (s, c) ->
if c of FileInfo.formatters
"' + f.formatters.#{c}() + '"
else
s
@funk = Function 'f', "return '#{code}'"
convertUnit: (unitT) ->
size = @data.size
unitF = @data.unit
if unitF isnt unitT
units = ['B', 'KB', 'MB']
i = units.indexOf(unitF) - units.indexOf unitT
unitT = 'Bytes' if unitT is 'B'
if i > 0
size *= 1024 while i-- > 0
else if i < 0
size /= 1024 while i++ < 0
if size < 1 and size.toString().length > size.toFixed(2).length
size = size.toFixed 2
"#{size} #{unitT}"
formatters:
t: -> FileInfo.data.link.match(/\d+\..+$/)[0]
T: -> "#{@t()}"
l: -> "#{@n()}"
L: -> "#{@N()}"
n: ->
if FileInfo.data.fullname is FileInfo.data.shortname
FileInfo.data.fullname
else
"#{FileInfo.data.shortname}#{FileInfo.data.fullname}"
N: -> FileInfo.data.fullname
p: -> if FileInfo.data.spoiler then 'Spoiler, ' else ''
s: -> "#{FileInfo.data.size} #{FileInfo.data.unit}"
B: -> FileInfo.convertUnit 'B'
K: -> FileInfo.convertUnit 'KB'
M: -> FileInfo.convertUnit 'MB'
r: -> FileInfo.data.resolution
Get =
post: (board, threadID, postID, root, cb) ->
if board is g.BOARD and post = $.id "pc#{postID}"
$.add root, Get.cleanPost post.cloneNode true
return
root.textContent = "Loading post No.#{postID}..."
if threadID
$.cache "//api.4chan.org/#{board}/res/#{threadID}.json", ->
Get.parsePost @, board, threadID, postID, root, cb
else if url = Redirect.post board, postID
$.cache url, ->
Get.parseArchivedPost @, board, postID, root, cb
parsePost: (req, board, threadID, postID, root, cb) ->
{status} = req
if status isnt 200
# The thread can die by the time we check a quote.
if url = Redirect.post board, postID
$.cache url, ->
Get.parseArchivedPost @, board, postID, root, cb
else
$.addClass root, 'warning'
root.textContent =
if status is 404
"Thread No.#{threadID} 404'd."
else
"Error #{req.status}: #{req.statusText}."
return
posts = JSON.parse(req.response).posts
if spoilerRange = posts[0].custom_spoiler
Build.spoilerRange[board] = spoilerRange
postID = +postID
for post in posts
break if post.no is postID # we found it!
if post.no > postID
# The post can be deleted by the time we check a quote.
if url = Redirect.post board, postID
$.cache url, ->
Get.parseArchivedPost @, board, postID, root, cb
else
$.addClass root, 'warning'
root.textContent = "Post No.#{postID} was not found."
return
$.replace root.firstChild, Get.cleanPost Build.postFromObject post, board
cb() if cb
parseArchivedPost: (req, board, postID, root, cb) ->
data = JSON.parse req.response
if data.error
$.addClass root, 'warning'
root.textContent = data.error
return
# convert comment to html
bq = $.el 'blockquote', textContent: data.comment # set this first to convert text to HTML entities
# https://github.com/eksopl/fuuka/blob/master/Board/Yotsuba.pm#L413-452
# https://github.com/eksopl/asagi/blob/master/src/main/java/net/easymodo/asagi/Yotsuba.java#L109-138
bq.innerHTML = bq.innerHTML.replace ///
\n
| \[/?b\]
| \[/?spoiler\]
| \[/?code\]
| \[/?moot\]
| \[/?banned\]
///g, (text) ->
switch text
when '\n'
' '
when '[b]'
''
when '[/b]'
''
when '[spoiler]'
''
when '[/spoiler]'
''
when '[code]'
'
'
when '[/code]'
'
'
when '[moot]'
'
'
when '[/moot]'
'
'
when '[banned]'
''
when '[/banned]'
''
# greentext
comment = bq.innerHTML.replace /(^|>)(>[^<$]*)(<|$)/g, '$1$2$3'
# quotes
comment = comment.replace /((>){2}(>\/[a-z\d]+\/)?\d+)/g, '$1'
o =
# id
postID: postID
threadID: data.thread_num
board: board
# info
name: data.name_processed
capcode: switch data.capcode
when 'M' then 'mod'
when 'A' then 'admin'
when 'D' then 'developer'
tripcode: data.trip
uniqueID: data.poster_hash
email: if data.email then encodeURI data.email else ''
subject: data.title_processed
flagCode: data.poster_country
flagName: data.poster_country_name_processed
date: data.fourchan_date
dateUTC: data.timestamp
comment: comment
# file
if data.media?.media_filename
o.file =
name: data.media.media_filename_processed
timestamp: data.media.media_orig
url: data.media.media_link or data.media.remote_media_link
height: data.media.media_h
width: data.media.media_w
MD5: data.media.media_hash
size: data.media.media_size
turl: data.media.thumb_link or "//thumbs.4chan.org/#{board}/thumb/#{data.media.preview_orig}"
theight: data.media.preview_h
twidth: data.media.preview_w
isSpoiler: data.media.spoiler is '1'
$.replace root.firstChild, Get.cleanPost Build.post o, true
cb() if cb
cleanPost: (root) ->
post = $ '.post', root
for child in Array::slice.call root.childNodes
$.rm child unless child is post
# Remove inlined posts inside of this post.
for inline in $$ '.inline', post
$.rm inline
for inlined in $$ '.inlined', post
$.rmClass inlined, 'inlined'
# Don't mess with other features
now = Date.now()
els = $$ '[id]', root
els.push root
for el in els
el.id = "#{now}_#{el.id}"
$.rmClass root, 'forwarded'
$.rmClass root, 'qphl' # op
$.rmClass post, 'highlight'
$.rmClass post, 'qphl' # reply
root.hidden = post.hidden = false
root
title: (thread) ->
op = $ '.op', thread
el = $ '.postInfo .subject', op
unless el.textContent
el = $ 'blockquote', op
unless el.textContent
el = $ '.nameBlock', op
span = $.el 'span', innerHTML: el.innerHTML.replace / /g, ' '
"/#{g.BOARD}/ - #{span.textContent.trim()}"
Build =
spoilerRange: {}
shortFilename: (filename, isOP) ->
# FILENAME SHORTENING SCIENCE:
# OPs have a +10 characters threshold.
# The file extension is not taken into account.
threshold = if isOP then 40 else 30
if filename.length - 4 > threshold
"#{filename[...threshold - 5]}(...).#{filename[-3..]}"
else
filename
postFromObject: (data, board) ->
o =
# id
postID: data.no
threadID: data.resto or data.no
board: board
# info
name: data.name
capcode: data.capcode
tripcode: data.trip
uniqueID: data.id
email: if data.email then encodeURI data.email.replace /"/g, '"' else ''
subject: data.sub
flagCode: data.country
flagName: data.country_name
date: data.now
dateUTC: data.time
comment: data.com
# thread status
isSticky: !!data.sticky
isClosed: !!data.closed
# file
if data.ext or data.filedeleted
o.file =
name: data.filename + data.ext
timestamp: "#{data.tim}#{data.ext}"
url: "//images.4chan.org/#{board}/src/#{data.tim}#{data.ext}"
height: data.h
width: data.w
MD5: data.md5
size: data.fsize
turl: "//thumbs.4chan.org/#{board}/thumb/#{data.tim}s.jpg"
theight: data.tn_h
twidth: data.tn_w
isSpoiler: !!data.spoiler
isDeleted: !!data.filedeleted
Build.post o
post: (o, isArchived) ->
###
This function contains code from 4chan-JS (https://github.com/4chan/4chan-JS).
@license: https://github.com/4chan/4chan-JS/blob/master/LICENSE
###
{
postID, threadID, board
name, capcode, tripcode, uniqueID, email, subject, flagCode, flagName, date, dateUTC
isSticky, isClosed
comment
file
} = o
isOP = postID is threadID
staticPath = '//static.4chan.org'
if email
emailStart = ''
emailEnd = ''
else
emailStart = ''
emailEnd = ''
subject = "#{subject or ''}"
userID =
if !capcode and uniqueID
" (ID: " +
"#{uniqueID}) "
else
''
switch capcode
when 'admin', 'admin_highlight'
capcodeClass = " capcodeAdmin"
capcodeStart = " ## Admin"
capcode = " "
when 'mod'
capcodeClass = " capcodeMod"
capcodeStart = " ## Mod"
capcode = " "
when 'developer'
capcodeClass = " capcodeDeveloper"
capcodeStart = " ## Developer"
capcode = " "
else
capcodeClass = ''
capcodeStart = ''
capcode = ''
flag =
if flagCode
" "
else
''
if file?.isDeleted
fileHTML =
if isOP
"
" +
"" +
"
"
else
"
" +
"" +
"
"
else if file
ext = file.name[-3..]
if !file.twidth and !file.theight and ext is 'gif' # wtf ?
file.twidth = file.width
file.theight = file.height
fileSize = $.bytesToString file.size
fileThumb = file.turl
if file.isSpoiler
fileSize = "Spoiler Image, #{fileSize}"
unless isArchived
fileThumb = '//static.4chan.org/image/spoiler'
if spoilerRange = Build.spoilerRange[board]
# Randomize the spoiler image.
fileThumb += "-#{board}" + Math.floor 1 + spoilerRange * Math.random()
fileThumb += '.png'
file.twidth = file.theight = 100
imgSrc = "" +
""
# Ha Ha filenames.
# html -> text, translate WebKit's %22s into "s
a = $.el 'a', innerHTML: file.name
filename = a.textContent.replace /%22/g, '"'
# shorten filename, get html
a.textContent = Build.shortFilename filename
shortFilename = a.innerHTML
# get html
a.textContent = filename
filename = a.innerHTML.replace /'/g, '''
fileDims = if ext is 'pdf' then 'PDF' else "#{file.width}x#{file.height}"
fileInfo = "File: #{file.timestamp}" +
"-(#{fileSize}, #{fileDims}#{
if file.isSpoiler
''
else
", #{shortFilename}"
}" + ")"
fileHTML = "