• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * @fileoverview Bridge to MathJax functions from the ChromeVox content script.
17 *
18 * @author sorge@google.com (Volker Sorge)
19 */
20
21if (typeof(goog) != 'undefined' && goog.provide) {
22  goog.provide('cvox.MathJax');
23}
24
25if (typeof(goog) != 'undefined' && goog.require) {
26  goog.require('cvox.Api');
27  goog.require('cvox.MathJaxExternalUtil');
28}
29
30(function() {
31  /**
32   * The channel between the page and content script.
33   * @type {MessageChannel}
34   */
35  var channel_ = new MessageChannel();
36
37
38  /**
39   * @constructor
40   */
41  cvox.MathJax = function() {
42  };
43
44
45  /**
46   * Initializes message channel in Chromevox.
47   */
48  cvox.MathJax.initMessage = function() {
49    channel_.port1.onmessage = function(evt) {
50      cvox.MathJax.execMessage(evt.data);
51    };
52    window.postMessage('cvox.MathJaxPortSetup', [channel_.port2], '*');
53  };
54
55
56  /**
57   * Post a message to Chromevox.
58   * @param {string} cmd The command to be executed in Chromevox.
59   * @param {string} callbackId A string representing the callback id.
60   * @param {Object.<string, *>} args Dictionary of arguments.
61   */
62  cvox.MathJax.postMessage = function(cmd, callbackId, args) {
63    channel_.port1.postMessage({'cmd': cmd, 'id': callbackId, 'args': args});
64  };
65
66
67  /**
68   * Executes a command for an incoming message.
69   * @param {{cmd: string, id: string, args: string}} msg A
70   *     serializable message.
71   */
72  cvox.MathJax.execMessage = function(msg) {
73    var args = msg.args;
74    switch (msg.cmd) {
75      case 'Active':
76        cvox.MathJax.isActive(msg.id);
77      break;
78      case 'AllJax':
79        cvox.MathJax.getAllJax(msg.id);
80      break;
81      case 'AsciiMathToMml':
82        cvox.MathJax.asciiMathToMml(msg.id, args.alt, args.id);
83      break;
84      case 'InjectScripts':
85        cvox.MathJax.injectScripts();
86      break;
87      case 'ConfWikipedia':
88        cvox.MathJax.configMediaWiki();
89      break;
90      case 'RegSig':
91        cvox.MathJax.registerSignal(msg.id, args.sig);
92      break;
93      case 'TexToMml':
94        cvox.MathJax.texToMml(msg.id, args.alt, args.id);
95      break;
96    }
97  };
98
99
100  /**
101   * Compute the MathML representation for all currently available MathJax
102   * nodes.
103   * @param {string} callbackId A string representing the callback id.
104   */
105  cvox.MathJax.getAllJax = function(callbackId) {
106    cvox.MathJaxExternalUtil.getAllJax(
107        cvox.MathJax.getMathmlCallback_(callbackId));
108  };
109
110
111  /**
112   * Registers a callback for a particular Mathjax signal.
113   * @param {string} callbackId A string representing the callback id.
114   * @param {string} signal The Mathjax signal on which to fire the callback.
115   */
116  cvox.MathJax.registerSignal = function(callbackId, signal) {
117    cvox.MathJaxExternalUtil.registerSignal(
118        cvox.MathJax.getMathmlCallback_(callbackId), signal);
119  };
120
121
122  /**
123   * Constructs a callback that posts a string with the MathML representation of
124   * a MathJax element to ChromeVox.
125   * @param {string} callbackId A string representing the callback id.
126   * @return {function(Node, string)} A function taking a Mathml node and an id
127   * string.
128   * @private
129   */
130  cvox.MathJax.getMathmlCallback_ = function(callbackId) {
131    return function(mml, id) {
132      cvox.MathJax.postMessage('NodeMml', callbackId,
133                               {'mathml': mml, 'elementId': id});
134    };
135  };
136
137
138  /**
139   * Inject a minimalistic MathJax script into a page for LaTeX translation.
140   */
141  cvox.MathJax.injectScripts = function() {
142    cvox.MathJaxExternalUtil.injectConfigScript();
143    cvox.MathJaxExternalUtil.injectLoadScript();
144  };
145
146
147  /**
148   * Loads configurations for MediaWiki pages (e.g., Wikipedia).
149   */
150  cvox.MathJax.configMediaWiki = function() {
151        cvox.MathJaxExternalUtil.configMediaWiki();
152  };
153
154
155  /**
156   * Translates a LaTeX expressions into a MathML element.
157   * @param {string} callbackId A string representing the callback id.
158   * @param {string} tex The LaTeX expression.
159   * @param {string} cvoxId A string representing the cvox id for the node.
160   */
161  cvox.MathJax.texToMml = function(callbackId, tex, cvoxId) {
162    cvox.MathJaxExternalUtil.texToMml(
163        function(mml) {
164          cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId);
165        },
166        tex);
167  };
168
169
170  /**
171   * Translates an AsciiMath expression into a MathML element.
172   * @param {string} callbackId A string representing the callback id.
173   * @param {string} asciiMath The AsciiMath expression.
174   * @param {string} cvoxId A string representing the cvox id for the node.
175   */
176  cvox.MathJax.asciiMathToMml = function(callbackId, asciiMath, cvoxId) {
177    cvox.MathJaxExternalUtil.asciiMathToMml(
178        function(mml) {
179          cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId);
180        },
181        asciiMath);
182  };
183
184
185  /**
186   * Check if MathJax is injected in the page.
187   * @param {string} callbackId A string representing the callback id.
188   */
189  cvox.MathJax.isActive = function(callbackId) {
190    cvox.MathJax.postMessage(
191        'Active', callbackId,
192        {'status': cvox.MathJaxExternalUtil.isActive()});
193  };
194
195
196  // Initializing the bridge.
197  cvox.MathJax.initMessage();
198
199})();
200