1 // Copyright (c) 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.
6 * @fileoverview Script that runs on the background page.
10 'accessibility_utils.js',
16 * The class handling the Caret Browsing background page, which keeps
17 * track of the current state, handles the browser action button, and
18 * initializes the content script in all running tabs when the extension
22 var CaretBkgnd = function() {};
25 * Flag indicating whether caret browsing is enabled. Global, applies to
26 * all tabs simultaneously.
32 * Change the browser action icon and tooltip based on the enabled state.
34 CaretBkgnd.setIcon = function() {
35 chrome.browserAction.setIcon(
36 {'path': CaretBkgnd.isEnabled ?
37 '../caret_19_on.png' :
39 chrome.browserAction.setTitle(
40 {'title': CaretBkgnd.isEnabled ?
41 'Turn Off Caret Browsing (F7)' :
42 'Turn On Caret Browsing (F7)' });
46 * This is called when the extension is first loaded, so that it can be
47 * immediately used in all already-open tabs. It's not needed for any
48 * new tabs that open after that, the content script will be automatically
49 * injected into any new tab.
51 CaretBkgnd.injectContentScripts = function() {
52 chrome.windows.getAll({'populate': true}, function(windows) {
53 for (var i = 0; i < windows.length; i++) {
54 var tabs = windows[i].tabs;
55 for (var j = 0; j < tabs.length; j++) {
56 for (var k = 0; k < CONTENT_SCRIPTS.length; k++) {
57 chrome.tabs.executeScript(
59 {file: CONTENT_SCRIPTS[k], allFrames: true},
62 chrome.runtime.lastError;
71 * Toggle caret browsing on or off, and update the browser action icon and
74 CaretBkgnd.toggle = function() {
75 CaretBkgnd.isEnabled = !CaretBkgnd.isEnabled;
77 obj['enabled'] = CaretBkgnd.isEnabled;
78 chrome.storage.sync.set(obj);
83 * Initialize the background script. Set the initial value of the flag
84 * based on the saved preference in localStorage, update the browser action,
85 * inject into running tabs, and then set up communication with content
86 * scripts in tabs. Also check for prefs updates (from the options page)
87 * and send them to content scripts.
89 CaretBkgnd.init = function() {
90 chrome.storage.sync.get('enabled', function(result) {
91 CaretBkgnd.isEnabled = result['enabled'];
93 CaretBkgnd.injectContentScripts();
95 chrome.browserAction.onClicked.addListener(function(tab) {
100 chrome.storage.onChanged.addListener(function() {
101 chrome.storage.sync.get('enabled', function(result) {
102 CaretBkgnd.isEnabled = result['enabled'];
103 CaretBkgnd.setIcon();