Tell 4chan to parse posts if we enabled 4chan's extension.

Add code/math parsing.
Add math tags keybinds.
Close #507.
This commit is contained in:
Nicolas Stepien 2013-02-19 19:09:47 +01:00
parent 88afacb8ee
commit aa16428d78
6 changed files with 152 additions and 13 deletions

View File

@ -42,7 +42,7 @@
*/
(function() {
var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Get, Header, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadExcerpt, ThreadHiding, ThreadStats, ThreadUpdater, ThreadWatcher, Time, UI, Unread, d, doc, g,
var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Fourchan, Get, Header, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadExcerpt, ThreadHiding, ThreadStats, ThreadUpdater, ThreadWatcher, Time, UI, Unread, d, doc, g,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@ -146,6 +146,7 @@
'Close': ['Esc', 'Close Settings, Notifications or QR.'],
'Spoiler tags': ['Ctrl+s', 'Insert spoiler tags.'],
'Code tags': ['Alt+c', 'Insert code tags.'],
'Math tags': ['Alt+m', 'Insert math tags.'],
'Submit QR': ['Alt+s', 'Submit post.'],
'Watch': ['w', 'Watch thread.'],
'Update': ['u', 'Update the thread now.'],
@ -1158,6 +1159,60 @@
}
};
Fourchan = {
init: function() {
var board;
if (g.VIEW === 'catalog') {
return;
}
board = g.BOARD.ID;
if (board === 'g') {
Post.prototype.callbacks.push({
name: 'Parse /g/ code',
cb: this.code
});
}
if (board === 'sci') {
return Post.prototype.callbacks.push({
name: 'Parse /sci/ math',
cb: this.math
});
}
},
code: function() {
var pre, _i, _len, _ref;
if (this.isClone) {
return;
}
_ref = $$('.prettyprint', this.nodes.comment);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pre = _ref[_i];
pre.innerHTML = $.unsafeWindow.prettyPrintOne(pre.innerHTML);
}
},
math: function() {
var jsMath;
if (this.isClone || !$('.math', this.nodes.comment)) {
return;
}
jsMath = $.unsafeWindow.jsMath;
if (jsMath) {
if (jsMath.loaded) {
return jsMath.ProcessBeforeShowing(this.nodes.post);
} else {
return $.globalEval("jsMath.Autoload.Script.Push('ProcessBeforeShowing', [null]);\njsMath.Autoload.LoadJsMath();");
}
}
},
parseThread: function(threadID, offset, limit) {
return $.event('4chanParsingDone', {
threadId: threadID,
offset: offset,
limit: limit
});
}
};
Filter = {
filters: {},
init: function() {
@ -2297,6 +2352,12 @@
}
Keybinds.tags('code', target);
break;
case Conf['Math tags']:
if (target.nodeName !== 'TEXTAREA') {
return;
}
Keybinds.tags('math', target);
break;
case Conf['Submit QR']:
if (QR.el && !QR.status()) {
QR.submit();
@ -4323,7 +4384,13 @@
QuoteOP.node.call(post);
}
if (Conf['Mark Cross-thread Quotes']) {
return QuoteCT.node.call(post);
QuoteCT.node.call(post);
}
if (g.BOARD.ID === 'g') {
Fourchan.code.call(post);
}
if (g.BOARD.ID === 'sci') {
return Fourchan.math.call(post);
}
}
};
@ -4434,7 +4501,12 @@
nodes.push(node);
}
Main.callbackNodes(Post, posts);
return $.after(a, nodes);
$.after(a, nodes);
if (Conf['Enable 4chan\'s extension']) {
return $.unsafeWindow.Parser.parseThread(thread.ID, 1, nodes.length);
} else {
return Fourchan.parseThread(thread.ID, 1, nodes.length);
}
}
};
@ -4907,10 +4979,15 @@
if (scroll) {
nodes[0].scrollIntoView();
}
$.event('4chanParsingDone', {
threadId: ThreadUpdater.thread.ID,
offset: ThreadUpdater.root.children.length - count,
limit: ThreadUpdater.root.children.length
$.queueTask(function() {
var length, threadID;
threadID = ThreadUpdater.thread.ID;
length = ThreadUpdater.root.children.length;
if (Conf['Enable 4chan\'s extension']) {
return $.unsafeWindow.Parser.parseThread(threadID, -count);
} else {
return Fourchan.parseThread(threadID, length - count, length);
}
});
}
return $.event('ThreadUpdate', {
@ -5883,6 +5960,7 @@
return QR.response(this.response);
},
onerror: function() {
delete QR.ajax;
QR.cooldown.auto = false;
QR.status();
return QR.error($.el('a', {
@ -6381,6 +6459,7 @@
initFeature('Polyfill', Polyfill);
initFeature('Header', Header);
initFeature('Settings', Settings);
initFeature('Fourchan thingies', Fourchan);
initFeature('Resurrect Quotes', Quotify);
initFeature('Filter', Filter);
initFeature('Thread Hiding', ThreadHiding);

View File

@ -22,6 +22,7 @@ alpha
Thread & Post Hiding Buttons can now be disabled in the settings.
Recursive Hiding will be automatically applied when manually hiding a post.
Visible posts will not be taken into account towards the unread count.
Added [math] tags keybind.
Fix Chrome's install warning saying that 4chan X would execute on all domains.
Fix Quote Backlinks not affecting inlined quotes.
Fix Quote Highlighting not affecting inlined quotes.

View File

@ -136,6 +136,7 @@ Config =
'Close': ['Esc', 'Close Settings, Notifications or QR.']
'Spoiler tags': ['Ctrl+s', 'Insert spoiler tags.']
'Code tags': ['Alt+c', 'Insert code tags.']
'Math tags': ['Alt+m', 'Insert math tags.']
'Submit QR': ['Alt+s', 'Submit post.']
# Thread related
'Watch': ['w', 'Watch thread.']

View File

@ -151,6 +151,47 @@ Settings =
$.event 'CloseMenu'
# Here be settings
Fourchan =
init: ->
return if g.VIEW is 'catalog'
board = g.BOARD.ID
if board is 'g'
Post::callbacks.push
name: 'Parse /g/ code'
cb: @code
if board is 'sci'
Post::callbacks.push
name: 'Parse /sci/ math'
cb: @math
code: ->
return if @isClone
for pre in $$ '.prettyprint', @nodes.comment
pre.innerHTML = $.unsafeWindow.prettyPrintOne pre.innerHTML
return
math: ->
return if @isClone or !$ '.math', @nodes.comment
# https://github.com/MayhemYDG/4chan-x/issues/645#issuecomment-13704562
{jsMath} = $.unsafeWindow
if jsMath
if jsMath.loaded
# process one post
jsMath.ProcessBeforeShowing @nodes.post
else
# load jsMath and process whole document
# Yes this requires to be globalEval'd, don't ask me why.
$.globalEval """
jsMath.Autoload.Script.Push('ProcessBeforeShowing', [null]);
jsMath.Autoload.LoadJsMath();
"""
parseThread: (threadID, offset, limit) ->
# Fix /sci/
# Fix /g/
$.event '4chanParsingDone',
threadId: threadID
offset: offset
limit: limit
Filter =
filters: {}
init: ->
@ -1064,6 +1105,9 @@ Keybinds =
when Conf['Code tags']
return if target.nodeName isnt 'TEXTAREA'
Keybinds.tags 'code', target
when Conf['Math tags']
return if target.nodeName isnt 'TEXTAREA'
Keybinds.tags 'math', target
when Conf['Submit QR']
QR.submit() if QR.el and !QR.status()
# Thread related
@ -2726,8 +2770,10 @@ ExpandComment =
QuoteOP.node.call post
if Conf['Mark Cross-thread Quotes']
QuoteCT.node.call post
# XXX g code
# XXX sci math
if g.BOARD.ID is 'g'
Fourchan.code.call post
if g.BOARD.ID is 'sci'
Fourchan.math.call post
ExpandThread =
init: ->
@ -2813,6 +2859,12 @@ ExpandThread =
Main.callbackNodes Post, posts
$.after a, nodes
# Enable 4chan features.
if Conf['Enable 4chan\'s extension']
$.unsafeWindow.Parser.parseThread thread.ID, 1, nodes.length
else
Fourchan.parseThread thread.ID, 1, nodes.length
ThreadExcerpt =
init: ->
return if g.VIEW isnt 'thread' or !Conf['Thread Excerpt']
@ -3200,10 +3252,14 @@ ThreadUpdater =
if scroll
nodes[0].scrollIntoView()
$.event '4chanParsingDone',
threadId: ThreadUpdater.thread.ID
offset: ThreadUpdater.root.children.length - count
limit: ThreadUpdater.root.children.length
$.queueTask ->
# Enable 4chan features.
threadID = ThreadUpdater.thread.ID
{length} = ThreadUpdater.root.children
if Conf['Enable 4chan\'s extension']
$.unsafeWindow.Parser.parseThread threadID, -count
else
Fourchan.parseThread threadID, length - count, length
$.event 'ThreadUpdate',
404: false

View File

@ -307,6 +307,7 @@ Main =
initFeature 'Polyfill', Polyfill
initFeature 'Header', Header
initFeature 'Settings', Settings
initFeature 'Fourchan thingies', Fourchan
initFeature 'Resurrect Quotes', Quotify
initFeature 'Filter', Filter
initFeature 'Thread Hiding', ThreadHiding

View File

@ -705,6 +705,7 @@ QR =
onload: ->
QR.response @response
onerror: ->
delete QR.ajax
# Connection error, or
# CORS disabled error on www.4chan.org/banned
QR.cooldown.auto = false