Skip to content

Commit 4b430ca

Browse files
committed
Support to count the time spent on local files (sheepzh#65)
1 parent 72d4a7f commit 4b430ca

File tree

13 files changed

+162
-7
lines changed

13 files changed

+162
-7
lines changed

global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ declare namespace Timer {
5959
* @since 0.5.0
6060
*/
6161
collectSiteName: boolean
62+
/**
63+
* Whether to count the local files
64+
* @since 0.7.0
65+
*/
66+
countLocalFiles: boolean
6267
}
6368

6469
type Option = PopupOption & AppearanceOption & StatisticsOption

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "timer",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "Web timer",
55
"homepage": "https://github.com/sheepzh/timer",
66
"scripts": {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const countWhenIdle = () => h(ElSwitch, {
2626
onChange: (newVal: boolean) => updateOptionVal('countWhenIdle', newVal)
2727
})
2828

29+
const countLocalFiles = () => h(ElSwitch, {
30+
modelValue: optionRef.value.countLocalFiles,
31+
onChange: (newVal: boolean) => updateOptionVal("countLocalFiles", newVal)
32+
})
33+
2934
const collectSiteName = () => h(ElSwitch, {
3035
modelValue: optionRef.value.collectSiteName,
3136
onChange: (newVal: boolean) => updateOptionVal('collectSiteName', newVal)
@@ -38,6 +43,12 @@ const options = () => [
3843
info: tooltip(msg => msg.option.statistics.idleTimeInfo)
3944
}, msg => msg.statistics.countWhenIdle, t(msg => msg.option.no)),
4045
h(ElDivider),
46+
renderOptionItem({
47+
input: countLocalFiles(),
48+
localFileTime: tagText(msg => msg.option.statistics.localFilesTime),
49+
info: tooltip(msg => msg.option.statistics.localFilesInfo)
50+
}, msg => msg.statistics.countLocalFiles, t(msg => msg.option.no)),
51+
h(ElDivider),
4152
renderOptionItem({
4253
input: collectSiteName(),
4354
siteName: tagText(msg => msg.option.statistics.siteName),

src/app/components/report/table/columns/host-merged-alert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { defineComponent, h } from "vue"
1111
/**
1212
* Merged host column
1313
*
14-
* @since 0.6.2
14+
* @since 0.7.0
1515
*/
1616
const _default = defineComponent({
1717
name: "HostMergedAlert",

src/app/components/whitelist/components/item-input.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { t } from "@app/locale"
99
import { Check, Close } from "@element-plus/icons"
10+
import { isRemainHost } from "@util/constant/remain-host"
1011
import { isValidHost } from "@util/pattern"
1112
import { ElButton, ElInput, ElMessage } from "element-plus"
1213
import { defineComponent, Ref, ref, h } from "vue"
@@ -47,7 +48,9 @@ const _default = defineComponent({
4748
size: 'small',
4849
icon: Check,
4950
class: 'item-check-button editable-item',
50-
onClick: () => isValidHost(white.value) ? ctx.emit("saved", white.value) : ElMessage.warning(invalidTxt)
51+
onClick: () => (isRemainHost(white.value) || isValidHost(white.value))
52+
? ctx.emit("saved", white.value)
53+
: ElMessage.warning(invalidTxt)
5154
})
5255
])
5356
}

src/app/locale/components/option.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export type OptionMessage = {
3434
countWhenIdle: string
3535
idleTime: string
3636
idleTimeInfo: string
37+
countLocalFiles: string
38+
localFilesTime: string
39+
localFilesInfo: string
3740
collectSiteName: string
3841
siteNameUsage: string
3942
siteName: string
@@ -68,6 +71,9 @@ const _default: Messages<OptionMessage> = {
6871
countWhenIdle: '{input} 是否统计 {idleTime} {info}',
6972
idleTime: '休眠时间',
7073
idleTimeInfo: '长时间不操作(比如全屏观看视频),浏览器会自动进入休眠状态',
74+
countLocalFiles: '{input} 是否统计使用浏览器 {localFileTime} {info}',
75+
localFilesTime: '阅读本地文件的时间',
76+
localFilesInfo: '支持 PDF、图片、txt 以及 json 等格式',
7177
collectSiteName: '{input} 访问网站主页时,是否自动收集 {siteName} {siteNameUsage}',
7278
siteName: '网站的名称',
7379
siteNameUsage: '数据只存放在本地,将代替域名用于展示,增加辨识度。当然您可以自定义每个网站的名称'
@@ -100,6 +106,9 @@ const _default: Messages<OptionMessage> = {
100106
countWhenIdle: '{input} Whether to count {idleTime} {info}',
101107
idleTime: 'idle time',
102108
idleTimeInfo: 'If you do not operate for a long time (such as watching a video in full screen), the browser will automatically enter the idle state',
109+
countLocalFiles: '{input} Whether to count the time spent {localFilesTime} {info} in the browser',
110+
localFilesTime: ' reading the local file ',
111+
localFilesInfo: 'Contains formats such as PDF, image, txt and json',
103112
collectSiteName: '{input} Whether to automatically collect the name of the website when visiting the homepage of the website',
104113
siteName: 'Site Name',
105114
siteNameUsage: 'The data only exists locally and will be used for display instead of the host to increase recognition.'
@@ -135,6 +144,9 @@ const _default: Messages<OptionMessage> = {
135144
countWhenIdle: '{input} Whether to count {idleTime} {info}',
136145
idleTime: 'idle time',
137146
idleTimeInfo: 'If you do not operate for a long time (such as watching a video in full screen), the browser will automatically enter the idle state',
147+
countLocalFiles: '{input} Whether to count the time spent {localFilesTime} {info} in the browser',
148+
localFilesTime: ' reading the local file ',
149+
localFilesInfo: 'Contains formats such as PDF, image, txt and json',
138150
collectSiteName: '{input} Whether to automatically collect the name of the website when visiting the homepage of the website',
139151
siteName: 'site name',
140152
siteNameUsage: 'The data only exists locally and will be used for display instead of the host to increase recognition.'

src/background/timer/collector.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8-
import { isBrowserUrl, extractHostname } from "@util/pattern"
8+
import { isBrowserUrl, extractHostname, extractFileHost } from "@util/pattern"
99
import CollectionContext from "./collection-context"
10+
import optionService from "@service/option-service"
11+
12+
let countLocalFiles: boolean
13+
optionService.getAllOption().then(option => countLocalFiles = !!option.countLocalFiles)
14+
optionService.addOptionChangeListener((newVal => countLocalFiles = !!newVal.countLocalFiles))
1015

1116
/**
1217
* The promise for window query
@@ -29,7 +34,11 @@ function handleTab(tab: chrome.tabs.Tab, isFocusWindow: boolean, context: Collec
2934
const url = tab.url
3035
if (!url) return
3136
if (isBrowserUrl(url)) return
32-
const host = extractHostname(url).host
37+
let host = extractHostname(url).host
38+
if (!host && countLocalFiles) {
39+
// Not host, try to detect the local files
40+
host = extractFileHost(url)
41+
}
3342
if (host) {
3443
context.collectHost(host)
3544
const isFocus = isFocusWindow && tab.active
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2022 Hengyang Zhang
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
import MergeRuleDatabase from "@db/merge-rule-database"
9+
import HostAliasDatabase from "@db/host-alias-database"
10+
import { JSON_HOST, LOCAL_HOST_PATTERN, MERGED_HOST, PDF_HOST, PIC_HOST, TXT_HOST } from "@util/constant/remain-host"
11+
import IVersionProcessor from "../i-version-processor"
12+
import { HostAliasSource } from "@entity/dao/host-alias"
13+
14+
const storage: chrome.storage.StorageArea = chrome.storage.local
15+
16+
const mergeRuleDatabase = new MergeRuleDatabase(storage)
17+
const hostAliasDatabase = new HostAliasDatabase(storage)
18+
19+
/**
20+
* Process the host of local files
21+
*
22+
* @since 0.7.0
23+
*/
24+
export default class LocalFileInitializer implements IVersionProcessor {
25+
since(): string {
26+
return '0.7.0'
27+
}
28+
29+
process(): void {
30+
// Add merged rules
31+
32+
mergeRuleDatabase.add({
33+
origin: LOCAL_HOST_PATTERN,
34+
merged: MERGED_HOST,
35+
}).then(() => console.log('Local file merge rules initialized'))
36+
// Add site name
37+
hostAliasDatabase.update({
38+
host: PDF_HOST,
39+
name: "PDF 文件",
40+
source: HostAliasSource.DETECTED
41+
})
42+
hostAliasDatabase.update({
43+
host: JSON_HOST,
44+
name: "JSON 文件",
45+
source: HostAliasSource.DETECTED
46+
})
47+
hostAliasDatabase.update({
48+
host: PIC_HOST,
49+
name: "图片文件",
50+
source: HostAliasSource.DETECTED
51+
})
52+
hostAliasDatabase.update({
53+
host: TXT_HOST,
54+
name: "文本文件",
55+
source: HostAliasSource.DETECTED
56+
})
57+
}
58+
}

src/background/version-manager/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import IVersionProcessor from "./i-version-processor"
99
import HostMergeInitializer from "./0-1-2/host-merge-initializer"
10+
import LocalFileInitializer from "./0-7-0/local-file-initializer"
1011

1112
/**
1213
* Version manager
@@ -17,7 +18,7 @@ class VersionManager {
1718
processorChain: IVersionProcessor[] = []
1819

1920
constructor() {
20-
this.processorChain.push(new HostMergeInitializer())
21+
this.processorChain.push(new HostMergeInitializer(), new LocalFileInitializer())
2122
this.processorChain = this.processorChain.sort((a, b) => a.since() >= b.since() ? 1 : 0)
2223
}
2324

src/util/constant/option.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function defaultAppearance(): Timer.AppearanceOption {
2828
export function defaultStatistics(): Timer.StatisticsOption {
2929
return {
3030
countWhenIdle: false,
31-
collectSiteName: true
31+
collectSiteName: true,
32+
countLocalFiles: false
3233
}
3334
}

0 commit comments

Comments
 (0)