From b9924973c5f315a9b3be7a4bb880b8d72c663400 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Fri, 10 Jan 2014 14:33:31 -0700 Subject: [PATCH] Implement a cheap and easy style switcher on navigate --- builds/4chan-X.user.js | 44 ++++++++++++++++++++++++------------- builds/crx/script.js | 44 ++++++++++++++++++++++++------------- src/General/Navigate.coffee | 24 ++++++++++++++++++-- 3 files changed, 80 insertions(+), 32 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index c06671ccb..5c22ef7e2 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -11950,7 +11950,7 @@ g.BOARD = new Board(boardID); req = null; onload = function(e) { - var board, err, _i, _len, _ref; + var board, mainStyleSheet, newStyleSheet, style, type, _ref; if (e.type === 'abort') { req.onloadend = null; return; @@ -11958,23 +11958,37 @@ if (req.status !== 200) { return; } - try { - _ref = JSON.parse(req.response).boards; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - board = _ref[_i]; - if (board.board === boardID) { - return Navigate.updateTitle(board); + board = (function() { + var err, _i, _len, _ref; + try { + _ref = JSON.parse(req.response).boards; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + board = _ref[_i]; + if (board.board === boardID) { + return board; + } } + } catch (_error) { + err = _error; + Main.handleErrors([ + { + message: "Navigation failed to update board name.", + error: err + } + ]); + return false; } - } catch (_error) { - err = _error; - return Main.handleErrors([ - { - message: "Navigation failed to update board name.", - error: err - } - ]); + })(); + if (!board) { + return; } + Navigate.updateTitle(board); + _ref = board.ws_board ? [(d.cookie.match(/ws\_style\=[^;]+/) || ['Yotsuba B New'])[0], 'ws_style'] : [(d.cookie.match(/nws\_style\=[^;]+/) || ['Yotsuba New'])[0], 'nws_style'], style = _ref[0], type = _ref[1]; + $.globalEval("var style_group = '" + type + "'"); + mainStyleSheet = $('link[title=switch]', d.head); + newStyleSheet = $("link[rel='alternate stylesheet'][title='" + style + "']", d.head); + mainStyleSheet.href = newStyleSheet.href; + return Main.initStyle(); }; fullBoardList = $('#full-board-list', Header.boardList); $.rmClass($('.current', fullBoardList), 'current'); diff --git a/builds/crx/script.js b/builds/crx/script.js index 8ce53019c..f26abb8a4 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -11939,7 +11939,7 @@ g.BOARD = new Board(boardID); req = null; onload = function(e) { - var board, err, _i, _len, _ref; + var board, mainStyleSheet, newStyleSheet, style, type, _ref; if (e.type === 'abort') { req.onloadend = null; return; @@ -11947,23 +11947,37 @@ if (req.status !== 200) { return; } - try { - _ref = JSON.parse(req.response).boards; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - board = _ref[_i]; - if (board.board === boardID) { - return Navigate.updateTitle(board); + board = (function() { + var err, _i, _len, _ref; + try { + _ref = JSON.parse(req.response).boards; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + board = _ref[_i]; + if (board.board === boardID) { + return board; + } } + } catch (_error) { + err = _error; + Main.handleErrors([ + { + message: "Navigation failed to update board name.", + error: err + } + ]); + return false; } - } catch (_error) { - err = _error; - return Main.handleErrors([ - { - message: "Navigation failed to update board name.", - error: err - } - ]); + })(); + if (!board) { + return; } + Navigate.updateTitle(board); + _ref = board.ws_board ? [(d.cookie.match(/ws\_style\=[^;]+/) || ['Yotsuba B New'])[0], 'ws_style'] : [(d.cookie.match(/nws\_style\=[^;]+/) || ['Yotsuba New'])[0], 'nws_style'], style = _ref[0], type = _ref[1]; + $.globalEval("var style_group = '" + type + "'"); + mainStyleSheet = $('link[title=switch]', d.head); + newStyleSheet = $("link[rel='alternate stylesheet'][title='" + style + "']", d.head); + mainStyleSheet.href = newStyleSheet.href; + return Main.initStyle(); }; fullBoardList = $('#full-board-list', Header.boardList); $.rmClass($('.current', fullBoardList), 'current'); diff --git a/src/General/Navigate.coffee b/src/General/Navigate.coffee index 9d6882540..d8215b805 100644 --- a/src/General/Navigate.coffee +++ b/src/General/Navigate.coffee @@ -123,15 +123,35 @@ Navigate = return unless req.status is 200 - try + board = do -> try for board in JSON.parse(req.response).boards - return Navigate.updateTitle board if board.board is boardID + return board if board.board is boardID catch err Main.handleErrors [ message: "Navigation failed to update board name." error: err ] + return false + + return unless board + Navigate.updateTitle board + [style, type] = if board.ws_board then [ + (d.cookie.match(/ws\_style\=[^;]+/) or ['Yotsuba B New'])[0] + 'ws_style' + ] else [ + (d.cookie.match(/nws\_style\=[^;]+/) or ['Yotsuba New'])[0] + 'nws_style' + ] + + $.globalEval "var style_group = '#{type}'" + + mainStyleSheet = $ 'link[title=switch]', d.head + newStyleSheet = $ "link[rel='alternate stylesheet'][title='#{style}']", d.head + + mainStyleSheet.href = newStyleSheet.href + + Main.initStyle() fullBoardList = $ '#full-board-list', Header.boardList $.rmClass $('.current', fullBoardList), 'current'