Skip to content

Commit

Permalink
Support pattern for whitelist (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepzh committed Dec 9, 2024
1 parent f7954b2 commit 69a65d3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timer",
"version": "2.5.6",
"version": "2.5.7",
"description": "Time tracker",
"homepage": "https://www.wfhg.cc",
"scripts": {
Expand Down Expand Up @@ -82,4 +82,4 @@
"engines": {
"node": ">=20"
}
}
}
4 changes: 2 additions & 2 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import initLimitProcessor from "./limit-processor"
import MessageDispatcher from "./message-dispatcher"
import initSidePanel from "./side-panel"
import initTrackServer from "./timer/server"
import VersionManager from "./version-manager"
import VersionMigrator from "./migrator"
import WhitelistMenuManager from "./whitelist-menu-manager"

// Open the log of console
Expand All @@ -40,7 +40,7 @@ initCsHandler(messageDispatcher)
initTrackServer(messageDispatcher)

// Process version
new VersionManager().init()
new VersionMigrator().init()

// Backup scheduler
new BackupScheduler().init()
Expand Down
17 changes: 17 additions & 0 deletions src/background/migrator/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

/**
* Processor for version
*
* @since 0.1.2
*/
export interface Migrator {
onInstall(): void

onUpdate(version: string): void
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import MergeRuleDatabase from "@db/merge-rule-database"
import { Migrator } from "./common"

const mergeRuleDatabase = new MergeRuleDatabase(chrome.storage.local)

Expand All @@ -14,13 +15,11 @@ const mergeRuleDatabase = new MergeRuleDatabase(chrome.storage.local)
*
* Initialize the merge rules
*/
export default class HostMergeInitializer implements VersionProcessor {

since(): string {
return '0.1.2'
export default class HostMergeInitializer implements Migrator {
onUpdate(_version: string): void {
}

process(): void {
onInstall(): void {
mergeRuleDatabase.add(
// Google's regional hosts
{ origin: '*.google.com.*', merged: 'google.com' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
/**
* Copyright (c) 2021 Hengyang Zhang
*
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

import { getVersion, onInstalled } from "@api/chrome/runtime"
import HostMergeInitializer from "./0-1-2/host-merge-initializer"
import LocalFileInitializer from "./0-7-0/local-file-initializer"
import HostMergeInitializer from "./host-merge-initializer"
import LocalFileInitializer from "./local-file-initializer"
import { Migrator } from "./common"
import WhitelistInitializer from "./whitelist-initializer"

/**
* Version manager
*
*
* @since 0.1.2
*/
class VersionManager {
processorChain: VersionProcessor[] = []
processorChain: Migrator[] = []

constructor() {
this.processorChain.push(
new HostMergeInitializer(),
new LocalFileInitializer(),
new WhitelistInitializer(),
)
this.processorChain = this.processorChain.sort((a, b) => a.since() >= b.since() ? 1 : 0)
}

private onChromeInstalled(reason: ChromeOnInstalledReason) {
const version: string = getVersion()
if (reason === 'update') {
// Update, process the latest version, which equals to current version
this.processorChain
.filter(processor => processor.since() === version)
.forEach(processor => processor.process(reason))
this.processorChain.forEach(processor => processor.onUpdate(version))
} else if (reason === 'install') {
// All
this.processorChain.forEach(processor => processor.process(reason))
// All
this.processorChain.forEach(processor => processor.onInstall())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ import MergeRuleDatabase from "@db/merge-rule-database"
import { JSON_HOST, LOCAL_HOST_PATTERN, MERGED_HOST, PDF_HOST, PIC_HOST, TXT_HOST } from "@util/constant/remain-host"
import { t2Chrome } from "@i18n/chrome/t"
import siteService from "@service/site-service"
import { Migrator } from "./common"

const storage: chrome.storage.StorageArea = chrome.storage.local
const mergeRuleDatabase = new MergeRuleDatabase(storage)
const mergeRuleDatabase = new MergeRuleDatabase(chrome.storage.local)

/**
* Process the host of local files
*
*
* @since 0.7.0
*/
export default class LocalFileInitializer implements VersionProcessor {
since(): string {
return '0.7.0'
export default class LocalFileInitializer implements Migrator {
onUpdate(_version: string): void {
}

process(): void {
onInstall(): void {
// Add merged rules
mergeRuleDatabase.add({
origin: LOCAL_HOST_PATTERN,
Expand Down
12 changes: 12 additions & 0 deletions src/background/migrator/whitelist-initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import whitelistService from "@service/whitelist-service"
import { Migrator } from "./common"

export default class WhitelistInitializer implements Migrator {
onInstall(): void {
whitelistService.add('localhost:*')
}

onUpdate(version: string): void {
version === '2.5.7' && this.onInstall()
}
}
25 changes: 0 additions & 25 deletions src/background/version-manager/version-manager.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function isValidVirtualHost(host: string) {
* @returns T/F
*/
export function judgeVirtualFast(host: string): boolean {
return host?.includes('/')
return host?.includes('/') || host?.includes('*')
}

export type HostInfo = {
Expand Down

0 comments on commit 69a65d3

Please sign in to comment.