Chromium Code Reviews| 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..592a472ba3f8801613653bcebf57fffacd611920 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. |
|
Devlin
2016/09/27 16:08:57
please add a crbug.com/nnn here.
Pam (message me for reviews)
2016/09/27 23:37:32
I'll take that as confirmation that it's not inten
|
| + try { |
| + chrome.runtime.sendMessage('', function(response) { |
|
Devlin
2016/09/27 16:08:57
shouldn't need ''
Pam (message me for reviews)
2016/09/27 23:37:32
That's the message, which is not optional. If it s
Devlin
2016/09/29 20:55:34
Whoops, you're right. This is fine.
|
| + // 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; |