From 0bff67c95f6b5e8c701acd97e8ac0f003df059a3 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 3 Nov 2020 02:55:32 +0000 Subject: [PATCH] extension: use browser name instead of somewhat cryptic 'local' thanks @koo5 for the idea (https://github.com/karlicoss/promnesia/pull/147) --- extension/src/background.js | 11 ++++------- extension/src/common.js | 15 +++++++++++++++ extension/src/options.js | 4 ++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/extension/src/background.js b/extension/src/background.js index 3930b8d0..dea03751 100644 --- a/extension/src/background.js +++ b/extension/src/background.js @@ -2,7 +2,7 @@ import type {Locator, Src, Url, Second, JsonArray, JsonObject, AwareDate, NaiveDate} from './common'; import {Visit, Visits, Blacklisted, unwrap, Methods, ldebug, linfo, lerror, lwarn} from './common'; -import {get_options_async, setOptions} from './options'; +import {get_options_async, setOptions, THIS_BROWSER_TAG} from './options'; import {chromeTabsExecuteScriptAsync, chromeTabsInsertCSS, chromeTabsQueryAsync, chromeRuntimeGetPlatformInfo, chromeTabsGet} from './async_chrome'; import {showTabNotification, showBlackListedNotification, showIgnoredNotification, defensify, notify} from './notifications'; import {Blacklist} from './blacklist' @@ -103,9 +103,6 @@ function getDelayMs(/*url*/) { return 10 * 1000; } -// TODO: make it configurable in options? -const LOCAL_TAG = 'local'; - async function getBrowserVisits(url: Url): Promise { const android = await isAndroid(); @@ -133,7 +130,7 @@ async function getBrowserVisits(url: Url): Promise { normalise_url(url), ((t: any): AwareDate), ((t: any): NaiveDate), // there is no TZ info in history anyway, so not much else we can do - [LOCAL_TAG], + [THIS_BROWSER_TAG], )); return new Visits(url, url, visits); } @@ -222,10 +219,10 @@ function getIconStyle(visits: Result): IconStyle { return {icon: 'images/ic_relatives_48.png' , title: `${vcount} visits, ${ctext}`, text: btext}; } // TODO a bit ugly, but ok for now.. maybe cut off by time? - const boring = visits.visits.every(v => v.tags.length == 1 && v.tags[0] == LOCAL_TAG); + const boring = visits.visits.every(v => v.tags.length == 1 && v.tags[0] == THIS_BROWSER_TAG); if (boring) { // TODO not sure if really worth distinguishing.. - return {icon: "images/ic_boring_48.png" , title: `${vcount} visits (${LOCAL_TAG} only)`, text: ''}; + return {icon: "images/ic_boring_48.png" , title: `${vcount} visits (${THIS_BROWSER_TAG} only)`, text: ''}; } else { return {icon: "images/ic_blue_48.png" , title: `${vcount} visits`, text: ''}; } diff --git a/extension/src/common.js b/extension/src/common.js index 6b81b3a0..05010bb4 100644 --- a/extension/src/common.js +++ b/extension/src/common.js @@ -166,3 +166,18 @@ export function safeSetInnerHTML(element: HTMLElement, html: string) { export type JsonArray = Array export type JsonObject = $Shape<{ [string]: any }> export type Json = JsonArray | JsonObject + + +export function getBrowser(): string { + // https://stackoverflow.com/questions/12489546/getting-a-browsers-name-client-side + const agent = window.navigator.userAgent.toLowerCase() + switch (true) { + case agent.indexOf("chrome") > -1 && !! window.chrome: return "chrome"; + case agent.indexOf("firefox") > -1 : return "firefox"; + case agent.indexOf("safari") > -1 : return "safari"; + case agent.indexOf("edge") > -1 : return "edge"; + case agent.indexOf("opr") > -1 && !!window.opr : return "opera"; + case agent.indexOf("trident") > -1 : return "ie"; + default: return "browser"; + } +} diff --git a/extension/src/options.js b/extension/src/options.js index f990b55d..2d0b3fbf 100644 --- a/extension/src/options.js +++ b/extension/src/options.js @@ -1,5 +1,6 @@ /* @flow */ import type {Src} from './common'; +import {getBrowser} from './common' // $FlowFixMe import OptionsSync from 'webext-options-sync'; @@ -44,6 +45,9 @@ export const USE_ORIGINAL_TZ = true; export const GROUP_CONSECUTIVE_SECONDS = 20 * 60; +// TODO: make it configurable in options? +export const THIS_BROWSER_TAG = getBrowser() + // TODO allow to export settings // https://github.com/fregante/webext-options-sync/issues/23 function defaultOptions(): Options {