blob: f80ab2a2b3eab5adedfd2c31ba8c9b1ea571fe0c [file] [log] [blame]
// 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 (!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;
}