| Index: go-back-with-backspace/content_script.js
|
| diff --git a/go-back-with-backspace/content_script.js b/go-back-with-backspace/content_script.js
|
| index ea6cecb9faed2c4f69ea92af858ad7961f53eac5..f80ab2a2b3eab5adedfd2c31ba8c9b1ea571fe0c 100644
|
| --- a/go-back-with-backspace/content_script.js
|
| +++ b/go-back-with-backspace/content_script.js
|
| @@ -1,18 +1,77 @@
|
| -// Listen for shift-backspace or unmodified backspace and navigate if not in
|
| -// an editable field. We capture the event at the Window to let any handlers
|
| -// or listeners registered on the Document have a chance to handle it first.
|
| -window.addEventListener('keydown', function(e) {
|
| +// Copyright 2016 Google Inc. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +// Load the options, then register a keyboard listener. We capture the event
|
| +// at the Window to let any handlers or listeners registered on the Document
|
| +// have a chance to handle it first.
|
| +var options;
|
| +chrome.storage.sync.get({
|
| + blacklist: [],
|
| + disableInApplets: true,
|
| + whitelist: []
|
| +}, function(items) {
|
| + options = items;
|
| + window.addEventListener('keydown', function(e) {
|
| + handleBackspace(e);
|
| + });
|
| +});
|
| +
|
| +// Update the local options when they're changed externally.
|
| +chrome.storage.onChanged.addListener(function(changes, area) {
|
| + if (area === 'sync') {
|
| + if (changes.blacklist)
|
| + options.blacklist = changes.blacklist.newValue;
|
| + if (changes.disableInApplets)
|
| + options.disableInApplets = changes.disableInApplets.newValue;
|
| + if (changes.whitelist)
|
| + options.whitelist = changes.whitelist.newValue;
|
| + }
|
| +});
|
| +
|
| +// Check for shift-backspace or unmodified backspace and navigate if
|
| +// applicable.
|
| +function handleBackspace(e) {
|
| + if (e.defaultPrevented ||
|
| + e.key !== 'Backspace' ||
|
| + e.altKey ||
|
| + e.ctrlKey ||
|
| + e.metaKey)
|
| + return;
|
| +
|
| + // The blacklist overrides everything.
|
| + var url = window.location.href;
|
| + if (options.blacklist.includes(url))
|
| + return;
|
| +
|
| + // The whitelist overrides applet focus.
|
| // Listening on the Window means the event has no path (see
|
| // http://crbug.com/645527), so we'll have to look at the focused (active)
|
| // element. This means it will not work properly with shadow DOM.
|
| // TODO: Fix behavior with shadow DOM when the above bug is resolved.
|
| - if (e.key === 'Backspace' &&
|
| - !e.defaultPrevented &&
|
| - !e.altKey &&
|
| - !e.ctrlKey &&
|
| - !e.metaKey &&
|
| - !isEditable(document.activeElement)) {
|
| - e.shiftKey ? window.history.forward(): window.history.back();
|
| - e.preventDefault();
|
| + if (!options.whitelist.includes(url) &&
|
| + disabledInApplet(document.activeElement))
|
| + return;
|
| + if (isEditable(document.activeElement))
|
| + return;
|
| +
|
| + e.shiftKey ? window.history.forward(): window.history.back();
|
| + e.preventDefault();
|
| +}
|
| +
|
| +// Return true if the option to disable the extension in applets is enabled,
|
| +// and focus is in an embedded Flash or Java applet.
|
| +function disabledInApplet(target) {
|
| + if (!options.disableInApplets)
|
| + return false;
|
| +
|
| + var nodeName = target.nodeName.toUpperCase();
|
| + var nodeType = target.type || '';
|
| + nodeType = nodeType.toLowerCase();
|
| + if ((nodeName === 'EMBED' || nodeName === 'OBJECT') &&
|
| + (nodeType === 'application/x-shockwave-flash' ||
|
| + nodeType === 'application/java')) {
|
| + return true;
|
| }
|
| -});
|
| + return false;
|
| +}
|
|
|