summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/extensions/caretbrowsing/background.js
blob: 18c722a2fc64fb00ae4b146fc05ca43096f123cf (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
// Copyright (c) 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 Script that runs on the background page.
 */

CONTENT_SCRIPTS = [
  'accessibility_utils.js',
  'traverse_util.js',
  'caret_browsing.js'
];

/**
 * The class handling the Caret Browsing background page, which keeps
 * track of the current state, handles the browser action button, and
 * initializes the content script in all running tabs when the extension
 * is first loaded.
 * @constructor
 */
var CaretBkgnd = function() {};

/**
 * Flag indicating whether caret browsing is enabled. Global, applies to
 * all tabs simultaneously.
 * @type {boolean}
 */
CaretBkgnd.isEnabled;

/**
 * Change the browser action icon and tooltip based on the enabled state.
 */
CaretBkgnd.setIcon = function() {
  chrome.browserAction.setIcon(
      {'path': CaretBkgnd.isEnabled ?
               '../caret_19_on.png' :
               '../caret_19.png'});
  chrome.browserAction.setTitle(
      {'title': CaretBkgnd.isEnabled ?
                'Turn Off Caret Browsing (F7)' :
                'Turn On Caret Browsing (F7)' });
};

/**
 * This is called when the extension is first loaded, so that it can be
 * immediately used in all already-open tabs. It's not needed for any
 * new tabs that open after that, the content script will be automatically
 * injected into any new tab.
 */
CaretBkgnd.injectContentScripts = function() {
  chrome.windows.getAll({'populate': true}, function(windows) {
    for (var i = 0; i < windows.length; i++) {
      var tabs = windows[i].tabs;
      for (var j = 0; j < tabs.length; j++) {
        for (var k = 0; k < CONTENT_SCRIPTS.length; k++) {
          chrome.tabs.executeScript(
              tabs[j].id,
              {file: CONTENT_SCRIPTS[k], allFrames: true},
              function(result) {
                // Ignore.
                chrome.runtime.lastError;
              });
        }
      }
    }
  });
};

/**
 * Toggle caret browsing on or off, and update the browser action icon and
 * all open tabs.
 */
CaretBkgnd.toggle = function() {
  CaretBkgnd.isEnabled = !CaretBkgnd.isEnabled;
  var obj = {};
  obj['enabled'] = CaretBkgnd.isEnabled;
  chrome.storage.sync.set(obj);
  CaretBkgnd.setIcon();
};

/**
 * Initialize the background script. Set the initial value of the flag
 * based on the saved preference in localStorage, update the browser action,
 * inject into running tabs, and then set up communication with content
 * scripts in tabs. Also check for prefs updates (from the options page)
 * and send them to content scripts.
 */
CaretBkgnd.init = function() {
  chrome.storage.sync.get('enabled', function(result) {
    CaretBkgnd.isEnabled = result['enabled'];
    CaretBkgnd.setIcon();
    CaretBkgnd.injectContentScripts();

    chrome.browserAction.onClicked.addListener(function(tab) {
      CaretBkgnd.toggle();
    });
  });

  chrome.storage.onChanged.addListener(function() {
    chrome.storage.sync.get('enabled', function(result) {
      CaretBkgnd.isEnabled = result['enabled'];
      CaretBkgnd.setIcon();
    });
  });
};

CaretBkgnd.init();