Skip to content

Instantly share code, notes, and snippets.

@zixaphir
Created October 20, 2013 00:49
Show Gist options
  • Save zixaphir/7063443 to your computer and use it in GitHub Desktop.
Save zixaphir/7063443 to your computer and use it in GitHub Desktop.
4chan X Infinite Scrolling
InfiniScroll =
init: ->
return unless Conf['Infinite Scrolling'] and g.VIEW is 'index' and g.BOARD isnt 'f'
@threads = g.threads
$.on d, '4chanXInitFinished', @ready
ready: ->
$.off d, '4chanXInitFinished', InfiniScroll.ready
$.on d, 'scroll', InfiniScroll.scroll
scroll: ->
return if InfiniScroll.isFetching or (doc.scrollTop <= doc.scrollHeight - (500 + window.innerHeight))
# For once, lets respect 4chan's API rules.
if InfiniScroll.cache and InfiniScroll.cache.time < Date.now() - $.MINUTE
return InfiniScroll.parse JSON.parse InfiniScroll.cache
InfiniScroll.isFetching = true
url = "//api.4chan.org/#{g.BOARD}/catalog.json"
$.ajax url, onloadend: InfiniScroll.cb.load,
whenModified: true
parse: (response) ->
threads = InfiniScroll.parsePages response
threadNodes = []
nodes = []
for thread in threads
posts = []
{omitted_posts, omitted_images} = thread
threadID = thread.no
el = $.el 'div',
className: 'thread'
id: "t#{threadID}"
op = Build.postFromObject thread, g.BOARD
posts.push op
if omitted_posts
posts.push $.el 'span',
className: 'summary desktop'
innerHTML: """
#{omitted_posts} posts #{if omitted_images then "and " + omitted_images + " image replies"} omitted. Click <a class="replylink" href="res/#{threadID}">here</a> to view.
"""
$.add op, $.el 'div',
className: "postLink mobile"
innerHTML: """
<span class="info"><strong>#{omitted_posts} posts omitted</strong>#{if omitted_images then "<br><em>(#{omitted_images} have images)</em></span>" else ""}<a href="res/#{threadID}" class="button">View Thread</a>
"""
if thread.last_replies then posts.push Build.postFromObject post, g.BOARD for post in thread.last_replies
$.add el, posts
threadNodes.push el
nodes.push el
nodes.push $.el 'hr'
InfiniScroll.features threadNodes
$.before botPostForm, nodes if botPostForm = $ '.board > .mobile.center'
parsePages: (response) ->
newThreads = []
for number, page of response
{threads} = page
for thread in threads
continue if g.threads["#{g.BOARD}.#{thread.no}"]
newThreads.push thread
return newThreads if newThreads.length is 15
return newThreads
features: (threadNodes) ->
posts = []
threads = []
for threadRoot in threadNodes
thread = new Thread +threadRoot.id[1..], g.BOARD
threads.push thread
for post in $$ '.thread > .postContainer', threadRoot
try
posts.push new Post post, thread, g.BOARD
catch err
# Skip posts that we failed to parse.
unless errors
errors = []
errors.push
message: "Parsing of Post No.#{postRoot.id.match(/\d+/)} failed. Post will be skipped."
error: err
Main.handleErrors errors if errors
Main.callbackNodes Thread, threads
Main.callbackNodes Post, posts
cb:
load: ->
InfiniScroll.isFetching = false
return unless @status is 200
InfiniScroll.cache = new String @response
InfiniScroll.cache.time = Date.now()
InfiniScroll.parse JSON.parse @response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment