Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Unified Diff: go-back-with-backspace/content_script.js

Issue 2325963003: Add options and a browser-action popup. (Closed) Base URL: https://chromium.googlesource.com/chromium/extensions-by-google.git@master
Patch Set: Response to comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go-back-with-backspace/build-zip.sh ('k') | go-back-with-backspace/installed.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
+}
« no previous file with comments | « go-back-with-backspace/build-zip.sh ('k') | go-back-with-backspace/installed.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698