From ddaf44ba7bfa8399ddc5e7f7025229f9aa03c700 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 13 Dec 2019 23:26:32 -0500 Subject: Isolate functions passed across window contexts --- ext/bg/js/util.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'ext/bg') diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index f7f6e770..96d549c9 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -32,11 +32,32 @@ function utilIsolate(value) { return typeof stringValue === 'string' ? JSON.parse(stringValue) : null; } +function utilFunctionIsolate(func) { + return function (...args) { + try { + return func.call(this, ...args); + } catch (e) { + try { + String(func); + } catch (e2) { + // Dead object + return; + } + throw e; + } + }; +} + function utilBackgroundIsolate(data) { const backgroundPage = chrome.extension.getBackgroundPage(); return backgroundPage.utilIsolate(data); } +function utilBackgroundFunctionIsolate(func) { + const backgroundPage = chrome.extension.getBackgroundPage(); + return backgroundPage.utilFunctionIsolate(func); +} + function utilSetEqual(setA, setB) { if (setA.size !== setB.size) { return false; @@ -110,15 +131,15 @@ function utilDatabasePurge() { function utilDatabaseDeleteDictionary(dictionaryName, onProgress) { return utilBackend().translator.database.deleteDictionary( utilBackgroundIsolate(dictionaryName), - onProgress + utilBackgroundFunctionIsolate(onProgress) ); } -async function utilDatabaseImport(data, progress, details) { +async function utilDatabaseImport(data, onProgress, details) { data = await utilReadFile(data); return utilBackend().translator.database.importDictionary( utilBackgroundIsolate(data), - progress, + utilBackgroundFunctionIsolate(onProgress), utilBackgroundIsolate(details) ); } -- cgit v1.2.3