Optimize header regeneration

This commit is contained in:
Zixaphir 2014-01-10 13:12:43 -07:00
parent 226ea6c27c
commit e615973436
5 changed files with 82 additions and 47 deletions

View File

@ -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. * Licensed under the MIT license.
* https://github.com/seaweedchan/4chan-x/blob/master/LICENSE * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE

View File

@ -22,7 +22,7 @@
// ==/UserScript== // ==/UserScript==
/* /*
* 4chan X - Version 1.2.45 - 2014-01-09 * 4chan X - Version 1.2.45 - 2014-01-10
* *
* Licensed under the MIT license. * Licensed under the MIT license.
* https://github.com/seaweedchan/4chan-x/blob/master/LICENSE * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE
@ -1778,7 +1778,7 @@
} }
$.asap((function() { $.asap((function() {
return $.id('boardNavMobile') || d.readyState !== 'loading'; return $.id('boardNavMobile') || d.readyState !== 'loading';
}), Header.setBoardList); }), Header.initReady);
$.prepend(d.body, _this.bar); $.prepend(d.body, _this.bar);
$.add(d.body, Header.hover); $.add(d.body, Header.hover);
_this.setBarPosition(Conf['Bottom Header']); _this.setBarPosition(Conf['Bottom Header']);
@ -1823,15 +1823,21 @@
toggle: $.el('div', { toggle: $.el('div', {
id: 'scroll-marker' id: 'scroll-marker'
}), }),
setBoardList: function() { initReady: function() {
var a, boardList, btn, fourchannav, fullBoardList, _i, _len, _ref; Header.cache();
if (Header.bar.children.length) { Header.setBoardList();
$.rmAll(Header.bar); return Header.addNav();
} },
cache: function() {
var fourchannav;
fourchannav = $.id('boardNavDesktop'); fourchannav = $.id('boardNavDesktop');
return Header.navCache = "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> " + fourchannav.innerHTML + "</span>";
},
setBoardList: function() {
var a, boardList, btn, fullBoardList, _i, _len, _ref;
boardList = $.el('span', { boardList = $.el('span', {
id: 'board-list', id: 'board-list',
innerHTML: "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> " + fourchannav.innerHTML + "</span>" innerHTML: Header.navCache
}); });
_ref = $$('a', boardList); _ref = $$('a', boardList);
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -1846,20 +1852,26 @@
$.on(btn, 'click', Header.toggleBoardList); $.on(btn, 'click', Header.toggleBoardList);
$.rm($('#navtopright', fullBoardList)); $.rm($('#navtopright', fullBoardList));
$.add(boardList, 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.setCustomNav(Conf['Custom Board Navigation']);
Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' '));
$.sync('Custom Board Navigation', Header.setCustomNav); $.sync('Custom Board Navigation', Header.setCustomNav);
return $.sync('boardnav', Header.generateBoardList); return $.sync('boardnav', Header.generateBoardList);
}, },
generateBoardList: function(text) { generateBoardList: function(text) {
var as, list, nodes; var as, list, nodes;
list = $('#custom-board-list', Header.bar); list = $('#custom-board-list', Header.boardList);
$.rmAll(list); $.rmAll(list);
if (!text) { if (!text) {
return; 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) { nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map(function(t) {
var a, board, m, _i, _len; var a, board, m, _i, _len;
if (/^[^\w@]/.test(t)) { if (/^[^\w@]/.test(t)) {
@ -11905,10 +11917,10 @@
} }
}, },
ready: function() { ready: function() {
var condition, err, errors, feature, features, name, _i, _len, _ref; var condition, err, errors, feature, name, _i, _len, _ref, _ref1;
features = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; _ref = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]];
for (_i = 0, _len = features.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
_ref = features[_i], name = _ref[0], feature = _ref[1], condition = _ref[2]; _ref1 = _ref[_i], name = _ref1[0], feature = _ref1[1], condition = _ref1[2];
try { try {
if (condition) { if (condition) {
feature.ready(); feature.ready();
@ -11991,7 +12003,7 @@
}, },
navigate: function(e) { navigate: function(e) {
var boardID, hash, onload, path, threadID, view; 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; return;
} }
path = this.pathname.split('/'); path = this.pathname.split('/');

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript // 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. * Licensed under the MIT license.
* https://github.com/seaweedchan/4chan-x/blob/master/LICENSE * https://github.com/seaweedchan/4chan-x/blob/master/LICENSE
@ -1788,7 +1788,7 @@
} }
$.asap((function() { $.asap((function() {
return $.id('boardNavMobile') || d.readyState !== 'loading'; return $.id('boardNavMobile') || d.readyState !== 'loading';
}), Header.setBoardList); }), Header.initReady);
$.prepend(d.body, _this.bar); $.prepend(d.body, _this.bar);
$.add(d.body, Header.hover); $.add(d.body, Header.hover);
_this.setBarPosition(Conf['Bottom Header']); _this.setBarPosition(Conf['Bottom Header']);
@ -1833,15 +1833,21 @@
toggle: $.el('div', { toggle: $.el('div', {
id: 'scroll-marker' id: 'scroll-marker'
}), }),
setBoardList: function() { initReady: function() {
var a, boardList, btn, fourchannav, fullBoardList, _i, _len, _ref; Header.cache();
if (Header.bar.children.length) { Header.setBoardList();
$.rmAll(Header.bar); return Header.addNav();
} },
cache: function() {
var fourchannav;
fourchannav = $.id('boardNavDesktop'); fourchannav = $.id('boardNavDesktop');
return Header.navCache = "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> " + fourchannav.innerHTML + "</span>";
},
setBoardList: function() {
var a, boardList, btn, fullBoardList, _i, _len, _ref;
boardList = $.el('span', { boardList = $.el('span', {
id: 'board-list', id: 'board-list',
innerHTML: "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> " + fourchannav.innerHTML + "</span>" innerHTML: Header.navCache
}); });
_ref = $$('a', boardList); _ref = $$('a', boardList);
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -1856,20 +1862,26 @@
$.on(btn, 'click', Header.toggleBoardList); $.on(btn, 'click', Header.toggleBoardList);
$.rm($('#navtopright', fullBoardList)); $.rm($('#navtopright', fullBoardList));
$.add(boardList, 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.setCustomNav(Conf['Custom Board Navigation']);
Header.generateBoardList(Conf['boardnav'].replace(/(\r\n|\n|\r)/g, ' '));
$.sync('Custom Board Navigation', Header.setCustomNav); $.sync('Custom Board Navigation', Header.setCustomNav);
return $.sync('boardnav', Header.generateBoardList); return $.sync('boardnav', Header.generateBoardList);
}, },
generateBoardList: function(text) { generateBoardList: function(text) {
var as, list, nodes; var as, list, nodes;
list = $('#custom-board-list', Header.bar); list = $('#custom-board-list', Header.boardList);
$.rmAll(list); $.rmAll(list);
if (!text) { if (!text) {
return; 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) { nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map(function(t) {
var a, board, m, _i, _len; var a, board, m, _i, _len;
if (/^[^\w@]/.test(t)) { if (/^[^\w@]/.test(t)) {
@ -11894,10 +11906,10 @@
} }
}, },
ready: function() { ready: function() {
var condition, err, errors, feature, features, name, _i, _len, _ref; var condition, err, errors, feature, name, _i, _len, _ref, _ref1;
features = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]]; _ref = [['Unread Count', Unread, Conf['Unread Count']], ['Quote Threading', QuoteThreading, Conf['Quote Threading'] && !Conf['Unread Count']]];
for (_i = 0, _len = features.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
_ref = features[_i], name = _ref[0], feature = _ref[1], condition = _ref[2]; _ref1 = _ref[_i], name = _ref1[0], feature = _ref1[1], condition = _ref1[2];
try { try {
if (condition) { if (condition) {
feature.ready(); feature.ready();
@ -11980,7 +11992,7 @@
}, },
navigate: function(e) { navigate: function(e) {
var boardID, hash, onload, path, threadID, view; 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; return;
} }
path = this.pathname.split('/'); path = this.pathname.split('/');

View File

@ -94,7 +94,7 @@ Header =
return unless Main.isThisPageLegit() return unless Main.isThisPageLegit()
# Wait for #boardNavMobile instead of #boardNavDesktop, # Wait for #boardNavMobile instead of #boardNavDesktop,
# it might be incomplete otherwise. # 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 $.prepend d.body, @bar
$.add d.body, Header.hover $.add d.body, Header.hover
@setBarPosition Conf['Bottom Header'] @setBarPosition Conf['Bottom Header']
@ -133,12 +133,19 @@ Header =
toggle: $.el 'div', toggle: $.el 'div',
id: 'scroll-marker' id: 'scroll-marker'
setBoardList: -> initReady: ->
$.rmAll Header.bar if Header.bar.children.length Header.cache()
Header.setBoardList()
Header.addNav()
cache: ->
fourchannav = $.id 'boardNavDesktop' fourchannav = $.id 'boardNavDesktop'
Header.navCache = "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> #{fourchannav.innerHTML}</span>"
setBoardList: ->
boardList = $.el 'span', boardList = $.el 'span',
id: 'board-list' id: 'board-list'
innerHTML: "<span id=custom-board-list></span><span id=full-board-list hidden><span class='hide-board-list-container brackets-wrap'><a href=javascript:; class='hide-board-list-button'>&nbsp;-&nbsp;</a></span> #{fourchannav.innerHTML}</span>" innerHTML: Header.navCache
for a in $$ 'a', boardList for a in $$ 'a', boardList
$.on a, 'click', Navigate.navigate $.on a, 'click', Navigate.navigate
if a.pathname.split('/')[1] is g.BOARD.ID if a.pathname.split('/')[1] is g.BOARD.ID
@ -149,19 +156,24 @@ Header =
$.rm $ '#navtopright', fullBoardList $.rm $ '#navtopright', fullBoardList
$.add boardList, fullBoardList $.add boardList, fullBoardList
$.add Header.bar, [boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]
Header.setCustomNav Conf['Custom Board Navigation'] $.replace Header.boardList, boardList if Header.boardList
Header.boardList = boardList
Header.generateBoardList Conf['boardnav'].replace /(\r\n|\n|\r)/g, ' ' 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 'Custom Board Navigation', Header.setCustomNav
$.sync 'boardnav', Header.generateBoardList $.sync 'boardnav', Header.generateBoardList
generateBoardList: (text) -> generateBoardList: (text) ->
list = $ '#custom-board-list', Header.bar list = $ '#custom-board-list', Header.boardList
$.rmAll list $.rmAll list
return unless text 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) -> nodes = text.match(/[\w@]+((-(all|title|replace|full|index|catalog|url:"[^"]+[^"]"|text:"[^"]+")|\,"[^"]+[^"]"))*|[^\w@]+/g).map (t) ->
if /^[^\w@]/.test t if /^[^\w@]/.test t
return $.tn t return $.tn t

View File

@ -81,12 +81,10 @@ Navigate =
return return
ready: -> ready: ->
features = [ for [name, feature, condition] in [
['Unread Count', Unread, Conf['Unread Count']] ['Unread Count', Unread, Conf['Unread Count']]
['Quote Threading', QuoteThreading, Conf['Quote Threading'] and not Conf['Unread Count']] ['Quote Threading', QuoteThreading, Conf['Quote Threading'] and not Conf['Unread Count']]
] ]
for [name, feature, condition] in features
try try
feature.ready() if condition feature.ready() if condition
catch err catch err
@ -146,7 +144,8 @@ Navigate =
$('.boardTitle').textContent = d.title = "/#{board}/ - #{title}" $('.boardTitle').textContent = d.title = "/#{board}/ - #{title}"
navigate: (e) -> 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 '/' path = @pathname.split '/'
hash = @hash hash = @hash