initial_speech.js revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 Defines the initial speech call.
7 */
8
9goog.provide('cvox.InitialSpeech');
10
11goog.require('cvox.AbstractTts');
12goog.require('cvox.BrailleOverlayWidget');
13goog.require('cvox.ChromeVox');
14goog.require('cvox.CursorSelection');
15goog.require('cvox.DescriptionUtil');
16goog.require('cvox.DomUtil');
17goog.require('cvox.LiveRegions');
18
19// INJECTED_AFTER_LOAD is set true by ChromeVox itself or ChromeOS when this
20// script is injected after page load (i.e. when manually enabling ChromeVox).
21if (!window['INJECTED_AFTER_LOAD'])
22  window['INJECTED_AFTER_LOAD'] = false;
23
24
25/**
26 * Initial speech when the page loads. This may happen only after we get
27 * prefs back, so we can make sure ChromeVox is active.
28 */
29cvox.InitialSpeech.speak = function() {
30  // Don't speak page title and other information if this script is not injected
31  // at the time of page load. This global is set by Chrome OS.
32  var disableSpeak = window['INJECTED_AFTER_LOAD'];
33
34  if (!cvox.ChromeVox.isActive || document.webkitHidden) {
35    disableSpeak = true;
36  }
37
38  // If we're the top-level frame, speak the title of the page +
39  // the active element if it is a user control.
40  if (window.top == window) {
41    var title = document.title;
42
43    // Allow the web author to disable reading the page title on load
44    // by adding aria-hidden=true to the <title> element.
45    var titleElement = document.querySelector('head > title');
46    if (titleElement && titleElement.getAttribute('aria-hidden') == 'true') {
47      title = null;
48    }
49
50    if (title && !disableSpeak) {
51      cvox.ChromeVox.tts.speak(
52          title, cvox.AbstractTts.QUEUE_MODE_FLUSH);
53    }
54    cvox.BrailleOverlayWidget.getInstance().init();
55  }
56
57  // Initialize live regions and speak alerts.
58  cvox.LiveRegions.init(
59      new Date(), cvox.AbstractTts.QUEUE_MODE_QUEUE, disableSpeak);
60
61  // If our activeElement is on body, try to sync to the first element. This
62  // actually happens inside of NavigationManager.reset, which doesn't get
63  // called until AbstractHost.onPageLoad, but we need to speak and braille the
64  // initial node here.
65  if (document.hasFocus() && document.activeElement == document.body) {
66    cvox.ChromeVox.navigationManager.syncToBeginning();
67  }
68
69  // If we had a previous position recorded, update to it.
70  if (cvox.ChromeVox.position[document.location.href]) {
71    var pos = cvox.ChromeVox.position[document.location.href];
72    cvox.ChromeVox.navigationManager.updateSelToArbitraryNode(
73        document.elementFromPoint(pos.x, pos.y));
74  }
75
76  // If this iframe has focus, speak and braille the current focused element.
77  if (document.hasFocus()) {
78    if (!disableSpeak) {
79      cvox.ChromeVoxEventSuspender.withSuspendedEvents(function() {
80        cvox.ChromeVox.navigationManager.finishNavCommand(
81            '', true, cvox.AbstractTts.QUEUE_MODE_QUEUE);
82      })();
83    }
84  }
85};
86