OLD | NEW |
| (Empty) |
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. | |
4 | |
5 /** | |
6 * @fileoverview Script that runs on the background page. | |
7 */ | |
8 | |
9 CONTENT_SCRIPTS = [ | |
10 'accessibility_utils.js', | |
11 'traverse_util.js', | |
12 'caret_browsing.js' | |
13 ]; | |
14 | |
15 /** | |
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 | |
19 * is first loaded. | |
20 * @constructor | |
21 */ | |
22 var CaretBkgnd = function() {}; | |
23 | |
24 /** | |
25 * Flag indicating whether caret browsing is enabled. Global, applies to | |
26 * all tabs simultaneously. | |
27 * @type {boolean} | |
28 */ | |
29 CaretBkgnd.isEnabled; | |
30 | |
31 /** | |
32 * Change the browser action icon and tooltip based on the enabled state. | |
33 */ | |
34 CaretBkgnd.setIcon = function() { | |
35 chrome.browserAction.setIcon( | |
36 {'path': CaretBkgnd.isEnabled ? | |
37 '../caret_19_on.png' : | |
38 '../caret_19.png'}); | |
39 chrome.browserAction.setTitle( | |
40 {'title': CaretBkgnd.isEnabled ? | |
41 'Turn Off Caret Browsing (F7)' : | |
42 'Turn On Caret Browsing (F7)' }); | |
43 }; | |
44 | |
45 /** | |
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. | |
50 */ | |
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( | |
58 tabs[j].id, | |
59 {file: CONTENT_SCRIPTS[k], allFrames: true}, | |
60 function(result) { | |
61 // Ignore. | |
62 chrome.runtime.lastError; | |
63 }); | |
64 } | |
65 } | |
66 } | |
67 }); | |
68 }; | |
69 | |
70 /** | |
71 * Toggle caret browsing on or off, and update the browser action icon and | |
72 * all open tabs. | |
73 */ | |
74 CaretBkgnd.toggle = function() { | |
75 CaretBkgnd.isEnabled = !CaretBkgnd.isEnabled; | |
76 var obj = {}; | |
77 obj['enabled'] = CaretBkgnd.isEnabled; | |
78 chrome.storage.sync.set(obj); | |
79 CaretBkgnd.setIcon(); | |
80 }; | |
81 | |
82 /** | |
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. | |
88 */ | |
89 CaretBkgnd.init = function() { | |
90 chrome.storage.sync.get('enabled', function(result) { | |
91 CaretBkgnd.isEnabled = result['enabled']; | |
92 CaretBkgnd.setIcon(); | |
93 CaretBkgnd.injectContentScripts(); | |
94 | |
95 chrome.browserAction.onClicked.addListener(function(tab) { | |
96 CaretBkgnd.toggle(); | |
97 }); | |
98 }); | |
99 | |
100 chrome.storage.onChanged.addListener(function() { | |
101 chrome.storage.sync.get('enabled', function(result) { | |
102 CaretBkgnd.isEnabled = result['enabled']; | |
103 CaretBkgnd.setIcon(); | |
104 }); | |
105 }); | |
106 }; | |
107 | |
108 CaretBkgnd.init(); | |
OLD | NEW |