From d8154e8ae2531e8b7d750cb5605da0c20e9c0d1c Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Mon, 13 Jan 2014 19:25:36 -0700 Subject: [PATCH 1/2] Lets not push states for infinite scrolling. :) --- builds/4chan-X.user.js | 24 ++++++++++-------------- builds/crx/script.js | 24 ++++++++++-------------- src/General/Index.coffee | 16 ++++++---------- src/General/Navigate.coffee | 2 ++ 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index dd01e429b..83090ae43 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -2324,18 +2324,20 @@ if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') { return; } - pageNum = Index.getCurrentPage() + 1; + if (Index.pageNum == null) { + Index.pageNum = Index.getCurrentPage(); + } + pageNum = Index.pageNum++; if (pageNum >= Index.pagesNum) { return Index.endNotice(); } nodesPerPage = Index.threadsNumPerPage * 2; - history.pushState(null, '', "/" + g.BOARD + "/" + pageNum); nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); if (Conf['Show Replies']) { Index.buildReplies(nodes); } $.add(Index.root, nodes); - return Index.setPage(); + return Index.setPage(pageNum); }), endNotice: (function() { var notify, reset; @@ -2386,14 +2388,6 @@ } e.preventDefault(); return Index.userPageNav(+a.pathname.split('/')[2]); - }, - link: function(e) { - if (g.VIEW !== 'index' || /catalog/.test(this.href)) { - return; - } - e.preventDefault(); - history.pushState(null, '', this.pathname); - return Index.update(); } }, scrollToIndex: function() { @@ -2456,9 +2450,9 @@ } return Index.togglePagelist(); }, - setPage: function() { - var a, href, maxPageNum, next, pageNum, pagesRoot, prev, strong; - pageNum = Index.getCurrentPage(); + setPage: function(pageNum) { + var a, href, maxPageNum, next, pagesRoot, prev, strong; + pageNum || (pageNum = Index.getCurrentPage()); maxPageNum = Index.getMaxPageNum(); pagesRoot = $('.pages', Index.pagelist); prev = pagesRoot.previousSibling.firstChild; @@ -2495,6 +2489,7 @@ if (!(d.readyState === 'loading' || Index.root.parentElement)) { $.replace($('.board'), Index.root); } + delete Index.pageNum; if ((_ref = Index.req) != null) { _ref.abort(); } @@ -12105,6 +12100,7 @@ if (e) { e.preventDefault(); } + delete Index.pageNum; path = this.pathname; if (this.hash) { path += this.hash; diff --git a/builds/crx/script.js b/builds/crx/script.js index 783cc5f33..2612a9ae6 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -2334,18 +2334,20 @@ if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') { return; } - pageNum = Index.getCurrentPage() + 1; + if (Index.pageNum == null) { + Index.pageNum = Index.getCurrentPage(); + } + pageNum = Index.pageNum++; if (pageNum >= Index.pagesNum) { return Index.endNotice(); } nodesPerPage = Index.threadsNumPerPage * 2; - history.pushState(null, '', "/" + g.BOARD + "/" + pageNum); nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); if (Conf['Show Replies']) { Index.buildReplies(nodes); } $.add(Index.root, nodes); - return Index.setPage(); + return Index.setPage(pageNum); }), endNotice: (function() { var notify, reset; @@ -2396,14 +2398,6 @@ } e.preventDefault(); return Index.userPageNav(+a.pathname.split('/')[2]); - }, - link: function(e) { - if (g.VIEW !== 'index' || /catalog/.test(this.href)) { - return; - } - e.preventDefault(); - history.pushState(null, '', this.pathname); - return Index.update(); } }, scrollToIndex: function() { @@ -2466,9 +2460,9 @@ } return Index.togglePagelist(); }, - setPage: function() { - var a, href, maxPageNum, next, pageNum, pagesRoot, prev, strong; - pageNum = Index.getCurrentPage(); + setPage: function(pageNum) { + var a, href, maxPageNum, next, pagesRoot, prev, strong; + pageNum || (pageNum = Index.getCurrentPage()); maxPageNum = Index.getMaxPageNum(); pagesRoot = $('.pages', Index.pagelist); prev = pagesRoot.previousSibling.firstChild; @@ -2505,6 +2499,7 @@ if (!(d.readyState === 'loading' || Index.root.parentElement)) { $.replace($('.board'), Index.root); } + delete Index.pageNum; if ((_ref = Index.req) != null) { _ref.abort(); } @@ -12094,6 +12089,7 @@ if (e) { e.preventDefault(); } + delete Index.pageNum; path = this.pathname; if (this.hash) { path += this.hash; diff --git a/src/General/Index.coffee b/src/General/Index.coffee index 43de32921..9a758b808 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -112,14 +112,14 @@ Index = scroll: $.debounce 100, -> return if Index.req or Conf['Index Mode'] isnt 'infinite' or (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) or g.VIEW is 'thread' - pageNum = Index.getCurrentPage() + 1 + Index.pageNum = Index.getCurrentPage() unless Index.pageNum? # Avoid having to pushState to keep track of the current page + pageNum = Index.pageNum++ return Index.endNotice() if pageNum >= Index.pagesNum nodesPerPage = Index.threadsNumPerPage * 2 - history.pushState null, '', "/#{g.BOARD}/#{pageNum}" nodes = Index.sortedNodes[nodesPerPage * pageNum ... nodesPerPage * (pageNum + 1)] Index.buildReplies nodes if Conf['Show Replies'] $.add Index.root, nodes - Index.setPage() + Index.setPage pageNum endNotice: do -> notify = false @@ -153,11 +153,6 @@ Index = return if a.textContent is 'Catalog' e.preventDefault() Index.userPageNav +a.pathname.split('/')[2] - link: (e) -> - return if g.VIEW isnt 'index' or /catalog/.test @href - e.preventDefault() - history.pushState null, '', @pathname - Index.update() scrollToIndex: -> Header.scrollToIfNeeded Index.root @@ -202,8 +197,8 @@ Index = $.rmAll pagesRoot $.add pagesRoot, nodes Index.togglePagelist() - setPage: -> - pageNum = Index.getCurrentPage() + setPage: (pageNum) -> + pageNum or= Index.getCurrentPage() maxPageNum = Index.getMaxPageNum() pagesRoot = $ '.pages', Index.pagelist # Previous/Next buttons @@ -232,6 +227,7 @@ Index = return unless d.readyState is 'loading' or Index.root.parentElement $.replace $('.board'), Index.root + delete Index.pageNum Index.req?.abort() Index.notice?.close() diff --git a/src/General/Navigate.coffee b/src/General/Navigate.coffee index 01b535c19..04d15302d 100644 --- a/src/General/Navigate.coffee +++ b/src/General/Navigate.coffee @@ -184,6 +184,8 @@ Navigate = return if view is 'catalog' or 'f' in [boardID, g.BOARD.ID] e.preventDefault() if e + delete Index.pageNum + path = @pathname path += @hash if @hash From bcd7ab21e39c4d38eb60f0ea475567608b01a977 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Mon, 13 Jan 2014 19:31:32 -0700 Subject: [PATCH 2/2] Make the JSON Navigation / Index features optional --- builds/4chan-X.user.js | 23 +++++++++++++++-------- builds/crx/script.js | 23 +++++++++++++++-------- src/General/Config.coffee | 4 ++++ src/General/Header.coffee | 9 ++++++--- src/General/Index.coffee | 2 +- src/General/Navigate.coffee | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 83090ae43..433af2b54 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -129,6 +129,7 @@ Config = { main: { 'Miscellaneous': { + 'JSON Navigation': [true, 'Use JSON for loading the Board Index and Threads. Also allows searching and sorting the board index and infinite scolling.'], 'Catalog Links': [true, 'Add toggle link in header menu to turn Navigation links into links to each board\'s catalog.'], 'External Catalog': [false, 'Link to external catalog instead of the internal one.'], 'QR Shortcut': [false, 'Adds a small [QR] link in the header.'], @@ -1775,10 +1776,12 @@ $.ready(function() { var a, cs, footer, _i, _len, _ref; _this.footer = footer = $.id('boardNavDesktopFoot'); - _ref = $$('a', footer); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - a = _ref[_i]; - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + _ref = $$('a', footer); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + a = _ref[_i]; + $.on(a, 'click', Navigate.navigate); + } } if (a = $("a[href*='/" + g.BOARD + "/']", footer)) { a.className = 'current'; @@ -1825,7 +1828,9 @@ _ref = $$('a', boardList); for (_i = 0, _len = _ref.length; _i < _len; _i++) { a = _ref[_i]; - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + $.on(a, 'click', Navigate.navigate); + } if (a.pathname.split('/')[1] === g.BOARD.ID) { a.className = 'current'; } @@ -1876,7 +1881,9 @@ a = as[_i]; if (a.textContent === board) { a = a.cloneNode(true); - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + $.on(a, 'click', Navigate.navigate); + } a.textContent = /-title/.test(t) || /-replace/.test(t) && $.hasClass(a, 'current') ? a.title : /-full/.test(t) ? "/" + board + "/ - " + a.title : (m = t.match(/-text:"(.+)"/)) ? m[1] : a.textContent; if (m = t.match(/-(index|catalog)/)) { a.dataset.only = m[1]; @@ -2150,7 +2157,7 @@ Index = { init: function() { var anchorEntry, input, label, modeEntry, name, refNavEntry, repliesEntry, sortEntry, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; - if (g.BOARD.ID === 'f' || g.VIEW === 'catalog') { + if (g.BOARD.ID === 'f' || g.VIEW === 'catalog' || !Conf['JSON Navigation']) { return; } this.button = $.el('a', { @@ -11877,7 +11884,7 @@ Navigate = { path: window.location.pathname, init: function() { - if (g.VIEW === 'catalog' || g.BOARD.ID === 'f') { + if (g.VIEW === 'catalog' || g.BOARD.ID === 'f' || !Conf['JSON Navigation']) { return; } $.ready(function() { diff --git a/builds/crx/script.js b/builds/crx/script.js index 2612a9ae6..eb9650742 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -107,6 +107,7 @@ Config = { main: { 'Miscellaneous': { + 'JSON Navigation': [true, 'Use JSON for loading the Board Index and Threads. Also allows searching and sorting the board index and infinite scolling.'], 'Catalog Links': [true, 'Add toggle link in header menu to turn Navigation links into links to each board\'s catalog.'], 'External Catalog': [false, 'Link to external catalog instead of the internal one.'], 'QR Shortcut': [false, 'Adds a small [QR] link in the header.'], @@ -1785,10 +1786,12 @@ $.ready(function() { var a, cs, footer, _i, _len, _ref; _this.footer = footer = $.id('boardNavDesktopFoot'); - _ref = $$('a', footer); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - a = _ref[_i]; - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + _ref = $$('a', footer); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + a = _ref[_i]; + $.on(a, 'click', Navigate.navigate); + } } if (a = $("a[href*='/" + g.BOARD + "/']", footer)) { a.className = 'current'; @@ -1835,7 +1838,9 @@ _ref = $$('a', boardList); for (_i = 0, _len = _ref.length; _i < _len; _i++) { a = _ref[_i]; - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + $.on(a, 'click', Navigate.navigate); + } if (a.pathname.split('/')[1] === g.BOARD.ID) { a.className = 'current'; } @@ -1886,7 +1891,9 @@ a = as[_i]; if (a.textContent === board) { a = a.cloneNode(true); - $.on(a, 'click', Navigate.navigate); + if (Conf['JSON Navigation']) { + $.on(a, 'click', Navigate.navigate); + } a.textContent = /-title/.test(t) || /-replace/.test(t) && $.hasClass(a, 'current') ? a.title : /-full/.test(t) ? "/" + board + "/ - " + a.title : (m = t.match(/-text:"(.+)"/)) ? m[1] : a.textContent; if (m = t.match(/-(index|catalog)/)) { a.dataset.only = m[1]; @@ -2160,7 +2167,7 @@ Index = { init: function() { var anchorEntry, input, label, modeEntry, name, refNavEntry, repliesEntry, sortEntry, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; - if (g.BOARD.ID === 'f' || g.VIEW === 'catalog') { + if (g.BOARD.ID === 'f' || g.VIEW === 'catalog' || !Conf['JSON Navigation']) { return; } this.button = $.el('a', { @@ -11866,7 +11873,7 @@ Navigate = { path: window.location.pathname, init: function() { - if (g.VIEW === 'catalog' || g.BOARD.ID === 'f') { + if (g.VIEW === 'catalog' || g.BOARD.ID === 'f' || !Conf['JSON Navigation']) { return; } $.ready(function() { diff --git a/src/General/Config.coffee b/src/General/Config.coffee index d9691c4ec..bd6ac89c9 100755 --- a/src/General/Config.coffee +++ b/src/General/Config.coffee @@ -1,6 +1,10 @@ Config = main: 'Miscellaneous': + 'JSON Navigation' : [ + true + 'Use JSON for loading the Board Index and Threads. Also allows searching and sorting the board index and infinite scolling.' + ] 'Catalog Links': [ true 'Add toggle link in header menu to turn Navigation links into links to each board\'s catalog.' diff --git a/src/General/Header.coffee b/src/General/Header.coffee index 929dbba3c..90ac8205c 100755 --- a/src/General/Header.coffee +++ b/src/General/Header.coffee @@ -102,7 +102,8 @@ Header = $.ready => @footer = footer = $.id 'boardNavDesktopFoot' - $.on a, 'click', Navigate.navigate for a in $$ 'a', footer + if Conf['JSON Navigation'] + $.on a, 'click', Navigate.navigate for a in $$ 'a', footer if a = $ "a[href*='/#{g.BOARD}/']", footer a.className = 'current' @@ -143,7 +144,8 @@ Header = id: 'board-list' innerHTML: "" for a in $$ 'a', boardList - $.on a, 'click', Navigate.navigate + if Conf['JSON Navigation'] + $.on a, 'click', Navigate.navigate if a.pathname.split('/')[1] is g.BOARD.ID a.className = 'current' fullBoardList = $ '#full-board-list', boardList @@ -189,7 +191,8 @@ Header = if a.textContent is board a = a.cloneNode true - $.on a, 'click', Navigate.navigate + if Conf['JSON Navigation'] + $.on a, 'click', Navigate.navigate a.textContent = if /-title/.test(t) or /-replace/.test(t) and $.hasClass a, 'current' a.title diff --git a/src/General/Index.coffee b/src/General/Index.coffee index 9a758b808..b293af5de 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -1,6 +1,6 @@ Index = init: -> - return if g.BOARD.ID is 'f' or g.VIEW is 'catalog' + return if g.BOARD.ID is 'f' or g.VIEW is 'catalog' or !Conf['JSON Navigation'] @button = $.el 'a', className: 'index-refresh-shortcut fa fa-refresh' diff --git a/src/General/Navigate.coffee b/src/General/Navigate.coffee index 04d15302d..63806eb3a 100644 --- a/src/General/Navigate.coffee +++ b/src/General/Navigate.coffee @@ -1,7 +1,7 @@ Navigate = path: window.location.pathname init: -> - return if g.VIEW is 'catalog' or g.BOARD.ID is 'f' + return if g.VIEW is 'catalog' or g.BOARD.ID is 'f' or !Conf['JSON Navigation'] # blink/webkit throw a popstate on page load. Not what we want. $.ready -> $.on window, 'popstate', Navigate.popstate