Skip to content

Commit 289c702

Browse files
author
sheepzh
committed
Fix some bugs in Safari (#152)
1 parent 7294e0f commit 289c702

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/background/badge-text-manager.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,29 @@ async function updateFocus(host?: string) {
5050
if (!host) {
5151
host = await findActiveHost()
5252
}
53-
const milliseconds = host ? (await timerDb.get(host, new Date)).focus : undefined
53+
const milliseconds = host ? (await timerDb.get(host, new Date())).focus : undefined
5454
setBadgeText(milliseconds)
5555
}
5656

57+
const ALARM_NAME = 'timer-badge-text-manager-alarm'
58+
const ALARM_INTERVAL = 1000
59+
function createAlarm(beforeAction?: () => void) {
60+
beforeAction?.()
61+
chrome.alarms.create(ALARM_NAME, { when: Date.now() + ALARM_INTERVAL })
62+
}
63+
5764
class BadgeTextManager {
5865
timer: NodeJS.Timer
5966
isPaused: boolean
6067

6168
async init() {
62-
this.timer = setInterval(() => !this.isPaused && updateFocus(), 1000)
69+
createAlarm()
70+
chrome.alarms.onAlarm.addListener(alarm => {
71+
if (ALARM_NAME === alarm.name) {
72+
createAlarm(() => !this.isPaused && updateFocus())
73+
}
74+
})
75+
// this.timer = setInterval(() => !this.isPaused && updateFocus(), 1000)
6376

6477
const option: Partial<timer.option.AllOption> = await optionService.getAllOption()
6578
this.pauseOrResumeAccordingToOption(!!option.displayBadgeText)

src/background/browser-action-menu-manager.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Copyright (c) 2021 Hengyang Zhang
33
*
44
* This software is released under the MIT License.
@@ -8,6 +8,7 @@
88
import { OPTION_ROUTE } from "../app/router/constants"
99
import { getAppPageUrl, SOURCE_CODE_PAGE, TU_CAO_PAGE } from "@util/constant/url"
1010
import { t2Chrome } from "@util/i18n/chrome/t"
11+
import { IS_SAFARI } from "@util/constant/environment"
1112

1213
const APP_PAGE_URL = getAppPageUrl(true)
1314

@@ -19,30 +20,39 @@ const baseProps: Partial<chrome.contextMenus.CreateProperties> = {
1920
visible: true
2021
}
2122

23+
function titleOf(prefixEmoji: string, title: string) {
24+
if (IS_SAFARI) {
25+
// Emoji does not work in Safari's context menu
26+
return title
27+
} else {
28+
return `${prefixEmoji} ${title}`
29+
}
30+
}
31+
2232
const allFunctionProps: chrome.contextMenus.CreateProperties = {
2333
id: chrome.runtime.id + '_timer_menu_item_app_link',
24-
title: '🏷️ ' + t2Chrome(msg => msg.contextMenus.allFunctions),
34+
title: titleOf('🏷️', t2Chrome(msg => msg.contextMenus.allFunctions)),
2535
onclick: () => chrome.tabs.create({ url: APP_PAGE_URL }),
2636
...baseProps
2737
}
2838

2939
const optionPageProps: chrome.contextMenus.CreateProperties = {
3040
id: chrome.runtime.id + '_timer_menu_item_option_link',
31-
title: '🥰 ' + t2Chrome(msg => msg.contextMenus.optionPage),
41+
title: titleOf('🥰', t2Chrome(msg => msg.contextMenus.optionPage)),
3242
onclick: () => chrome.tabs.create({ url: APP_PAGE_URL + '#' + OPTION_ROUTE }),
3343
...baseProps
3444
}
3545

3646
const repoPageProps: chrome.contextMenus.CreateProperties = {
3747
id: chrome.runtime.id + '_timer_menu_item_repo_link',
38-
title: '🍻 ' + t2Chrome(msg => msg.contextMenus.repoPage),
48+
title: titleOf('🍻', t2Chrome(msg => msg.contextMenus.repoPage)),
3949
onclick: () => chrome.tabs.create({ url: SOURCE_CODE_PAGE }),
4050
...baseProps
4151
}
4252

4353
const feedbackPageProps: chrome.contextMenus.CreateProperties = {
4454
id: chrome.runtime.id + '_timer_menu_item_feedback_link',
45-
title: '😿 ' + t2Chrome(msg => msg.contextMenus.feedbackPage),
55+
title: titleOf('😿', t2Chrome(msg => msg.contextMenus.feedbackPage)),
4656
onclick: () => chrome.tabs.create({ url: TU_CAO_PAGE }),
4757
...baseProps
4858
}

src/util/constant/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ if (/Firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
2020
} else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
2121
// The Opera implements the chrome
2222
isOpera = true
23-
} else if (userAgent.includes('Safari')) {
23+
} else if (userAgent.includes('Safari') && !userAgent.includes('Chrome')) {
24+
// Chrome on macOs includes 'Safari'
2425
isSafari = true
2526
} else if (userAgent.includes('Chrome')) {
2627
isChrome = true

src/util/pattern.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function isBrowserUrl(url: string) {
1818
// Firefox addons' pages
1919
|| /^moz-extension:/.test(url)
2020
|| /^edge.*?:\/\/.*$/.test(url)
21+
|| /^safari.*?:\/\/.*/.test(url)
2122
}
2223

2324
const isNotValidPort = (portStr: string) => {

0 commit comments

Comments
 (0)