Clean up some spaghetti in Main.coffee.

Also be more careful about pages we shouldn't run on
(e.g. robots.txt, sitemap.xml, ping.js). #574
This commit is contained in:
ccd0 2015-11-07 20:37:46 -08:00
parent 820192b653
commit dc7bbb6821
2 changed files with 39 additions and 38 deletions

View File

@ -15,22 +15,6 @@ Main =
$.ready -> Captcha.fixes.init() $.ready -> Captcha.fixes.init()
return return
g.threads = new SimpleDict()
g.posts = new SimpleDict()
pathname = location.pathname.split /\/+/
g.BOARD = new Board pathname[1]
g.VIEW =
switch pathname[2]
when 'res', 'thread'
'thread'
when 'catalog', 'archive', 'post'
pathname[2]
else
'index'
if g.VIEW is 'thread'
g.THREADID = +pathname[3]
# Flatten default values from Config into Conf # Flatten default values from Config into Conf
flatten = (parent, obj) -> flatten = (parent, obj) ->
if obj instanceof Array if obj instanceof Array
@ -75,10 +59,6 @@ Main =
Main.initFeatures() Main.initFeatures()
# set up CSS when <head> is completely loaded
$.asap (-> doc = d.documentElement), ->
$.onExists doc, 'body', false, Main.initStyle
upgrade: (items) -> upgrade: (items) ->
{previousversion} = items {previousversion} = items
changes = {previousversion: g.VERSION} changes = {previousversion: g.VERSION}
@ -91,31 +71,39 @@ Main =
new Notice 'info', el, 15 new Notice 'info', el, 15
initFeatures: -> initFeatures: ->
if location.hostname in ['boards.4chan.org', 'sys.4chan.org', 'www.4chan.org'] {hostname, search} = location
pathname = location.pathname.split /\/+/
g.BOARD = new Board pathname[1] unless hostname is 'www.4chan.org'
if hostname in ['boards.4chan.org', 'sys.4chan.org', 'www.4chan.org']
$.global -> $.global ->
document.documentElement.classList.add 'js-enabled' document.documentElement.classList.add 'js-enabled'
window.FCX = {} window.FCX = {}
switch location.hostname switch hostname
when 'www.4chan.org' when 'www.4chan.org'
$.onExists doc, 'body', false, -> $.addStyle Main.cssWWW
Captcha.replace.init() Captcha.replace.init()
return return
when 'a.4cdn.org' when 'a.4cdn.org'
return return
when 'sys.4chan.org' when 'sys.4chan.org'
Report.init() if pathname[2] is 'imgboard.php'
PostSuccessful.init() if g.VIEW is 'post' if /\bmode=report\b/.test search
if Conf['404 Redirect'] and /\/imgboard\.php$/.test(location.pathname) and (match = location.search.match /\bres=(\d+)/) Report.init()
$.ready -> else if (match = search.match /\bres=(\d+)/)
if $.id('errmsg')?.textContent is 'Error: Specified thread does not exist.' $.ready ->
Redirect.navigate 'thread', if Conf['404 Redirect'] and $.id('errmsg')?.textContent is 'Error: Specified thread does not exist.'
boardID: g.BOARD.ID Redirect.navigate 'thread',
postID: +match[1] boardID: g.BOARD.ID
postID: +match[1]
else if pathname[2] is 'post'
PostSuccessful.init()
return return
when 'i.4cdn.org' when 'i.4cdn.org'
return unless pathname[2] and not /s\.jpg$/.test(pathname[2])
$.asap (-> d.readyState isnt 'loading'), -> $.asap (-> d.readyState isnt 'loading'), ->
if Conf['404 Redirect'] and d.title in ['4chan - Temporarily Offline', '4chan - 404 Not Found'] if Conf['404 Redirect'] and d.title in ['4chan - Temporarily Offline', '4chan - 404 Not Found']
pathname = location.pathname.split /\/+/
Redirect.navigate 'file', Redirect.navigate 'file',
boardID: g.BOARD.ID boardID: g.BOARD.ID
filename: pathname[pathname.length - 1] filename: pathname[pathname.length - 1]
@ -129,17 +117,32 @@ Main =
ImageCommon.addControls video ImageCommon.addControls video
return return
if pathname[2] in ['thread', 'res']
g.VIEW = 'thread'
g.THREADID = +pathname[3]
else if pathname[2] in ['catalog', 'archive']
g.VIEW = pathname[2]
else if pathname[2].match /^\d*$/
g.VIEW = 'index'
else
return
g.threads = new SimpleDict()
g.posts = new SimpleDict()
# set up CSS when <head> is completely loaded
$.onExists doc, 'body', false, Main.initStyle
if Conf['Normalize URL'] if Conf['Normalize URL']
pathname = location.pathname.split /\/+/
switch g.VIEW switch g.VIEW
when 'thread' when 'thread'
pathname[2] = 'thread' pathname[2] = 'thread'
pathname = pathname[0...4] pathname = pathname[0...4]
when 'index' when 'index'
pathname = pathname[0...3] pathname = pathname[0...3]
pathname = pathname.join '/' pathname2 = pathname.join '/'
if location.pathname isnt pathname if location.pathname isnt pathname2
history.replaceState history.state, '', "#{location.protocol}//#{location.host}#{pathname}#{location.hash}" history.replaceState history.state, '', "#{location.protocol}//#{location.host}#{pathname2}#{location.hash}"
# c.time 'All initializations' # c.time 'All initializations'
for [name, feature] in Main.features for [name, feature] in Main.features
@ -158,8 +161,6 @@ Main =
$.ready Main.initReady $.ready Main.initReady
initStyle: -> initStyle: ->
$.addStyle Main.cssWWW if location.hostname is 'www.4chan.org'
return if !Main.isThisPageLegit() or $.hasClass doc, 'fourchan-x' return if !Main.isThisPageLegit() or $.hasClass doc, 'fourchan-x'
# disable the mobile layout # disable the mobile layout

View File

@ -2,7 +2,7 @@ Report =
css: `<%= importCSS('report') %>` css: `<%= importCSS('report') %>`
init: -> init: ->
return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ return unless (match = location.search.match /\bno=(\d+)/)
Captcha.replace.init() Captcha.replace.init()
@postID = +match[1] @postID = +match[1]
$.ready @ready $.ready @ready