diff --git a/src/main/Main.coffee b/src/main/Main.coffee index 812513430..7cdb7129e 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -123,11 +123,30 @@ Main = <%= html(meta.name + ' has been updated to version ${g.VERSION}.') %> new Notice 'info', el, 15 - initFeatures: -> - {hostname, search} = location - pathname = location.pathname.split /\/+/ - g.BOARD = new Board pathname[1] unless hostname in ['www.4chan.org', 'www.4channel.org'] + parseURL: (site=g.SITE, url=location) -> + r = {} + return r if !site + r.siteID = site.ID + + return r if site.isBoardlessPage?(url) + pathname = url.pathname.split /\/+/ + r.boardID = pathname[1] + + if site.isFileURL(url) + r.VIEW = 'file' + else if site.isAuxiliaryPage?(url) + # pass + else if pathname[2] in ['thread', 'res'] + r.VIEW = 'thread' + r.threadID = r.THREADID = +pathname[3].replace(/\.\w+$/, '') + else if /^(?:catalog|archive)(?:\.\w+)?$/.test(pathname[2]) + r.VIEW = pathname[2].replace(/\.\w+$/, '') + else if /^(?:index|\d*)(?:\.\w+)?$/.test(pathname[2]) + r.VIEW = 'index' + r + + initFeatures: -> $.global -> document.documentElement.classList.add 'js-enabled' window.FCX = {} @@ -136,29 +155,17 @@ Main = # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638 $.ajaxPageInit?() - switch hostname - when 'www.4chan.org', 'www.4channel.org' - $.onExists doc, 'body', -> $.addStyle CSS.www - Captcha.replace.init() - return - when 'sys.4chan.org', 'sys.4channel.org' - if pathname[2] is 'imgboard.php' - if /\bmode=report\b/.test search - Report.init() - else if (match = search.match /\bres=(\d+)/) - $.ready -> - if Conf['404 Redirect'] and $.id('errmsg')?.textContent is 'Error: Specified thread does not exist.' - Redirect.navigate 'thread', { - boardID: g.BOARD.ID - postID: +match[1] - } - else if pathname[2] is 'post' - PostSuccessful.init() - return + $.extend g, Main.parseURL() + g.BOARD = new Board g.boardID if g.boardID - if g.SITE.isFileURL() + if !g.VIEW + g.SITE.initAuxiliary?() + return + + if g.VIEW is 'file' $.asap (-> d.readyState isnt 'loading'), -> if g.SITE.software is 'yotsuba' and Conf['404 Redirect'] and g.SITE.is404?() + pathname = location.pathname.split /\/+/ Redirect.navigate 'file', { boardID: g.BOARD.ID filename: pathname[pathname.length - 1] @@ -173,18 +180,6 @@ Main = ImageCommon.addControls video return - return if g.SITE.isAuxiliaryPage?() - - if pathname[2] in ['thread', 'res'] - g.VIEW = 'thread' - g.THREADID = +pathname[3].replace(/\.\w+$/, '') - else if /^(?:catalog|archive)(?:\.\w+)?$/.test(pathname[2]) - g.VIEW = pathname[2].replace(/\.\w+$/, '') - else if /^(?:index|\d*)(?:\.\w+)?$/.test(pathname[2]) - g.VIEW = 'index' - else - return - g.threads = new SimpleDict() g.posts = new SimpleDict() diff --git a/src/site/SW.tinyboard.coffee b/src/site/SW.tinyboard.coffee index b848e7bf2..318d7f207 100644 --- a/src/site/SW.tinyboard.coffee +++ b/src/site/SW.tinyboard.coffee @@ -154,8 +154,8 @@ SW.tinyboard = bgColoredEl: -> $.el 'div', className: 'post reply' - isFileURL: -> - /\/src\/[^\/]+/.test(location.pathname) + isFileURL: (url) -> + /\/src\/[^\/]+/.test(url.pathname) parseNodes: (post, nodes) -> # Add vichan's span.poster_id around the ID if not already present. diff --git a/src/site/SW.yotsuba.coffee b/src/site/SW.yotsuba.coffee index ea28a78ac..d56ea7f08 100644 --- a/src/site/SW.yotsuba.coffee +++ b/src/site/SW.yotsuba.coffee @@ -106,11 +106,36 @@ SW.yotsuba = isIncomplete: -> return g.VIEW in ['index', 'thread'] and not $('.board + *') - isAuxiliaryPage: -> - location.hostname not in ['boards.4chan.org', 'boards.4channel.org'] + isBoardlessPage: (url) -> + url.hostname in ['www.4chan.org', 'www.4channel.org'] - isFileURL: -> - ImageHost.test(location.hostname) + isAuxiliaryPage: (url) -> + url.hostname not in ['boards.4chan.org', 'boards.4channel.org'] + + isFileURL: (url) -> + ImageHost.test(url.hostname) + + initAuxiliary: -> + switch location.hostname + when 'www.4chan.org', 'www.4channel.org' + $.onExists doc, 'body', -> $.addStyle CSS.www + Captcha.replace.init() + return + when 'sys.4chan.org', 'sys.4channel.org' + pathname = location.pathname.split /\/+/ + if pathname[2] is 'imgboard.php' + if /\bmode=report\b/.test location.search + Report.init() + else if (match = location.search.match /\bres=(\d+)/) + $.ready -> + if Conf['404 Redirect'] and $.id('errmsg')?.textContent is 'Error: Specified thread does not exist.' + Redirect.navigate 'thread', { + boardID: g.BOARD.ID + postID: +match[1] + } + else if pathname[2] is 'post' + PostSuccessful.init() + return scriptData: -> for script in $$ 'script:not([src])', d.head diff --git a/src/site/Site.coffee b/src/site/Site.coffee index 33ceaff0e..317f8bed9 100644 --- a/src/site/Site.coffee +++ b/src/site/Site.coffee @@ -6,14 +6,10 @@ Site = init: (cb) -> $.extend Conf['siteProperties'], Site.defaultProperties - {hostname} = location - while hostname and hostname not of Conf['siteProperties'] - hostname = hostname.replace(/^[^.]*\.?/, '') - if hostname - hostname = canonical if (canonical = Conf['siteProperties'][hostname].canonical) - if Conf['siteProperties'][hostname].software of SW - @set hostname - cb() + hostname = Site.resolve() + if hostname and Conf['siteProperties'][hostname].software of SW + @set hostname + cb() $.onExists doc, 'body', => for software of SW when (changes = SW[software].detect?()) changes.software = software @@ -31,6 +27,18 @@ Site = return return + resolve: (url=location) -> + {hostname} = url + while hostname and hostname not of Conf['siteProperties'] + hostname = hostname.replace(/^[^.]*\.?/, '') + if hostname + hostname = canonical if (canonical = Conf['siteProperties'][hostname].canonical) + hostname + + parseURL: (url) -> + siteID = Site.resolve url + Main.parseURL g.sites[siteID], url + set: (hostname) -> for ID, properties of Conf['siteProperties'] continue if properties.canonical