From efa4ff3bcf7877ed542556e759382f41447ef00c Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 26 Sep 2019 15:57:55 -0400 Subject: [PATCH] Code review re. dynamically loaded vapi-client-extra.js Related commit: - https://github.com/gorhill/uBlock/commit/87d0e456f1997cddb75168ee8dff6c8afcae1636 Ensure that the code which depends on extending `vapi-client.js` is ready to deal with `vapi-client-extra.js` failing to load. --- platform/chromium/vapi-client.js | 14 ++++++++++++ src/js/scriptlets/cosmetic-logger.js | 9 +++----- src/js/scriptlets/dom-inspector.js | 34 ++++++++++++++-------------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index 6dad8a212..25b021ef5 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -79,6 +79,7 @@ vAPI.messaging = { port: null, portTimer: null, portTimerDelay: 10000, + extended: undefined, extensions: [], msgIdGenerator: 1, pending: new Map(), @@ -219,6 +220,19 @@ vAPI.messaging = { port.postMessage({ channel, msgId, msg }); return promise; }, + + // Dynamically extend capabilities. + extend: function() { + if ( this.extended === undefined ) { + this.extended = vAPI.messaging.send('vapi', { + what: 'extendClient' + }).then(( ) => { + return vAPI instanceof Object && this.extensions.length !== 0; + }).catch(( ) => { + }); + } + return this.extended; + }, }; vAPI.shutdown.add(( ) => { diff --git a/src/js/scriptlets/cosmetic-logger.js b/src/js/scriptlets/cosmetic-logger.js index 6115da315..1120c68d4 100644 --- a/src/js/scriptlets/cosmetic-logger.js +++ b/src/js/scriptlets/cosmetic-logger.js @@ -303,11 +303,8 @@ const handlers = { /******************************************************************************/ -(async ( ) => { - // Dynamically add broadcast listening abilities. - if ( vAPI.broadcastListener instanceof Object === false ) { - await vAPI.messaging.send('vapi', { what: 'extendClient' }); - } +vAPI.messaging.extend().then(extended => { + if ( extended !== true ) { return; } const broadcastListener = msg => { if ( msg.what === 'loggerDisabled' ) { processTimer.clear(); @@ -318,7 +315,7 @@ const handlers = { } }; vAPI.broadcastListener.add(broadcastListener); -})(); +}); vAPI.domWatcher.addListener(handlers); diff --git a/src/js/scriptlets/dom-inspector.js b/src/js/scriptlets/dom-inspector.js index 22805e97e..c4cfc8368 100644 --- a/src/js/scriptlets/dom-inspector.js +++ b/src/js/scriptlets/dom-inspector.js @@ -884,7 +884,7 @@ const onMessage = function(request) { // Install DOM inspector widget -const bootstrap = async function(ev) { +const bootstrap = function(ev) { if ( ev ) { pickerRoot.removeEventListener(ev.type, bootstrap); } @@ -934,22 +934,22 @@ const bootstrap = async function(ev) { // Dynamically add direct connection abilities so that we can establish // a direct, fast messaging connection to the logger. - if ( vAPI.MessagingConnection instanceof Function === false ) { - await vAPI.messaging.send('vapi', { what: 'extendClient' }); - } - vAPI.MessagingConnection.connectTo('domInspector', 'loggerUI', msg => { - switch ( msg.what ) { - case 'connectionAccepted': - loggerConnectionId = msg.id; - start(); - break; - case 'connectionBroken': - shutdown(); - break; - case 'connectionMessage': - onMessage(msg.payload); - break; - } + vAPI.messaging.extend().then(extended => { + if ( extended !== true ) { return; } + vAPI.MessagingConnection.connectTo('domInspector', 'loggerUI', msg => { + switch ( msg.what ) { + case 'connectionAccepted': + loggerConnectionId = msg.id; + start(); + break; + case 'connectionBroken': + shutdown(); + break; + case 'connectionMessage': + onMessage(msg.payload); + break; + } + }); }); };