From e6159734361a761f96976c82a55f6731ee961871 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Fri, 10 Jan 2014 13:12:43 -0700 Subject: [PATCH] Optimize header regeneration --- LICENSE | 2 +- builds/4chan-X.user.js | 46 +++++++++++++++++++++++-------------- builds/crx/script.js | 46 +++++++++++++++++++++++-------------- src/General/Header.coffee | 28 +++++++++++++++------- src/General/Navigate.coffee | 7 +++--- 5 files changed, 82 insertions(+), 47 deletions(-) diff --git a/LICENSE b/LICENSE index ae866da0b..9ac7fdbf0 100755 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ /* -* 4chan X - Version 1.2.45 - 2014-01-09 +* 4chan X - Version 1.2.45 - 2014-01-10 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 783c0f2e3..1b86631ef 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -22,7 +22,7 @@ // ==/UserScript== /* -* 4chan X - Version 1.2.45 - 2014-01-09 +* 4chan X - Version 1.2.45 - 2014-01-10 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE @@ -1778,7 +1778,7 @@ } $.asap((function() { return $.id('boardNavMobile') || d.readyState !== 'loading'; - }), Header.setBoardList); + }), Header.initReady); $.prepend(d.body, _this.bar); $.add(d.body, Header.hover); _this.setBarPosition(Conf['Bottom Header']); @@ -1823,15 +1823,21 @@ toggle: $.el('div', { id: 'scroll-marker' }), - setBoardList: function() { - var a, boardList, btn, fourchannav, fullBoardList, _i, _len, _ref; - if (Header.bar.children.length) { - $.rmAll(Header.bar); - } + initReady: function() { + Header.cache(); + Header.setBoardList(); + return Header.addNav(); + }, + cache: function() { + var fourchannav; fourchannav = $.id('boardNavDesktop'); + return Header.navCache = ""; + }, + setBoardList: function() { + var a, boardList, btn, fullBoardList, _i, _len, _ref; boardList = $.el('span', { id: 'board-list', - innerHTML: "" + innerHTML: Header.navCache }); _ref = $$('a', boardList); for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -1846,20 +1852,26 @@ $.on(btn, 'click', Header.toggleBoardList); $.rm($('#navtopright', fullBoardList)); $.add(boardList, fullBoardList); - $.add(Header.bar, [boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]); + if (Header.boardList) { + $.replace(Header.boardList, boardList); + } + Header.boardList = boardList; + return Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' ')); + }, + addNav: function() { + $.add(Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]); Header.setCustomNav(Conf['Custom Board Navigation']); - Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' ')); $.sync('Custom Board Navigation', Header.setCustomNav); return $.sync('boardnav', Header.generateBoardList); }, generateBoardList: function(text) { var as, list, nodes; - list = $('#custom-board-list', Header.bar); + list = $('#custom-board-list', Header.boardList); $.rmAll(list); if (!text) { return; } - as = $$('#full-board-list a[title]', Header.bar); + as = $$('#full-board-list a[title]', Header.boardList); nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map(function(t) { var a, board, m, _i, _len; if (/^[^\w@]/.test(t)) { @@ -11905,10 +11917,10 @@ } }, ready: function() { - var condition, err, errors, feature, features, name, _i, _len, _ref; - features = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; - for (_i = 0, _len = features.length; _i < _len; _i++) { - _ref = features[_i], name = _ref[0], feature = _ref[1], condition = _ref[2]; + var condition, err, errors, feature, name, _i, _len, _ref, _ref1; + _ref = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + _ref1 = _ref[_i], name = _ref1[0], feature = _ref1[1], condition = _ref1[2]; try { if (condition) { feature.ready(); @@ -11991,7 +12003,7 @@ }, navigate: function(e) { var boardID, hash, onload, path, threadID, view; - if (this.hostname !== 'boards.4chan.org' || window.location.hostname === 'rs.4chan.org') { + if (this.hostname !== 'boards.4chan.org' || window.location.hostname === 'rs.4chan.org' || (e && (e.shiftKey || (e.type === 'click' && e.button !== 0)))) { return; } path = this.pathname.split('/'); diff --git a/builds/crx/script.js b/builds/crx/script.js index 2242102ff..d903d7a2f 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript /* -* 4chan X - Version 1.2.45 - 2014-01-09 +* 4chan X - Version 1.2.45 - 2014-01-10 * * Licensed under the MIT license. * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE @@ -1788,7 +1788,7 @@ } $.asap((function() { return $.id('boardNavMobile') || d.readyState !== 'loading'; - }), Header.setBoardList); + }), Header.initReady); $.prepend(d.body, _this.bar); $.add(d.body, Header.hover); _this.setBarPosition(Conf['Bottom Header']); @@ -1833,15 +1833,21 @@ toggle: $.el('div', { id: 'scroll-marker' }), - setBoardList: function() { - var a, boardList, btn, fourchannav, fullBoardList, _i, _len, _ref; - if (Header.bar.children.length) { - $.rmAll(Header.bar); - } + initReady: function() { + Header.cache(); + Header.setBoardList(); + return Header.addNav(); + }, + cache: function() { + var fourchannav; fourchannav = $.id('boardNavDesktop'); + return Header.navCache = ""; + }, + setBoardList: function() { + var a, boardList, btn, fullBoardList, _i, _len, _ref; boardList = $.el('span', { id: 'board-list', - innerHTML: "" + innerHTML: Header.navCache }); _ref = $$('a', boardList); for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -1856,20 +1862,26 @@ $.on(btn, 'click', Header.toggleBoardList); $.rm($('#navtopright', fullBoardList)); $.add(boardList, fullBoardList); - $.add(Header.bar, [boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]); + if (Header.boardList) { + $.replace(Header.boardList, boardList); + } + Header.boardList = boardList; + return Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' ')); + }, + addNav: function() { + $.add(Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]); Header.setCustomNav(Conf['Custom Board Navigation']); - Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' ')); $.sync('Custom Board Navigation', Header.setCustomNav); return $.sync('boardnav', Header.generateBoardList); }, generateBoardList: function(text) { var as, list, nodes; - list = $('#custom-board-list', Header.bar); + list = $('#custom-board-list', Header.boardList); $.rmAll(list); if (!text) { return; } - as = $$('#full-board-list a[title]', Header.bar); + as = $$('#full-board-list a[title]', Header.boardList); nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map(function(t) { var a, board, m, _i, _len; if (/^[^\w@]/.test(t)) { @@ -11894,10 +11906,10 @@ } }, ready: function() { - var condition, err, errors, feature, features, name, _i, _len, _ref; - features = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; - for (_i = 0, _len = features.length; _i < _len; _i++) { - _ref = features[_i], name = _ref[0], feature = _ref[1], condition = _ref[2]; + var condition, err, errors, feature, name, _i, _len, _ref, _ref1; + _ref = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + _ref1 = _ref[_i], name = _ref1[0], feature = _ref1[1], condition = _ref1[2]; try { if (condition) { feature.ready(); @@ -11980,7 +11992,7 @@ }, navigate: function(e) { var boardID, hash, onload, path, threadID, view; - if (this.hostname !== 'boards.4chan.org' || window.location.hostname === 'rs.4chan.org') { + if (this.hostname !== 'boards.4chan.org' || window.location.hostname === 'rs.4chan.org' || (e && (e.shiftKey || (e.type === 'click' && e.button !== 0)))) { return; } path = this.pathname.split('/'); diff --git a/src/General/Header.coffee b/src/General/Header.coffee index 67fd614ab..9e980feab 100755 --- a/src/General/Header.coffee +++ b/src/General/Header.coffee @@ -94,7 +94,7 @@ Header = return unless Main.isThisPageLegit() # Wait for #boardNavMobile instead of #boardNavDesktop, # it might be incomplete otherwise. - $.asap (-> $.id('boardNavMobile') or d.readyState isnt 'loading'), Header.setBoardList + $.asap (-> $.id('boardNavMobile') or d.readyState isnt 'loading'), Header.initReady $.prepend d.body, @bar $.add d.body, Header.hover @setBarPosition Conf['Bottom Header'] @@ -133,12 +133,19 @@ Header = toggle: $.el 'div', id: 'scroll-marker' - setBoardList: -> - $.rmAll Header.bar if Header.bar.children.length + initReady: -> + Header.cache() + Header.setBoardList() + Header.addNav() + + cache: -> fourchannav = $.id 'boardNavDesktop' + Header.navCache = "" + + setBoardList: -> boardList = $.el 'span', id: 'board-list' - innerHTML: "" + innerHTML: Header.navCache for a in $$ 'a', boardList $.on a, 'click', Navigate.navigate if a.pathname.split('/')[1] is g.BOARD.ID @@ -149,19 +156,24 @@ Header = $.rm $ '#navtopright', fullBoardList $.add boardList, fullBoardList - $.add Header.bar, [boardList, Header.shortcuts, Header.noticesRoot, Header.toggle] + + $.replace Header.boardList, boardList if Header.boardList + Header.boardList = boardList - Header.setCustomNav Conf['Custom Board Navigation'] Header.generateBoardList Conf['boardnav'].replace /(\r\n|\n|\r)/g, ' ' + addNav: -> + $.add Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle] + Header.setCustomNav Conf['Custom Board Navigation'] + $.sync 'Custom Board Navigation', Header.setCustomNav $.sync 'boardnav', Header.generateBoardList generateBoardList: (text) -> - list = $ '#custom-board-list', Header.bar + list = $ '#custom-board-list', Header.boardList $.rmAll list return unless text - as = $$ '#full-board-list a[title]', Header.bar + as = $$ '#full-board-list a[title]', Header.boardList nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map (t) -> if /^[^\w@]/.test t return $.tn t diff --git a/src/General/Navigate.coffee b/src/General/Navigate.coffee index 7f4161867..d984c0e1b 100644 --- a/src/General/Navigate.coffee +++ b/src/General/Navigate.coffee @@ -81,12 +81,10 @@ Navigate = return ready: -> - features = [ + for [name, feature, condition] in [ ['Unread Count', Unread, Conf['Unread Count']] ['Quote Threading', QuoteThreading, Conf['Quote Threading'] and not Conf['Unread Count']] ] - - for [name, feature, condition] in features try feature.ready() if condition catch err @@ -146,7 +144,8 @@ Navigate = $('.boardTitle').textContent = d.title = "/#{board}/ - #{title}" navigate: (e) -> - return if @hostname isnt 'boards.4chan.org' or window.location.hostname is 'rs.4chan.org' + return if @hostname isnt 'boards.4chan.org' or window.location.hostname is 'rs.4chan.org' or + (e and (e.shiftKey or (e.type is 'click' and e.button isnt 0))) # Not simply a left click path = @pathname.split '/' hash = @hash