summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/extensions/chromevoxclassic/chromevox/injected/mathjax.js
blob: 50c99fa06598825d711b8e425a714b9cc6cd7bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Bridge to MathJax functions from the ChromeVox content script.
 *
 */

if (typeof(goog) != 'undefined' && goog.provide) {
  goog.provide('cvox.MathJax');
}

if (typeof(goog) != 'undefined' && goog.require) {
  goog.require('cvox.Api');
  goog.require('cvox.MathJaxExternalUtil');
}

(function() {
  /**
   * The channel between the page and content script.
   * @type {MessageChannel}
   */
  var channel_ = new MessageChannel();


  /**
   * @constructor
   */
  cvox.MathJax = function() {
  };


  /**
   * Initializes message channel in Chromevox.
   */
  cvox.MathJax.initMessage = function() {
    channel_.port1.onmessage = function(evt) {
      cvox.MathJax.execMessage(evt.data);
    };
    window.postMessage(
        'cvox.MathJaxPortSetup', '*' /* target origin */,
        [channel_.port2] /* transfer */);
  };


  /**
   * Post a message to Chromevox.
   * @param {string} cmd The command to be executed in Chromevox.
   * @param {string} callbackId A string representing the callback id.
   * @param {Object<string, *>} args Dictionary of arguments.
   */
  cvox.MathJax.postMessage = function(cmd, callbackId, args) {
    channel_.port1.postMessage({'cmd': cmd, 'id': callbackId, 'args': args});
  };


  /**
   * Executes a command for an incoming message.
   * @param {{cmd: string, id: string, args: string}} msg A
   *     serializable message.
   */
  cvox.MathJax.execMessage = function(msg) {
    var args = msg.args;
    switch (msg.cmd) {
      case 'Active':
        cvox.MathJax.isActive(msg.id);
      break;
      case 'AllJax':
        cvox.MathJax.getAllJax(msg.id);
      break;
      case 'AsciiMathToMml':
        cvox.MathJax.asciiMathToMml(msg.id, args.alt, args.id);
      break;
      case 'InjectScripts':
        cvox.MathJax.injectScripts();
      break;
      case 'ConfWikipedia':
        cvox.MathJax.configMediaWiki();
      break;
      case 'RegSig':
        cvox.MathJax.registerSignal(msg.id, args.sig);
      break;
      case 'TexToMml':
        cvox.MathJax.texToMml(msg.id, args.alt, args.id);
      break;
    }
  };


  /**
   * Compute the MathML representation for all currently available MathJax
   * nodes.
   * @param {string} callbackId A string representing the callback id.
   */
  cvox.MathJax.getAllJax = function(callbackId) {
    cvox.MathJaxExternalUtil.getAllJax(
        cvox.MathJax.getMathmlCallback_(callbackId));
  };


  /**
   * Registers a callback for a particular Mathjax signal.
   * @param {string} callbackId A string representing the callback id.
   * @param {string} signal The Mathjax signal on which to fire the callback.
   */
  cvox.MathJax.registerSignal = function(callbackId, signal) {
    cvox.MathJaxExternalUtil.registerSignal(
        cvox.MathJax.getMathmlCallback_(callbackId), signal);
  };


  /**
   * Constructs a callback that posts a string with the MathML representation of
   * a MathJax element to ChromeVox.
   * @param {string} callbackId A string representing the callback id.
   * @return {function(Node, string)} A function taking a Mathml node and an id
   * string.
   * @private
   */
  cvox.MathJax.getMathmlCallback_ = function(callbackId) {
    return function(mml, id) {
      cvox.MathJax.postMessage('NodeMml', callbackId,
                               {'mathml': mml, 'elementId': id});
    };
  };


  /**
   * Inject a minimalistic MathJax script into a page for LaTeX translation.
   */
  cvox.MathJax.injectScripts = function() {
    cvox.MathJaxExternalUtil.injectConfigScript();
    cvox.MathJaxExternalUtil.injectLoadScript();
  };


  /**
   * Loads configurations for MediaWiki pages (e.g., Wikipedia).
   */
  cvox.MathJax.configMediaWiki = function() {
        cvox.MathJaxExternalUtil.configMediaWiki();
  };


  /**
   * Translates a LaTeX expressions into a MathML element.
   * @param {string} callbackId A string representing the callback id.
   * @param {string} tex The LaTeX expression.
   * @param {string} cvoxId A string representing the cvox id for the node.
   */
  cvox.MathJax.texToMml = function(callbackId, tex, cvoxId) {
    cvox.MathJaxExternalUtil.texToMml(
        function(mml) {
          cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId);
        },
        tex);
  };


  /**
   * Translates an AsciiMath expression into a MathML element.
   * @param {string} callbackId A string representing the callback id.
   * @param {string} asciiMath The AsciiMath expression.
   * @param {string} cvoxId A string representing the cvox id for the node.
   */
  cvox.MathJax.asciiMathToMml = function(callbackId, asciiMath, cvoxId) {
    cvox.MathJaxExternalUtil.asciiMathToMml(
        function(mml) {
          cvox.MathJax.getMathmlCallback_(callbackId)(mml, cvoxId);
        },
        asciiMath);
  };


  /**
   * Check if MathJax is injected in the page.
   * @param {string} callbackId A string representing the callback id.
   */
  cvox.MathJax.isActive = function(callbackId) {
    cvox.MathJax.postMessage(
        'Active', callbackId,
        {'status': cvox.MathJaxExternalUtil.isActive()});
  };


  // Initializing the bridge.
  cvox.MathJax.initMessage();

})();