From 2bd217a29ea91b313fe5f46828621bb27d69cb06 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sun, 8 Sep 2019 21:52:42 +0200 Subject: [PATCH] fix: feature detection for Google Chrome Old version did not check if chrome.sockets.udp actually exist. This caused problems with extensions built for Chromium-based browsers that tried to maintain the single codebase and do feature-detection at runtime. License: MIT Signed-off-by: Marcin Rataj --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6962ee2..9479e19 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,14 @@ var BIND_STATE_BOUND = 2 // Track open sockets to route incoming data (via onReceive) to the right handlers. var sockets = {} -if (typeof chrome !== 'undefined') { +// Thorough check for Chrome App since both Edge and Chrome implement dummy chrome object +if ( + typeof chrome === 'object' && + typeof chrome.runtime === 'object' && + typeof chrome.runtime.id === 'string' && + typeof chrome.sockets === 'object' && + typeof chrome.sockets.udp === 'object' +) { chrome.sockets.udp.onReceive.addListener(onReceive) chrome.sockets.udp.onReceiveError.addListener(onReceiveError) } else {