From e93117cbb607472a830e1c0653dfbddde4c965fc Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 14 Mar 2023 18:50:01 -0400 Subject: [PATCH] Add `call-nothrow` scriptlet The purpose is to prevent a call to an existing function from throwing an exception. The exception will be caught by the scriptlet and neutralized. The first argument must be a reference to a function call. At the moment, the function call must exist at the time the scriptlet is called. --- assets/resources/scriptlets.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 4e7bbe17b1094..9bcbd4308e68b 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -1981,6 +1981,34 @@ })(); +/// call-nothrow.js +(function() { + const chain = '{{1}}'; + if ( chain === '' || chain === '{{1}}' ) { return; } + const parts = chain.split('.'); + let owner = window, prop; + for (;;) { + prop = parts.shift(); + if ( parts.length === 0 ) { break; } + owner = owner[prop]; + if ( owner instanceof Object === false ) { return; } + } + if ( prop === '' ) { return; } + const fn = owner[prop]; + if ( typeof fn !== 'function' ) { return; } + owner[prop] = new Proxy(fn, { + apply: function(...args) { + let r; + try { + r = Reflect.apply(...args); + } catch(ex) { + } + return r; + }, + }); +})(); + + // These lines below are skipped by the resource parser. // <<<< end of private namespace })();