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 f80ab2a2b3eab5adedfd2c31ba8c9b1ea571fe0c..cc7fca6218a17ebc7f28b84725daee28d3925a22 100644 |
--- a/go-back-with-backspace/content_script.js |
+++ b/go-back-with-backspace/content_script.js |
@@ -12,9 +12,7 @@ chrome.storage.sync.get({ |
whitelist: [] |
}, function(items) { |
options = items; |
- window.addEventListener('keydown', function(e) { |
- handleBackspace(e); |
- }); |
+ window.addEventListener('keydown', handleBackspace); |
}); |
// Update the local options when they're changed externally. |
@@ -36,7 +34,8 @@ function handleBackspace(e) { |
e.key !== 'Backspace' || |
e.altKey || |
e.ctrlKey || |
- e.metaKey) |
+ e.metaKey || |
+ window.history.length < 2) // Nowhere to go back or forward to anyway. |
return; |
// The blacklist overrides everything. |
@@ -55,8 +54,27 @@ function handleBackspace(e) { |
if (isEditable(document.activeElement)) |
return; |
- e.shiftKey ? window.history.forward(): window.history.back(); |
- e.preventDefault(); |
+ // Make sure this extension is still active. |
+ // sendMessage throws an internal error, not reported in lastError, if the |
+ // other end no longer exists. So we use JS error-catching rather than |
+ // extension errors. See http://crbug.com/650872 |
+ try { |
+ chrome.runtime.sendMessage('', function(response) { |
+ // Future-proofing in case sendMessage ever changes to setting lastError |
+ // instead of throwing a JS error. |
+ if (chrome.runtime.lastError) { |
+ window.removeEventListener('keydown', handleBackspace); |
+ } else { |
+ e.shiftKey ? window.history.forward(): window.history.back(); |
+ e.preventDefault(); |
+ } |
+ }); |
+ } catch(error) { |
+ // If we have no connection to the background page, the extension has |
+ // been updated, disabled, or uninstalled. Remove our listener and do |
+ // nothing. |
+ window.removeEventListener('keydown', handleBackspace); |
+ } |
} |
// Return true if the option to disable the extension in applets is enabled, |
@@ -69,8 +87,11 @@ function disabledInApplet(target) { |
var nodeType = target.type || ''; |
nodeType = nodeType.toLowerCase(); |
if ((nodeName === 'EMBED' || nodeName === 'OBJECT') && |
- (nodeType === 'application/x-shockwave-flash' || |
- nodeType === 'application/java')) { |
+ (nodeType === 'application/java' || |
+ nodeType === 'application/pdf' || |
+ nodeType === 'application/x-chat' || |
+ nodeType === 'application/x-google-chrome-pdf' || |
+ nodeType === 'application/x-shockwave-flash')) { |
return true; |
} |
return false; |