From d2b9b30239e3dbac94594bff882e1c50953f7692 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 8 Nov 2017 06:46:56 +0300 Subject: [PATCH] reliable userAgent detection in FF before 57 fixes #228 --- content/install.js | 8 ++++---- js/dom.js | 4 ++-- js/messaging.js | 18 +++++++++++++++--- js/prefs.js | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/content/install.js b/content/install.js index a20ff50a..e6810f9c 100644 --- a/content/install.js +++ b/content/install.js @@ -1,9 +1,9 @@ 'use strict'; -const CHROMIUM = /Chromium/.test(navigator.userAgent); // non-Windows Chromium -const FIREFOX = /Firefox/.test(navigator.userAgent); -const VIVALDI = /Vivaldi/.test(navigator.userAgent); -const OPERA = /OPR/.test(navigator.userAgent); +const CHROMIUM = chrome.app && /Chromium/.test(navigator.userAgent); // non-Windows Chromium +const FIREFOX = !chrome.app; +const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent); +const OPERA = chrome.app && /OPR/.test(navigator.userAgent); document.addEventListener('stylishUpdate', onUpdateClicked); document.addEventListener('stylishUpdateChrome', onUpdateClicked); diff --git a/js/dom.js b/js/dom.js index 249de722..630d0738 100644 --- a/js/dom.js +++ b/js/dom.js @@ -1,6 +1,6 @@ 'use strict'; -if (!navigator.userAgent.includes('Windows')) { +if (!/^Win\d+/.test(navigator.platform)) { document.documentElement.classList.add('non-windows'); } @@ -38,7 +38,7 @@ for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) onDOMready().then(() => $('#firefox-transitions-bug-suppressor').remove()); -if (navigator.userAgent.includes('Firefox')) { +if (!chrome.app) { // die if unable to access BG directly chrome.windows.getCurrent(wnd => { if (!BG && wnd.incognito) { diff --git a/js/messaging.js b/js/messaging.js index 6d4a19a7..164eafc7 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -1,11 +1,22 @@ /* global BG: true, onRuntimeMessage, applyOnMessage, handleUpdate, handleDelete */ +/* global FIREFOX: true */ 'use strict'; // keep message channel open for sendResponse in chrome.runtime.onMessage listener const KEEP_CHANNEL_OPEN = true; -const FIREFOX = parseFloat(navigator.userAgent.match(/\bFirefox\/(\d+\.\d+)|$/)[1]); -const OPERA = parseFloat(navigator.userAgent.match(/\bOPR\/(\d+\.\d+)|$/)[1]); +let FIREFOX = !chrome.app && parseFloat(navigator.userAgent.match(/\bFirefox\/(\d+\.\d+)|$/)[1]); +const OPERA = chrome.app && parseFloat(navigator.userAgent.match(/\bOPR\/(\d+\.\d+)|$/)[1]); + +if (!chrome.app && !chrome.browserAction.openPopup) { + // in FF pre-57 legacy addons can override useragent so we assume the worst + // until we know for sure in the async getBrowserInfo() + // (browserAction.openPopup was added in 57) + FIREFOX = 50; + browser.runtime.getBrowserInfo().then(info => { + FIREFOX = parseFloat(info.version); + }); +} const URLS = { ownOrigin: chrome.runtime.getURL(''), @@ -29,6 +40,7 @@ const URLS = { // Chrome 61.0.3161+ doesn't run content scripts on NTP https://crrev.com/2978953002/ // TODO: remove when "minimum_chrome_version": "61" or higher chromeProtectsNTP: + chrome.app && parseInt(navigator.userAgent.match(/Chrom\w+\/(?:\d+\.){2}(\d+)|$/)[1]) >= 3161, supported: url => ( @@ -52,7 +64,7 @@ if (!BG || BG !== window) { document.documentElement.classList.toggle('opera', OPERA); // TODO: remove once our manifest's minimum_chrome_version is 50+ // Chrome 49 doesn't report own extension pages in webNavigation apparently - if (navigator.userAgent.includes('Chrome/49.')) { + if (chrome.app && navigator.userAgent.includes('Chrome/49.')) { getActiveTab().then(BG.updateIcon); } } diff --git a/js/prefs.js b/js/prefs.js index 7938a475..70adcefb 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -317,7 +317,7 @@ var prefs = new function Prefs() { } function contextDeleteMissing() { - return ( + return chrome.app && ( // detect browsers without Delete by looking at the end of UA string /Vivaldi\/[\d.]+$/.test(navigator.userAgent) || // Chrome and co.