Last active
August 29, 2015 14:01
-
-
Save hlieberman/b5a386571c3583a0b7c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name MathJax inside IRCCloud | |
// @version 0.0.1 | |
// @author Harlan Lieberman-Berg <H.LiebermanBerg@gmail.com> | |
// @license BSD | |
// @include https://www.irccloud.com/* | |
// ==/UserScript== | |
function parse_mathjax() { | |
"use strict"; | |
// Add MathJax to the head of the page | |
// Note, this currently uses the MathJax CDN and could be used | |
// to hijack your IRCCloud account, so don't use this script | |
// if you don't trust the MathJax CDN implicitly | |
var head = document.getElementsByTagName("head")[0]; | |
var mathjax_script = document.createElement("script"); | |
mathjax_script.type = "text/x-mathjax-config"; | |
mathjax_script[(window.opera ? "innerHTML" : "text")] = | |
"MathJax.Hub.Config({\n" + | |
" tex2jax: {\n" + | |
" displayMath: [],\n" + | |
" inlineMath: [['$','$']],\n" + | |
" ignoreClass: \"paste\"\n" + | |
" }\n" + | |
"});"; | |
mathjax_script.type = "text/javascript"; | |
mathjax_script.src = "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; | |
head.appendChild(mathjax_script); | |
BufferLogView.prototype.oldPrepareInsertion = BufferLogView.prototype.prepareInsertion; | |
BufferLogView.prototype.prepareInsertion = function(els, classNames, initial) { | |
var procels = this.oldPrepareInsertion(els, classNames, initial); | |
for (var prop in procels) { | |
if (procels.hasOwnProperty(prop)) { | |
process_line(procels[prop]); | |
} | |
} | |
return procels; | |
} | |
function process_line(line) { | |
MathJax.Hub.Queue(["Typeset", MathJax.Hub, line]); | |
} | |
} | |
// Use injection method from irccloud-colornicks, from | |
// https://github.com/avidal/irccloud-colornicks/blob/master/src/colornicks.user.js | |
function inject(fn) { | |
"use strict"; | |
function waitloop(fn) { | |
var has_session = typeof(window.SESSION) !== 'undefined'; | |
var has_jquery = typeof(window.jQuery) !== 'undefined'; | |
if(has_jquery === false || has_session === false) { | |
console.log("[MJ] Resources are not ready..."); | |
window.setTimeout(function() { waitloop(fn); }, 100); | |
return; | |
} | |
console.log("[MJ] Required resources are ready, calling plugin function."); | |
fn(); | |
} | |
var wrap = "(" + fn.toString() + ")"; | |
console.log("[MJ] Injecting wrapper script."); | |
var script = document.createElement('script'); | |
script.textContent += "(" + waitloop.toString() + ')(' + wrap + ');'; | |
document.body.appendChild(script); | |
console.log("[MJ] Done injecting wrapper script."); | |
} | |
inject(parse_mathjax); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment