Skip to content

Commit 9398dd0

Browse files
committed
feat: display restricted URL when page blocked
1 parent 5d53deb commit 9398dd0

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

src/content-script/limit/modal/components/Reason.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { t } from "@cs/locale"
22
import { useRequest } from "@hooks/useRequest"
33
import Flex from "@pages/components/Flex"
4-
import { meetLimit, meetTimeLimit, period2Str } from "@util/limit"
4+
import { matchCond, meetLimit, meetTimeLimit, period2Str } from "@util/limit"
55
import { formatPeriodCommon, MILL_PER_SECOND } from "@util/time"
66
import { ElDescriptions, ElDescriptionsItem, ElTag } from "element-plus"
77
import { computed, defineComponent } from "vue"
8-
import { useReason, useRule } from "../context"
8+
import { useGlobalParam, useReason, useRule } from "../context"
9+
10+
const renderBaseItems = (rule: timer.limit.Rule | null, url: string) => <>
11+
<ElDescriptionsItem label={t(msg => msg.limit.item.name)} labelAlign="right">
12+
{rule?.name ?? '-'}
13+
</ElDescriptionsItem>
14+
<ElDescriptionsItem label={t(msg => msg.limit.item.condition)} labelAlign='right'>
15+
{matchCond(rule?.cond ?? [], url)?.join(', ')}
16+
</ElDescriptionsItem>
17+
</>
918

1019
const TimeDescriptions = defineComponent({
1120
props: {
@@ -21,15 +30,14 @@ const TimeDescriptions = defineComponent({
2130
setup(props) {
2231
const rule = useRule()
2332
const reason = useReason()
33+
const { url } = useGlobalParam()
2434

2535
const timeLimited = computed(() => meetTimeLimit(props.time ?? 0, props.waste ?? 0, !!reason.value?.allowDelay, reason.value?.delayCount ?? 0))
2636
const visitLimited = computed(() => meetLimit(props.count ?? 0, props.visit ?? 0))
2737

2838
return () => (
2939
<ElDescriptions border column={1} labelWidth={130}>
30-
<ElDescriptionsItem label={t(msg => msg.limit.item.name)} labelAlign="right">
31-
{rule.value?.name ?? '-'}
32-
</ElDescriptionsItem>
40+
{renderBaseItems(rule.value, url)}
3341
<ElDescriptionsItem label={props.ruleLabel} labelAlign="right">
3442
<Flex gap={5} width={200}>
3543
<ElTag v-show={!!props.time}>{formatPeriodCommon((props.time ?? 0) * MILL_PER_SECOND)}</ElTag>
@@ -67,6 +75,7 @@ const TimeDescriptions = defineComponent({
6775
const _default = defineComponent(() => {
6876
const reason = useReason()
6977
const rule = useRule()
78+
const { url } = useGlobalParam()
7079
const type = computed(() => reason.value?.type)
7180

7281
const { data: browsingTime, refresh: refreshBrowsingTime } = useRequest(() => {
@@ -98,9 +107,7 @@ const _default = defineComponent(() => {
98107
dataLabel={t(msg => msg.calendar.range.thisWeek)}
99108
/>
100109
<ElDescriptions border column={1} v-show={type.value === 'VISIT'}>
101-
<ElDescriptionsItem label={t(msg => msg.limit.item.name)} labelAlign="right">
102-
{rule.value?.name || '-'}
103-
</ElDescriptionsItem>
110+
{renderBaseItems(rule.value, url)}
104111
<ElDescriptionsItem label={t(msg => msg.limit.item.visitTime)} labelAlign="right">
105112
{formatPeriodCommon((rule.value?.visitTime ?? 0) * MILL_PER_SECOND) || '-'}
106113
</ElDescriptionsItem>
@@ -114,9 +121,7 @@ const _default = defineComponent(() => {
114121
</ElDescriptionsItem>
115122
</ElDescriptions>
116123
<ElDescriptions border column={1} v-show={type.value === 'PERIOD'}>
117-
<ElDescriptionsItem label={t(msg => msg.limit.item.name)} labelAlign="right">
118-
{rule.value?.name || '-'}
119-
</ElDescriptionsItem>
124+
{renderBaseItems(rule.value, url)}
120125
<ElDescriptionsItem label={t(msg => msg.limit.item.period)} labelAlign="right">
121126
{
122127
rule.value?.periods?.length

src/content-script/limit/modal/context.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ import { type LimitReason } from "../common"
66

77
const REASON_KEY = "display_reason"
88
const RULE_KEY = "display_rule"
9+
const GLOBAL_KEY = "delay_global"
910
const DELAY_HANDLER_KEY = 'delay_handler'
1011

12+
type GlobalParam = {
13+
url: string
14+
}
15+
16+
export const provideGlobalParam = (app: App<Element>, gp: GlobalParam) => {
17+
app.provide(GLOBAL_KEY, gp)
18+
}
19+
20+
export const useGlobalParam = () => inject(GLOBAL_KEY) as GlobalParam
21+
1122
export const provideReason = (app: App<Element>): Ref<LimitReason | undefined> => {
1223
const reason = ref<LimitReason>()
1324
app.provide(REASON_KEY, reason)

src/content-script/limit/modal/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createApp, Ref, type App } from 'vue'
55
import { exitFullscreen, isSameReason, type LimitReason, type MaskModal } from '../common'
66
import { TAG_NAME, type RootElement } from '../element'
77
import Main from './Main'
8-
import { provideDelayHandler, provideReason } from './context'
8+
import { provideDelayHandler, provideGlobalParam, provideReason } from './context'
99

1010
function pauseAllVideo(): void {
1111
const elements = document?.getElementsByTagName('video')
@@ -156,6 +156,7 @@ class ModalInstance implements MaskModal {
156156
private initApp() {
157157
this.app = createApp(Main)
158158
this.reason = provideReason(this.app)
159+
provideGlobalParam(this.app, { url: this.url })
159160
provideDelayHandler(this.app, () => this.delayHandlers?.forEach(h => h?.()))
160161
this.body && this.app.mount(this.body)
161162
}

src/util/limit.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ export const cleanCond = (origin: string | undefined): string | undefined => {
1414
return res || undefined
1515
}
1616

17+
const matchUrl = (cond: string, url: string): boolean => {
18+
return new RegExp(`^.*//${cond.split('*').join('.*')}`).test(url)
19+
}
20+
1721
export function matches(cond: timer.limit.Item['cond'], url: string): boolean {
18-
return cond?.some?.(
19-
c => new RegExp(`^.*//${(c || '').split('*').join('.*')}`).test(url)
20-
)
22+
return cond.some(c => matchUrl(c, url))
23+
}
24+
25+
export function matchCond(cond: timer.limit.Item['cond'], url: string): string[] {
26+
return cond.filter(c => matchUrl(c, url))
2127
}
2228

2329
export const meetLimit = (limit: number | undefined, value: number | undefined): boolean => {

test/util/limit.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { calcTimeState, cleanCond, dateMinute2Idx, hasLimited, hasWeeklyLimited, isEffective, isEnabledAndEffective, matches, meetLimit, meetTimeLimit, period2Str } from "@util/limit"
1+
import {
2+
calcTimeState, cleanCond, dateMinute2Idx, hasLimited, hasWeeklyLimited, isEffective, isEnabledAndEffective,
3+
matchCond, matches, meetLimit, meetTimeLimit, period2Str
4+
} from "@util/limit"
25

36
describe('util/limit', () => {
47
test('cleanCond', () => {
@@ -18,6 +21,13 @@ describe('util/limit', () => {
1821
expect(matches(cond, 'http://github.com')).toBe(false)
1922
})
2023

24+
test('matchCond', () => {
25+
const cond = ['www.baidu.com', '*.google.com', 'github.com/sheepzh', 'github.com']
26+
expect(matchCond(cond, 'http://www.baidu.com')).toEqual(['www.baidu.com'])
27+
expect(matchCond(cond, 'https://github.com/sheepzh/time-tracker-for-browser')).toEqual(['github.com/sheepzh', 'github.com'])
28+
expect(matchCond(cond, 'https://www.github.com')).toEqual([])
29+
})
30+
2131
test('meetLimit', () => {
2232
expect(meetLimit(undefined, undefined)).toBe(false)
2333
expect(meetLimit(1, undefined)).toBe(false)

0 commit comments

Comments
 (0)