From 2a94bb7150ce6a5eb34d98e6d5a767d95cef8880 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Fri, 9 Jan 2015 20:05:44 -0700 Subject: [PATCH] Update Dice, Fourchan, and IDColor --- builds/appchan-x.user.js | 69 +++++++++++++++++++++---------- builds/crx/script.js | 69 +++++++++++++++++++++---------- src/Miscellaneous/Dice.coffee | 2 +- src/Miscellaneous/Fourchan.coffee | 49 ++++++++++++++++------ src/Miscellaneous/IDColor.coffee | 20 +++++---- 5 files changed, 146 insertions(+), 63 deletions(-) diff --git a/builds/appchan-x.user.js b/builds/appchan-x.user.js index fc806fb83..d5b4883d2 100644 --- a/builds/appchan-x.user.js +++ b/builds/appchan-x.user.js @@ -14675,7 +14675,8 @@ Dice = { init: function() { - if (g.BOARD.ID !== 'tg' || !Conf['Show Dice Roll']) { + var _ref; + if (!(((_ref = g.VIEW) === 'index' || _ref === 'thread') && g.BOARD.ID === 'tg' && Conf['Show Dice Roll'])) { return; } return Post.callbacks.push({ @@ -15077,22 +15078,32 @@ Fourchan = { init: function() { - var board; - board = g.BOARD.ID; - if (board === 'g') { - $.globalEval("window.addEventListener('prettyprint', function(e) {\n window.dispatchEvent(new CustomEvent('prettyprint:cb', {\n detail: prettyPrintOne(e.detail)\n }));\n}, false);"); + var id, _ref; + if ((_ref = g.VIEW) !== 'index' && _ref !== 'thread') { + return; + } + id = g.BOARD.ID; + if (id === 'g') { + $.globalEval('window.addEventListener(\'prettyprint\', function(e) {\n window.dispatchEvent(new CustomEvent(\'prettyprint:cb\', {\n detail: prettyPrintOne(e.detail)\n }));\n}, false);'); Post.callbacks.push({ name: 'Parse /g/ code', cb: this.code }); } - if (board === 'sci') { - $.globalEval("window.addEventListener('jsmath', function(e) {\n if (jsMath.loaded) {\n // process one post\n jsMath.ProcessBeforeShowing(document.getElementById(e.detail));\n } else {\n // load jsMath and process whole document\n jsMath.Autoload.Script.Push('ProcessBeforeShowing', [null]);\n jsMath.Autoload.LoadJsMath();\n }\n}, false);"); - return Post.callbacks.push({ + if (id === 'sci') { + $.globalEval('window.addEventListener(\'jsmath\', function(e) {\n if (!jsMath) return;\n // process one post\n jsMath.ProcessBeforeShowing(e.target);\n } else if (jsMath.Autoload && jsMath.Autoload.checked) {\n // load jsMath and process whole document\n jsMath.Autoload.Script.Push(\'ProcessBeforeShowing\', [null]);\n jsMath.Autoload.LoadJsMath();\n }\n}, false);'); + Post.callbacks.push({ + name: 'Parse /sci/ math', + cb: this.math + }); + CatalogThread.callbacks.push({ name: 'Parse /sci/ math', cb: this.math }); } + return Main.ready(function() { + return $.globalEval('(function() {\n window.clickable_ids = false;\n var nodes = document.querySelectorAll(\'.posteruid, .capcode\');\n for (var i = 0; i < nodes.length; i++) {\n nodes[i].removeEventListener("click", window.idClick, false);\n }\n window.removeEventListener("message", Report.onMessage, false);\n})();'); + }); }, code: function() { var apply, pre, _i, _len, _ref; @@ -15100,7 +15111,8 @@ return; } apply = function(e) { - return pre.innerHTML = e.detail; + pre.innerHTML = e.detail; + return $.addClass(pre, 'prettyprinted'); }; $.on(window, 'prettyprint:cb', apply); _ref = $$('.prettyprint:not(.prettyprinted)', this.nodes.comment); @@ -15111,16 +15123,25 @@ $.off(window, 'prettyprint:cb', apply); }, math: function() { - if (this.isClone || !$('.math', this.nodes.comment)) { + if ((this.isClone && doc.contains(this.origin.nodes.root)) || !$('.math', this.nodes.comment)) { return; } - return $.event('jsmath', this.nodes.post.id, window); + return $.asap(((function(_this) { + return function() { + return doc.contains(_this.nodes.comment); + }; + })(this)), (function(_this) { + return function() { + return $.event('jsmath', null, _this.nodes.comment); + }; + })(this)); } }; IDColor = { init: function() { - if (!Conf['Color User IDs']) { + var _ref; + if (!(((_ref = g.VIEW) === 'index' || _ref === 'thread') && Conf['Color User IDs'])) { return; } this.ids = { @@ -15132,26 +15153,32 @@ }); }, node: function() { - var rgb, span, uid; + var rgb, span, style, uid; if (this.isClone || !((uid = this.info.uniqueID) && (span = $('span.hand', this.nodes.uniqueID)))) { return; } rgb = IDColor.ids[uid] || IDColor.compute(uid); - span.style.color = rgb[3]; - span.style.backgroundColor = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"; + style = span.style; + style.color = rgb[3]; + style.backgroundColor = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"; $.addClass(span, 'painted'); return span.title = 'Highlight posts by this ID'; }, compute: function(uid) { - var hash, i, rgb; - i = 1; - hash = uid.charCodeAt(0); - while (i < 8) { - hash = (hash << 5) - hash + uid.charCodeAt(i++); - } + var hash, rgb; + hash = IDColor.hash(uid); rgb = [(hash >> 24) & 0xFF, (hash >> 16) & 0xFF, (hash >> 8) & 0xFF]; rgb.push((rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114) > 125 ? '#000' : '#fff'); return this.ids[uid] = rgb; + }, + hash: function(uid) { + var i, msg; + msg = 0; + i = 0; + while (i < 8) { + msg = (msg << 5) - msg + uid.charCodeAt(i++); + } + return msg; } }; diff --git a/builds/crx/script.js b/builds/crx/script.js index 4b9541d5a..6d4081fd8 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -14696,7 +14696,8 @@ Dice = { init: function() { - if (g.BOARD.ID !== 'tg' || !Conf['Show Dice Roll']) { + var _ref; + if (!(((_ref = g.VIEW) === 'index' || _ref === 'thread') && g.BOARD.ID === 'tg' && Conf['Show Dice Roll'])) { return; } return Post.callbacks.push({ @@ -15098,22 +15099,32 @@ Fourchan = { init: function() { - var board; - board = g.BOARD.ID; - if (board === 'g') { - $.globalEval("window.addEventListener('prettyprint', function(e) {\n window.dispatchEvent(new CustomEvent('prettyprint:cb', {\n detail: prettyPrintOne(e.detail)\n }));\n}, false);"); + var id, _ref; + if ((_ref = g.VIEW) !== 'index' && _ref !== 'thread') { + return; + } + id = g.BOARD.ID; + if (id === 'g') { + $.globalEval('window.addEventListener(\'prettyprint\', function(e) {\n window.dispatchEvent(new CustomEvent(\'prettyprint:cb\', {\n detail: prettyPrintOne(e.detail)\n }));\n}, false);'); Post.callbacks.push({ name: 'Parse /g/ code', cb: this.code }); } - if (board === 'sci') { - $.globalEval("window.addEventListener('jsmath', function(e) {\n if (jsMath.loaded) {\n // process one post\n jsMath.ProcessBeforeShowing(document.getElementById(e.detail));\n } else {\n // load jsMath and process whole document\n jsMath.Autoload.Script.Push('ProcessBeforeShowing', [null]);\n jsMath.Autoload.LoadJsMath();\n }\n}, false);"); - return Post.callbacks.push({ + if (id === 'sci') { + $.globalEval('window.addEventListener(\'jsmath\', function(e) {\n if (!jsMath) return;\n // process one post\n jsMath.ProcessBeforeShowing(e.target);\n } else if (jsMath.Autoload && jsMath.Autoload.checked) {\n // load jsMath and process whole document\n jsMath.Autoload.Script.Push(\'ProcessBeforeShowing\', [null]);\n jsMath.Autoload.LoadJsMath();\n }\n}, false);'); + Post.callbacks.push({ + name: 'Parse /sci/ math', + cb: this.math + }); + CatalogThread.callbacks.push({ name: 'Parse /sci/ math', cb: this.math }); } + return Main.ready(function() { + return $.globalEval('(function() {\n window.clickable_ids = false;\n var nodes = document.querySelectorAll(\'.posteruid, .capcode\');\n for (var i = 0; i < nodes.length; i++) {\n nodes[i].removeEventListener("click", window.idClick, false);\n }\n window.removeEventListener("message", Report.onMessage, false);\n})();'); + }); }, code: function() { var apply, pre, _i, _len, _ref; @@ -15121,7 +15132,8 @@ return; } apply = function(e) { - return pre.innerHTML = e.detail; + pre.innerHTML = e.detail; + return $.addClass(pre, 'prettyprinted'); }; $.on(window, 'prettyprint:cb', apply); _ref = $$('.prettyprint:not(.prettyprinted)', this.nodes.comment); @@ -15132,16 +15144,25 @@ $.off(window, 'prettyprint:cb', apply); }, math: function() { - if (this.isClone || !$('.math', this.nodes.comment)) { + if ((this.isClone && doc.contains(this.origin.nodes.root)) || !$('.math', this.nodes.comment)) { return; } - return $.event('jsmath', this.nodes.post.id, window); + return $.asap(((function(_this) { + return function() { + return doc.contains(_this.nodes.comment); + }; + })(this)), (function(_this) { + return function() { + return $.event('jsmath', null, _this.nodes.comment); + }; + })(this)); } }; IDColor = { init: function() { - if (!Conf['Color User IDs']) { + var _ref; + if (!(((_ref = g.VIEW) === 'index' || _ref === 'thread') && Conf['Color User IDs'])) { return; } this.ids = { @@ -15153,26 +15174,32 @@ }); }, node: function() { - var rgb, span, uid; + var rgb, span, style, uid; if (this.isClone || !((uid = this.info.uniqueID) && (span = $('span.hand', this.nodes.uniqueID)))) { return; } rgb = IDColor.ids[uid] || IDColor.compute(uid); - span.style.color = rgb[3]; - span.style.backgroundColor = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"; + style = span.style; + style.color = rgb[3]; + style.backgroundColor = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"; $.addClass(span, 'painted'); return span.title = 'Highlight posts by this ID'; }, compute: function(uid) { - var hash, i, rgb; - i = 1; - hash = uid.charCodeAt(0); - while (i < 8) { - hash = (hash << 5) - hash + uid.charCodeAt(i++); - } + var hash, rgb; + hash = IDColor.hash(uid); rgb = [(hash >> 24) & 0xFF, (hash >> 16) & 0xFF, (hash >> 8) & 0xFF]; rgb.push((rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114) > 125 ? '#000' : '#fff'); return this.ids[uid] = rgb; + }, + hash: function(uid) { + var i, msg; + msg = 0; + i = 0; + while (i < 8) { + msg = (msg << 5) - msg + uid.charCodeAt(i++); + } + return msg; } }; diff --git a/src/Miscellaneous/Dice.coffee b/src/Miscellaneous/Dice.coffee index 8729d9faf..b1aa21429 100755 --- a/src/Miscellaneous/Dice.coffee +++ b/src/Miscellaneous/Dice.coffee @@ -1,6 +1,6 @@ Dice = init: -> - return if g.BOARD.ID isnt 'tg' or !Conf['Show Dice Roll'] + return unless g.VIEW in ['index', 'thread'] and g.BOARD.ID is 'tg' and Conf['Show Dice Roll'] Post.callbacks.push name: 'Show Dice Roll' cb: @node diff --git a/src/Miscellaneous/Fourchan.coffee b/src/Miscellaneous/Fourchan.coffee index 14f7b491b..294790f53 100755 --- a/src/Miscellaneous/Fourchan.coffee +++ b/src/Miscellaneous/Fourchan.coffee @@ -1,42 +1,65 @@ Fourchan = init: -> - board = g.BOARD.ID - if board is 'g' - $.globalEval """ + return unless g.VIEW in ['index', 'thread'] + + id = g.BOARD.ID + if id is 'g' + $.globalEval ''' window.addEventListener('prettyprint', function(e) { window.dispatchEvent(new CustomEvent('prettyprint:cb', { detail: prettyPrintOne(e.detail) })); }, false); - """ + ''' Post.callbacks.push name: 'Parse /g/ code' cb: @code - if board is 'sci' + if id is 'sci' # https://github.com/MayhemYDG/4chan-x/issues/645#issuecomment-13704562 - $.globalEval """ + $.globalEval ''' window.addEventListener('jsmath', function(e) { - if (jsMath.loaded) { + if (!jsMath) return; // process one post - jsMath.ProcessBeforeShowing(document.getElementById(e.detail)); - } else { + jsMath.ProcessBeforeShowing(e.target); + } else if (jsMath.Autoload && jsMath.Autoload.checked) { // load jsMath and process whole document jsMath.Autoload.Script.Push('ProcessBeforeShowing', [null]); jsMath.Autoload.LoadJsMath(); } }, false); - """ + ''' Post.callbacks.push name: 'Parse /sci/ math' cb: @math + + CatalogThread.callbacks.push + name: 'Parse /sci/ math' + cb: @math + + # Disable 4chan's ID highlighting (replaced by IDHighlight) and reported post hiding. + Main.ready -> + $.globalEval ''' + (function() { + window.clickable_ids = false; + var nodes = document.querySelectorAll('.posteruid, .capcode'); + for (var i = 0; i < nodes.length; i++) { + nodes[i].removeEventListener("click", window.idClick, false); + } + window.removeEventListener("message", Report.onMessage, false); + })(); + ''' + code: -> return if @isClone - apply = (e) -> pre.innerHTML = e.detail + apply = (e) -> + pre.innerHTML = e.detail + $.addClass pre, 'prettyprinted' $.on window, 'prettyprint:cb', apply for pre in $$ '.prettyprint:not(.prettyprinted)', @nodes.comment $.event 'prettyprint', pre.innerHTML, window $.off window, 'prettyprint:cb', apply return math: -> - return if @isClone or !$ '.math', @nodes.comment - $.event 'jsmath', @nodes.post.id, window + return if (@isClone and doc.contains @origin.nodes.root) or !$ '.math', @nodes.comment + $.asap (=> doc.contains @nodes.comment), => + $.event 'jsmath', null, @nodes.comment diff --git a/src/Miscellaneous/IDColor.coffee b/src/Miscellaneous/IDColor.coffee index 037949f9b..c509ff684 100755 --- a/src/Miscellaneous/IDColor.coffee +++ b/src/Miscellaneous/IDColor.coffee @@ -1,6 +1,6 @@ IDColor = init: -> - return if !Conf['Color User IDs'] + return unless g.VIEW in ['index', 'thread'] and Conf['Color User IDs'] @ids = { Heaven: [0, 0, 0, '#fff'] } @@ -15,17 +15,16 @@ IDColor = rgb = IDColor.ids[uid] or IDColor.compute uid # Style the damn node. - span.style.color = rgb[3] - span.style.backgroundColor = "rgb(#{rgb[0]},#{rgb[1]},#{rgb[2]})" + {style} = span + style.color = rgb[3] + style.backgroundColor = "rgb(#{rgb[0]},#{rgb[1]},#{rgb[2]})" $.addClass span, 'painted' span.title = 'Highlight posts by this ID' compute: (uid) -> # Convert chars to integers, bitshift and math to create a larger integer # Create a nice string of binary - i = 1 - hash = uid.charCodeAt 0 - hash = (hash << 5) - hash + uid.charCodeAt i++ while i < 8 + hash = IDColor.hash uid # Convert binary string to numerical values with bitshift and '&' truncation. rgb = [ @@ -41,4 +40,11 @@ IDColor = '#fff' # Cache. - @ids[uid] = rgb \ No newline at end of file + @ids[uid] = rgb + + hash: (uid) -> + msg = 0 + i = 0 + while i < 8 + msg = (msg << 5) - msg + uid.charCodeAt i++ + msg