Skip to content

Commit 7294e0f

Browse files
author
sheepzh
committed
Support to build for Safari (#148)
1 parent 3f1a1b9 commit 7294e0f

File tree

8 files changed

+86
-25
lines changed

8 files changed

+86
-25
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
node_modules
22
dist
33
dist_dev
4+
dist_dev_safari
45
dist_prod
56
dist_analyze
67

8+
Timer_Safari_DEV
9+
710
firefox_dev
811

912
market_packages

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"homepage": "https://github.com/sheepzh/timer",
66
"scripts": {
77
"dev": "webpack --config=webpack/webpack.dev.ts --watch",
8+
"dev:safari": "webpack --config=webpack/webpack.dev.safari.ts --watch",
89
"build": "webpack --config=webpack/webpack.prod.ts",
910
"analyze": "webpack --config=webpack/webpack.analyze.ts",
1011
"test": "jest --env=jsdom",

src/app/components/option/components/statistics.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { defaultStatistics } from "@util/constant/option"
1313
import { defineComponent, h, reactive, unref } from "vue"
1414
import { t } from "@app/locale"
1515
import { renderOptionItem, tagText, tooltip } from "../common"
16+
import { IS_SAFARI } from "@util/constant/environment"
1617

1718
function updateOptionVal(key: keyof timer.option.StatisticsOption, newVal: boolean, option: UnwrapRef<timer.option.StatisticsOption>) {
1819
option[key] = newVal
@@ -40,6 +41,35 @@ function copy(target: timer.option.StatisticsOption, source: timer.option.Statis
4041
target.countLocalFiles = source.countLocalFiles
4142
}
4243

44+
function renderOptionItems(option: timer.option.StatisticsOption) {
45+
const result = []
46+
if (!IS_SAFARI) {
47+
// chrome.idle does not work in Safari, so not to display this option
48+
result.push(
49+
renderOptionItem({
50+
input: countWhenIdle(option),
51+
idleTime: tagText(msg => msg.option.statistics.idleTime),
52+
info: tooltip(msg => msg.option.statistics.idleTimeInfo)
53+
}, msg => msg.statistics.countWhenIdle, t(msg => msg.option.no)),
54+
h(ElDivider)
55+
)
56+
}
57+
result.push(
58+
renderOptionItem({
59+
input: countLocalFiles(option),
60+
localFileTime: tagText(msg => msg.option.statistics.localFileTime),
61+
info: tooltip(msg => msg.option.statistics.localFilesInfo)
62+
}, msg => msg.statistics.countLocalFiles, t(msg => msg.option.no)),
63+
h(ElDivider),
64+
renderOptionItem({
65+
input: collectSiteName(option),
66+
siteName: tagText(msg => msg.option.statistics.siteName),
67+
siteNameUsage: tooltip(msg => msg.option.statistics.siteNameUsage)
68+
}, msg => msg.statistics.collectSiteName, t(msg => msg.option.yes))
69+
)
70+
return result
71+
}
72+
4373
const _default = defineComponent({
4474
name: "StatisticsOptionContainer",
4575
setup(_props, ctx) {
@@ -51,25 +81,7 @@ const _default = defineComponent({
5181
await optionService.setStatisticsOption(unref(option))
5282
}
5383
})
54-
return () => h('div', [
55-
renderOptionItem({
56-
input: countWhenIdle(option),
57-
idleTime: tagText(msg => msg.option.statistics.idleTime),
58-
info: tooltip(msg => msg.option.statistics.idleTimeInfo)
59-
}, msg => msg.statistics.countWhenIdle, t(msg => msg.option.no)),
60-
h(ElDivider),
61-
renderOptionItem({
62-
input: countLocalFiles(option),
63-
localFileTime: tagText(msg => msg.option.statistics.localFileTime),
64-
info: tooltip(msg => msg.option.statistics.localFilesInfo)
65-
}, msg => msg.statistics.countLocalFiles, t(msg => msg.option.no)),
66-
h(ElDivider),
67-
renderOptionItem({
68-
input: collectSiteName(option),
69-
siteName: tagText(msg => msg.option.statistics.siteName),
70-
siteNameUsage: tooltip(msg => msg.option.statistics.siteNameUsage)
71-
}, msg => msg.statistics.collectSiteName, t(msg => msg.option.yes))
72-
])
84+
return () => h('div', renderOptionItems(option))
7385
}
7486
})
7587

src/background/timer/idle-listener.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8+
import { IS_SAFARI } from "@util/constant/environment"
89
import { formatTime } from "@util/time"
910
import TimerContext from "./context"
1011

@@ -24,6 +25,9 @@ export default class IdleListener {
2425
}
2526

2627
listen() {
27-
chrome.idle.onStateChanged.addListener(newState => listen(this.context, newState))
28+
if (!IS_SAFARI) {
29+
// Idle does not work in macOs
30+
chrome.idle.onStateChanged.addListener(newState => listen(this.context, newState))
31+
}
2832
}
2933
}

src/manifest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ const _default: chrome.runtime.ManifestV2 = {
3434
},
3535
content_scripts: [
3636
{
37-
"matches": [
37+
matches: [
3838
"<all_urls>"
3939
],
40-
"js": [
40+
js: [
4141
"content_scripts.js"
4242
],
43-
"run_at": "document_start"
43+
run_at: "document_start"
4444
}
4545
],
4646
permissions: [

src/util/constant/environment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let isFirefox = false
1010
let isChrome = false
1111
let isEdge = false
1212
let isOpera = false
13+
let isSafari = false
1314

1415
if (/Firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
1516
isFirefox = true
@@ -19,6 +20,8 @@ if (/Firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
1920
} else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
2021
// The Opera implements the chrome
2122
isOpera = true
23+
} else if (userAgent.includes('Safari')) {
24+
isSafari = true
2225
} else if (userAgent.includes('Chrome')) {
2326
isChrome = true
2427
}
@@ -32,4 +35,9 @@ export const IS_CHROME: boolean = isChrome
3235
/**
3336
* @since 0.8.0
3437
*/
35-
export const IS_OPERA: boolean = isOpera
38+
export const IS_OPERA: boolean = isOpera
39+
40+
/**
41+
* @since 1.3.0
42+
*/
43+
export const IS_SAFARI: boolean = isSafari

webpack/webpack.dev.safari.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import path from "path"
2+
import optionGenerator from "./webpack.common"
3+
4+
const outputDir = path.join(__dirname, '..', 'dist_dev_safari')
5+
6+
function removeUnsupportedProperties(manifest: Partial<chrome.runtime.ManifestV2>) {
7+
// 1. permissions. 'idle' is not supported
8+
const originPermissions = manifest.permissions || []
9+
const unsupported = ['idle']
10+
const supported = []
11+
originPermissions.forEach(perm => !unsupported.includes(perm) && supported.push(perm))
12+
manifest.permissions = supported
13+
}
14+
15+
const options = optionGenerator(
16+
outputDir,
17+
baseManifest => {
18+
baseManifest.name = 'Timer_Safari_DEV'
19+
// Remove unsupported properties in Safari
20+
removeUnsupportedProperties(baseManifest)
21+
}
22+
)
23+
24+
options.mode = 'development'
25+
options.output.path = outputDir
26+
27+
// no eval with development, but generate *.map.js
28+
options.devtool = 'cheap-module-source-map'
29+
30+
// Use cache with filesystem
31+
options.cache = { type: 'filesystem' }
32+
33+
export default options

webpack/webpack.dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ options.devtool = 'cheap-module-source-map'
5656
// Use cache with filesystem
5757
options.cache = { type: 'filesystem' }
5858

59-
module.exports = options
59+
export default options

0 commit comments

Comments
 (0)