1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @fileoverview Bridge to MathJax functions from the ChromeVox content script. 7 * 8 */ 9 10if (typeof(goog) != 'undefined' && goog.provide) { 11 goog.provide('cvox.MathJax'); 12} 13 14if (typeof(goog) != 'undefined' && goog.require) { 15 goog.require('cvox.Api'); 16 goog.require('cvox.MathJaxExternalUtil'); 17} 18 19(function() { 20 /** 21 * The channel between the page and content script. 22 * @type {MessageChannel} 23 */ 24 var channel_ = new MessageChannel(); 25 26 27 /** 28 * @constructor 29 */ 30 cvox.MathJax = function() { 31 }; 32 33 34 /** 35 * Initializes message channel in Chromevox. 36 */ 37 cvox.MathJax.initMessage = function() { 38 channel_.port1.onmessage = function(evt) { 39 cvox.MathJax.execMessage(evt.data); 40 }; 41 window.postMessage('cvox.MathJaxPortSetup', [channel_.port2], '*'); 42 }; 43 44 45 /** 46 * Post a message to Chromevox. 47 * @param {string} cmd The command to be executed in Chromevox. 48 * @param {string} callbackId A string representing the callback id. 49 * @param {Object.<string, *>} args Dictionary of arguments. 50 */ 51 cvox.MathJax.postMessage = function(cmd, callbackId, args) { 52 channel_.port1.postMessage({'cmd': cmd, 'id': callbackId, 'args': args}); 53 }; 54 55 56 /** 57 * Executes a command for an incoming message. 58 * @param {{cmd: string, id: string, args: string}} msg A 59 * serializable message. 60 */ 61 cvox.MathJax.execMessage = function(msg) { 62 var args = msg.args; 63 switch (msg.cmd) { 64 case 'Active': 65 cvox.MathJax.isActive(msg.id); 66 break; 67 case 'AllJax': 68 cvox.MathJax.getAllJax(msg.id); 69 break; 70 case 'AsciiMathToMml': 71 cvox.MathJax.asciiMathToMml(msg.id, args.alt, args.id); 72 break; 73 case 'InjectScripts': 74 cvox.MathJax.injectScripts(); 75 break; 76 case 'ConfWikipedia': 77 cvox.MathJax.configMediaWiki(); 78 break; 79 case 'RegSig': 80 cvox.MathJax.registerSignal(msg.id, args.sig); 81 break; 82 case 'TexToMml': 83 cvox.MathJax.texToMml(msg.id, args.alt, args.id); 84 break; 85 } 86 }; 87 88 89 /** 90 * Compute the MathML representation for all currently available MathJax 91 * nodes. 92 * @param {string} callbackId A string representing the callback id. 93 */ 94 cvox.MathJax.getAllJax = function(callbackId) { 95 cvox.MathJaxExternalUtil.getAllJax( 96 cvox.MathJax.getMathmlCallback_(callbackId)); 97 }; 98 99 100 /** 101 * Registers a callback for a particular Mathjax signal. 102 * @param {string} callbackId A string representing the callback id. 103 * @param {string} signal The Mathjax signal on which to fire the callback. 104 */ 105 cvox.MathJax.registerSignal = function(callbackId, signal) { 106 cvox.MathJaxExternalUtil.registerSignal( 107 cvox.MathJax.getMathmlCallback_(callbackId), signal); 108 }; 109 110 111 /** 112 * Constructs a callback that posts a string with the MathML representation of 113 * a MathJax element to ChromeVox. 114 * @param {string} callbackId A string representing the callback id. 115 * @return {function(Node, string)} A function taking a Mathml node and an id 116 * string. 117 * @private 118 */ 119 cvox.MathJax.getMathmlCallback_ = function(callbackId) { 120 return function(mml, id) { 121 cvox.MathJax.postMessage('NodeMml', callbackId, 122 {'mathml': mml, 'elementId': id}); 123 }; 124 }; 125 126 127 /** 128 * Inject a minimalistic MathJax script into a page for LaTeX translation. 129 */ 130 cvox.MathJax.injectScripts = function() { 131 cvox.MathJaxExternalUtil.injectConfigScript(); 132 cvox.MathJaxExternalUtil.injectLoadScript(); 133 }; 134 135 136 /** 137 * Loads configurations for MediaWiki pages (e.g., Wikipedia). 138 */ 139 cvox.MathJax.configMediaWiki = function() { 140 cvox.MathJaxExternalUtil.configMediaWiki(); 141 }; 142 143 144 /** 145 * Translates a LaTeX expressions into a MathML element. 146 * @param {string} callbackId A string representing the callback id. 147 * @param {string} tex The LaTeX expression. 148 * @param {string} cvoxId A string representing the cvox id for the node. 149 */ 150 cvox.MathJax.texToMml = function(callbackId, tex, cvoxId) { 151 cvox.MathJaxExternalUtil.texToMml( 152 function(mml) { 153 cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId); 154 }, 155 tex); 156 }; 157 158 159 /** 160 * Translates an AsciiMath expression into a MathML element. 161 * @param {string} callbackId A string representing the callback id. 162 * @param {string} asciiMath The AsciiMath expression. 163 * @param {string} cvoxId A string representing the cvox id for the node. 164 */ 165 cvox.MathJax.asciiMathToMml = function(callbackId, asciiMath, cvoxId) { 166 cvox.MathJaxExternalUtil.asciiMathToMml( 167 function(mml) { 168 cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId); 169 }, 170 asciiMath); 171 }; 172 173 174 /** 175 * Check if MathJax is injected in the page. 176 * @param {string} callbackId A string representing the callback id. 177 */ 178 cvox.MathJax.isActive = function(callbackId) { 179 cvox.MathJax.postMessage( 180 'Active', callbackId, 181 {'status': cvox.MathJaxExternalUtil.isActive()}); 182 }; 183 184 185 // Initializing the bridge. 186 cvox.MathJax.initMessage(); 187 188})(); 189