OLD | NEW |
1 // Copyright 2016 Google Inc. All rights reserved. | 1 // Copyright 2016 Google Inc. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Load the options, then register a keyboard listener. We capture the event | 5 // Load the options, then register a keyboard listener. We capture the event |
6 // at the Window to let any handlers or listeners registered on the Document | 6 // at the Window to let any handlers or listeners registered on the Document |
7 // have a chance to handle it first. | 7 // have a chance to handle it first. |
8 var options; | 8 var options; |
9 chrome.storage.sync.get({ | 9 chrome.storage.sync.get({ |
10 blacklist: [], | 10 blacklist: [], |
11 disableInApplets: true, | 11 disableInApplets: true, |
12 whitelist: [] | 12 whitelist: [] |
13 }, function(items) { | 13 }, function(items) { |
14 options = items; | 14 options = items; |
15 window.addEventListener('keydown', function(e) { | 15 window.addEventListener('keydown', handleBackspace); |
16 handleBackspace(e); | 16 |
| 17 // Set up a longstanding connection to the background page. When the |
| 18 // extension updates, is disabled, or is uninstalled, this connection will |
| 19 // drop, and we will know to remove the listener. |
| 20 var port = chrome.runtime.connect(); |
| 21 port.onDisconnect.addListener(function(port) { |
| 22 window.removeEventListener('keydown', handleBackspace); |
17 }); | 23 }); |
18 }); | 24 }); |
19 | 25 |
20 // Update the local options when they're changed externally. | 26 // Update the local options when they're changed externally. |
21 chrome.storage.onChanged.addListener(function(changes, area) { | 27 chrome.storage.onChanged.addListener(function(changes, area) { |
22 if (area === 'sync') { | 28 if (area === 'sync') { |
23 if (changes.blacklist) | 29 if (changes.blacklist) |
24 options.blacklist = changes.blacklist.newValue; | 30 options.blacklist = changes.blacklist.newValue; |
25 if (changes.disableInApplets) | 31 if (changes.disableInApplets) |
26 options.disableInApplets = changes.disableInApplets.newValue; | 32 options.disableInApplets = changes.disableInApplets.newValue; |
27 if (changes.whitelist) | 33 if (changes.whitelist) |
28 options.whitelist = changes.whitelist.newValue; | 34 options.whitelist = changes.whitelist.newValue; |
29 } | 35 } |
30 }); | 36 }); |
31 | 37 |
32 // Check for shift-backspace or unmodified backspace and navigate if | 38 // Check for shift-backspace or unmodified backspace and navigate if |
33 // applicable. | 39 // applicable. |
34 function handleBackspace(e) { | 40 function handleBackspace(e) { |
35 if (e.defaultPrevented || | 41 if (e.defaultPrevented || |
36 e.key !== 'Backspace' || | 42 e.key !== 'Backspace' || |
37 e.altKey || | 43 e.altKey || |
38 e.ctrlKey || | 44 e.ctrlKey || |
39 e.metaKey) | 45 e.metaKey || |
| 46 window.history.length < 2) // Nowhere to go back or forward to anyway. |
40 return; | 47 return; |
41 | 48 |
42 // The blacklist overrides everything. | 49 // The blacklist overrides everything. |
43 var url = window.location.href; | 50 var url = window.location.href; |
44 if (options.blacklist.includes(url)) | 51 if (options.blacklist.includes(url)) |
45 return; | 52 return; |
46 | 53 |
47 // The whitelist overrides applet focus. | 54 // The whitelist overrides applet focus. |
48 // Listening on the Window means the event has no path (see | 55 // Listening on the Window means the event has no path (see |
49 // http://crbug.com/645527), so we'll have to look at the focused (active) | 56 // http://crbug.com/645527), so we'll have to look at the focused (active) |
(...skipping 12 matching lines...) Expand all Loading... |
62 // Return true if the option to disable the extension in applets is enabled, | 69 // Return true if the option to disable the extension in applets is enabled, |
63 // and focus is in an embedded Flash or Java applet. | 70 // and focus is in an embedded Flash or Java applet. |
64 function disabledInApplet(target) { | 71 function disabledInApplet(target) { |
65 if (!options.disableInApplets) | 72 if (!options.disableInApplets) |
66 return false; | 73 return false; |
67 | 74 |
68 var nodeName = target.nodeName.toUpperCase(); | 75 var nodeName = target.nodeName.toUpperCase(); |
69 var nodeType = target.type || ''; | 76 var nodeType = target.type || ''; |
70 nodeType = nodeType.toLowerCase(); | 77 nodeType = nodeType.toLowerCase(); |
71 if ((nodeName === 'EMBED' || nodeName === 'OBJECT') && | 78 if ((nodeName === 'EMBED' || nodeName === 'OBJECT') && |
72 (nodeType === 'application/x-shockwave-flash' || | 79 (nodeType === 'application/java' || |
73 nodeType === 'application/java')) { | 80 nodeType === 'application/pdf' || |
| 81 nodeType === 'application/x-chat' || |
| 82 nodeType === 'application/x-google-chrome-pdf' || |
| 83 nodeType === 'application/x-shockwave-flash')) { |
74 return true; | 84 return true; |
75 } | 85 } |
76 return false; | 86 return false; |
77 } | 87 } |
OLD | NEW |