From 60834f7bd6445ff3520c4a37c09e8e2f92456c03 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 27 Jan 2022 15:21:02 +0300 Subject: [PATCH] check userAgentData when available --- background/background.js | 5 +++++ background/context-menus.js | 6 +----- background/icon-manager.js | 6 +++--- edit/codemirror-default.js | 3 ++- js/dom.js | 6 ++---- js/prefs.js | 4 ++-- js/toolbox.js | 32 +++++++++++++++++++++++--------- manage/manage.js | 4 ++-- options/options.js | 4 ++-- 9 files changed, 42 insertions(+), 28 deletions(-) diff --git a/background/background.js b/background/background.js index c8481c9c..7c9168c9 100644 --- a/background/background.js +++ b/background/background.js @@ -10,6 +10,7 @@ /* global uswApi */ /* global FIREFOX + UA URLS activateTab download @@ -177,6 +178,10 @@ if (chrome.commands) { } chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => { + if (reason === 'install') { + if (UA.mobile) prefs.set('manage.newUI', false); + if (UA.windows) prefs.set('editor.keyMap', 'sublime'); + } if (reason === 'update') { const [a, b, c] = (previousVersion || '').split('.'); if (a <= 1 && b <= 5 && c <= 13) { // 1.5.13 diff --git a/background/context-menus.js b/background/context-menus.js index 53101dea..e0c50143 100644 --- a/background/context-menus.js +++ b/background/context-menus.js @@ -41,11 +41,7 @@ }; // "Delete" item in context menu for browsers that don't have it - if (CHROME && - // looking at the end of UA string - /(Vivaldi|Safari)\/[\d.]+$/.test(navigator.userAgent) && - // skip forks with Flash as those are likely to have the menu e.g. CentBrowser - !Array.from(navigator.plugins).some(p => p.name === 'Shockwave Flash')) { + if (CHROME) { prefs.__defaults['editor.contextDelete'] = true; } diff --git a/background/icon-manager.js b/background/icon-manager.js index ae255518..9b8d1f2d 100644 --- a/background/icon-manager.js +++ b/background/icon-manager.js @@ -2,17 +2,17 @@ /* global addAPI bgReady */// common.js /* global prefs */ /* global tabMan */ -/* global CHROME FIREFOX VIVALDI debounce ignoreChromeError */// toolbox.js +/* global CHROME FIREFOX UA debounce ignoreChromeError */// toolbox.js 'use strict'; /* exported iconMan */ const iconMan = (() => { - const ICON_SIZES = FIREFOX || CHROME >= 55 && !VIVALDI ? [16, 32] : [19, 38]; + const ICON_SIZES = FIREFOX || CHROME && !UA.vivaldi ? [16, 32] : [19, 38]; const staleBadges = new Set(); const imageDataCache = new Map(); const badgeOvr = {color: '', text: ''}; // https://github.com/openstyles/stylus/issues/1287 Fenix can't use custom ImageData - const FIREFOX_ANDROID = FIREFOX && navigator.userAgent.includes('Android'); + const FIREFOX_ANDROID = FIREFOX && UA.mobile; // https://github.com/openstyles/stylus/issues/335 let hasCanvas = FIREFOX_ANDROID ? false : loadImage(`/images/icon/${ICON_SIZES[0]}.png`) diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index 22523dcb..9ddef5ba 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -1,5 +1,6 @@ /* global $ */// dom.js /* global CodeMirror */ +/* global UA */// toolbox.js /* global editor */ /* global prefs */ /* global t */// localization.js @@ -62,7 +63,7 @@ if (!extras.includes('blockComment')) { KM.sublime['Shift-Ctrl-/'] = 'commentSelection'; } - if (navigator.appVersion.includes('Windows')) { + if (UA.windows) { // 'pcDefault' keymap on Windows should have F3/Shift-F3/Ctrl-R if (!extras.includes('findNext')) KM.pcDefault['F3'] = 'findNext'; if (!extras.includes('findPrev')) KM.pcDefault['Shift-F3'] = 'findPrev'; diff --git a/js/dom.js b/js/dom.js index 9d7671b5..155deae6 100644 --- a/js/dom.js +++ b/js/dom.js @@ -1,4 +1,4 @@ -/* global FIREFOX */// toolbox.js +/* global FIREFOX UA */// toolbox.js /* global prefs */ 'use strict'; @@ -468,9 +468,7 @@ const dom = {}; '/js/dom-on-load', ]; const elHtml = document.documentElement; - if (!/^Win\d+/.test(navigator.platform)) { - elHtml.classList.add('non-windows'); - } + if (!UA.windows) elHtml.classList.add('non-windows'); // set language for a) CSS :lang pseudo and b) hyphenation elHtml.setAttribute('lang', chrome.i18n.getUILanguage()); // set up header width resizer diff --git a/js/prefs.js b/js/prefs.js index cceae0a3..5a33f9bf 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -56,7 +56,7 @@ 'manage.backup.expanded': true, 'manage.filters.expanded': true, // the new compact layout doesn't look good on Android yet - 'manage.newUI': !navigator.appVersion.includes('Android'), + 'manage.newUI': true, 'manage.newUI.favicons': false, // show favicons for the sites in applies-to 'manage.newUI.faviconsGray': true, // gray out favicons 'manage.newUI.targets': 3, // max number of applies-to targets visible: 0 = none @@ -71,7 +71,7 @@ 'editor.smartIndent': true, // 'smart' indent 'editor.indentWithTabs': false, // smart indent with tabs 'editor.tabSize': 4, // tab width, in spaces - 'editor.keyMap': navigator.appVersion.indexOf('Windows') > 0 ? 'sublime' : 'default', + 'editor.keyMap': 'default', 'editor.theme': 'default', // CSS theme // CSS beautifier 'editor.beautify': { diff --git a/js/toolbox.js b/js/toolbox.js index 55bf3f46..336f9c71 100644 --- a/js/toolbox.js +++ b/js/toolbox.js @@ -3,6 +3,7 @@ /* exported CHROME_POPUP_BORDER_BUG RX_META + UA capitalize clamp closeCurrentTab @@ -23,10 +24,23 @@ waitForTabUrl */ -const CHROME = Boolean(chrome.app) && Number(navigator.userAgent.match(/Chrom\w+\/(\d+)|$/)[1]); -const OPERA = Boolean(chrome.app) && parseFloat(navigator.userAgent.match(/\bOPR\/(\d+\.\d+)|$/)[1]); -const VIVALDI = Boolean(chrome.app) && navigator.userAgent.includes('Vivaldi'); -let FIREFOX = !chrome.app && parseFloat(navigator.userAgent.match(/\bFirefox\/(\d+\.\d+)|$/)[1]); +let FIREFOX; +const [CHROME, UA] = (() => { + const uad = navigator.userAgentData; + const ua = uad || navigator.userAgent; + const brands = uad ? uad.brands.map(_ => `${_.brand}/${_.version}`).join(' ') : ua; + const getVer = name => Number(brands.match(new RegExp(name + '\\w*/(\\d+)|$'))[1]) || false; + FIREFOX = !chrome.app && getVer('Firefox'); + return [ + getVer('Chrom'), + { + mobile: uad ? uad.mobile : /Android/.test(ua), + windows: /Windows/.test(uad ? uad.platform : ua), + opera: getVer('(Opera|OPR)'), + vivaldi: getVer('Vivaldi'), + }, + ]; +})(); // see PR #781 const CHROME_POPUP_BORDER_BUG = CHROME >= 62 && CHROME <= 74; @@ -47,7 +61,7 @@ const URLS = { ownOrigin: chrome.runtime.getURL(''), configureCommands: - OPERA ? 'opera://settings/configureCommands' + UA.opera ? 'opera://settings/configureCommands' : 'chrome://extensions/configureCommands', installUsercss: chrome.runtime.getURL('install-usercss.html'), @@ -56,7 +70,7 @@ const URLS = { // https://cs.chromium.org/chromium/src/chrome/common/extensions/chrome_extensions_client.cc browserWebStore: FIREFOX ? 'https://addons.mozilla.org/' : - OPERA ? 'https://addons.opera.com/' : + UA.opera ? 'https://addons.opera.com/' : 'https://chrome.google.com/webstore/', emptyTab: [ @@ -124,11 +138,11 @@ const URLS = { const RX_META = /\/\*!?\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i; -if (FIREFOX || OPERA || VIVALDI) { +if (FIREFOX || UA.opera || UA.vivaldi) { document.documentElement.classList.add( FIREFOX && 'firefox' || - OPERA && 'opera' || - VIVALDI && 'vivaldi'); + UA.opera && 'opera' || + UA.vivaldi && 'vivaldi'); } // FF57+ supports openerTabId, but not in Android diff --git a/manage/manage.js b/manage/manage.js index bb171d09..c8621d36 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -1,5 +1,5 @@ /* global API msg */// msg.js -/* global CHROME VIVALDI debounce */// toolbox.js +/* global CHROME UA debounce */// toolbox.js /* global Events handleBulkChange handleVisibilityChange */// events.js /* global fitSelectBoxesIn switchUI showStyles */// render.js /* global prefs */ @@ -78,7 +78,7 @@ newUI.renderClass(); ].map(id => `--${id}:"${CSS.escape(t(id))}";`).join('') }}`); - if (!VIVALDI) { + if (!UA.vivaldi) { waitForSheet().then(() => { fitSelectBoxesIn($('#filters')); }); diff --git a/options/options.js b/options/options.js index 1590fa32..0fd6894e 100644 --- a/options/options.js +++ b/options/options.js @@ -14,7 +14,7 @@ CHROME CHROME_POPUP_BORDER_BUG FIREFOX - OPERA + UA URLS capitalize clamp @@ -34,7 +34,7 @@ if (CHROME_POPUP_BORDER_BUG) { } // collapse #advanced block in Chrome pre-66 (classic chrome://extensions UI) -if (!FIREFOX && !OPERA && CHROME < 66) { +if (!FIREFOX && !UA.opera && CHROME < 66) { const block = $('#advanced'); $('h1', block).onclick = event => { event.preventDefault();