Isolate try-catch in background script

This commit is contained in:
tophf 2015-08-01 15:06:47 +03:00
parent ae8683873a
commit bbddba9532

View File

@ -1,10 +1,10 @@
var frameIdMessageable; var frameIdMessageable;
try { runTryCatch(function() {
chrome.tabs.sendMessage(0, {}, {frameId: 0}, function() { chrome.tabs.sendMessage(0, {}, {frameId: 0}, function() {
var clearError = chrome.runtime.lastError; var clearError = chrome.runtime.lastError;
frameIdMessageable = true; frameIdMessageable = true;
}); });
} catch(e) {} });
// This happens right away, sometimes so fast that the content script isn't even ready. That's // This happens right away, sometimes so fast that the content script isn't even ready. That's
// why the content script also asks for this stuff. // why the content script also asks for this stuff.
@ -76,16 +76,17 @@ chrome.commands.onCommand.addListener(function(command) {
// contextMenus API is present in ancient Chrome but it throws an exception // contextMenus API is present in ancient Chrome but it throws an exception
// upon encountering the unsupported parameter value "browser_action", so we have to catch it. // upon encountering the unsupported parameter value "browser_action", so we have to catch it.
try { runTryCatch(function() {
chrome.contextMenus.create({ chrome.contextMenus.create({
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"), id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge") type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge")
}, function() { var clearError = chrome.runtime.lastError; }); }, function() { var clearError = chrome.runtime.lastError });
chrome.contextMenus.create({ chrome.contextMenus.create({
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"), id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll") type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll")
}, function() { var clearError = chrome.runtime.lastError; }); }, function() { var clearError = chrome.runtime.lastError });
} catch(e) {} });
chrome.contextMenus.onClicked.addListener(function(info, tab) { chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "disableAll") { if (info.menuItemId == "disableAll") {
disableAllStylesToggle(info.checked); disableAllStylesToggle(info.checked);
@ -251,13 +252,12 @@ function sectionAppliesToUrl(section, url) {
if (regexp[regexp.length - 1] != "$") { if (regexp[regexp.length - 1] != "$") {
regexp += "$"; regexp += "$";
} }
try { var re = runTryCatch(function() { return new RegExp(regexp) });
var re = new RegExp(regexp); if (re) {
} catch (ex) { return (re).test(url);
} else {
console.log(section.id + "'s regexp '" + regexp + "' is not valid"); console.log(section.id + "'s regexp '" + regexp + "' is not valid");
return false;
} }
return (re).test(url);
})) { })) {
//console.log(section.id + " applies to " + url + " due to regexp rules"); //console.log(section.id + " applies to " + url + " due to regexp rules");
return true; return true;
@ -403,6 +403,13 @@ function openURL(options) {
}); });
} }
// js engine can't optimize the entire function if it contains try-catch
// so we should keep it isolated from normal code in a minimal wrapper
function runTryCatch(func) {
try { return func() }
catch(e) {}
}
var codeMirrorThemes; var codeMirrorThemes;
getCodeMirrorThemes(function(themes) { getCodeMirrorThemes(function(themes) {
codeMirrorThemes = themes; codeMirrorThemes = themes;