Skip to content

Commit 8b973b4

Browse files
piquark6046Copilot
andcommitted
feat: add GlobalMatchBuild class for enhanced domain matching functionality
Co-authored-by: Copilot <copilot@github.com>
1 parent a013e66 commit 8b973b4

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

builder/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"eslint": "^10.2.1",
4343
"piscina": "^5.1.4",
4444
"tldts": "^7.0.28",
45+
"ts-morph": "^28.0.0",
4546
"tsx": "^4.21.0",
4647
"typescript": "^6.0.3",
4748
"typescript-eslint": "^8.58.2",

builder/source/buildci.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { StandardBuild } from './build.js'
55
import { GroupedBuild } from './build-grouped.js'
66
import { Build } from './build-core.js'
77
import type { BuildOptions } from './build-core.js'
8+
import { GlobalMatchBuild } from './globalmtach-test.js'
89

910
let ParsedArgv = (await ParseArgumentsAndOptions<BuildOptions>(FilterArgumentsForOptions(Process.argv))).Options
1011
let Options = await Zod.strictObject({
@@ -21,4 +22,5 @@ const GroupedBuildInstance = new GroupedBuild(CoreBuildInstance, Options)
2122
await GroupedBuildInstance.Build()
2223
console.log('GroupedBuild completed successfully.')
2324
await GroupedBuildInstance.MakeGroupMetadata()
24-
console.log('Group metadata creation completed successfully.')
25+
console.log('Group metadata creation completed successfully.')
26+
await new GlobalMatchBuild(CoreBuildInstance, Options).Build()

builder/source/globalmtach-test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as Fs from 'node:fs'
2+
import * as ESBuild from 'esbuild'
3+
import PackageJson from '@npmcli/package-json'
4+
import { CreateBanner } from './banner/index.js'
5+
import { Build, type BuildOptions } from './build-core.js'
6+
7+
export class GlobalMatchBuild extends Build {
8+
constructor(FromOrOption: Build | BuildOptions, Option?: BuildOptions) {
9+
if (FromOrOption instanceof Build) {
10+
super(Option!)
11+
this.CopyStateFrom(FromOrOption)
12+
return
13+
}
14+
15+
super(FromOrOption)
16+
}
17+
18+
async Build() {
19+
let MatchingDomains: Set<string> = new Set<string>(this.FetchedDomains.get('Full'))
20+
let SubscriptionURL = new URL(`https://cdn.jsdelivr.net/npm/@filteringdev/tinyshield@${this.Options?.Version ?? (await PackageJson.load(this.ProjectRoot)).content.version ?? '0.0.0'}/dist/tinyShield-GlobalMatch.user.js`)
21+
const Banner = CreateBanner({
22+
Version: this.Options?.Version ?? (await PackageJson.load(this.ProjectRoot)).content.version ?? '0.0.0',
23+
BuildType: this.Options!.BuildType ?? 'production',
24+
Domains: new Set<string>(['*']),
25+
Name: 'tinyShield GlobalMatch',
26+
Namespace: 'https://github.com/FilteringDev/tinyShield',
27+
DownloadURL: SubscriptionURL,
28+
UpdateURL: SubscriptionURL,
29+
HomepageURL: new URL('https://github.com/FilteringDev/tinyShield'),
30+
SupportURL: new URL('https://github.com/FilteringDev/tinyShield/issues'),
31+
License: 'MPL-2.0',
32+
Author: 'PiQuark6046 and contributors',
33+
Description: {
34+
en: 'tinyShield allows AdGuard, uBlock Origin, Brave and ABP to resist against Ad-Shield quickly.',
35+
ko: 'tinyShield는 AdGuard, uBlock Origin, Brave 와 ABP가 애드쉴드에 빠르게 저항할 수 있도록 합니다.',
36+
ja: 'tinyShieldを使うと、AdGuard, uBlock Origin, Brave, およびABPがAd-Shieldに素早く対抗できます。'
37+
}
38+
})
39+
40+
Fs.copyFileSync(this.ProjectRoot + '/userscript/source/index.ts', this.ProjectRoot + '/userscript/source/index-globalmatch.ts')
41+
const GlobalMatchIndexPath = this.ProjectRoot + '/userscript/source/index-globalmatch.ts'
42+
const SourceText = Fs.readFileSync(GlobalMatchIndexPath, 'utf8')
43+
const Anchor = 'export const OriginalRegExpTest = BrowserWindow.RegExp.prototype.test'
44+
const AnchorIndex = SourceText.indexOf(Anchor)
45+
if (AnchorIndex === -1) {
46+
throw new Error('[globalmatch] Failed to find OriginalRegExpTest anchor in index-globalmatch.ts')
47+
}
48+
49+
const AnchorLineEndIndex = SourceText.indexOf('\n', AnchorIndex)
50+
if (AnchorLineEndIndex === -1) {
51+
throw new Error('[globalmatch] Failed to determine insertion point in index-globalmatch.ts')
52+
}
53+
54+
const DomainListElements = Array.from(MatchingDomains).map(Domain => JSON.stringify(Domain)).join(', ')
55+
const ExecutionCondition = `(() => {\n const DomainList: string[] = [${DomainListElements}]\n const CurrentURL = new URL(BrowserWindow.location.href)\n if (!DomainList.some(Domain => BrowserWindow.location.href.includes(\`://\${Domain}/\`) || CurrentURL.hostname.endsWith(\`.\${Domain}\`))) {\n return\n }\n\n`
56+
57+
const Head = SourceText.slice(0, AnchorLineEndIndex + 1)
58+
const Tail = SourceText.slice(AnchorLineEndIndex + 1)
59+
const TransformedSource = `${Head}\n${ExecutionCondition}${Tail}\n})()\n`
60+
Fs.writeFileSync(GlobalMatchIndexPath, TransformedSource)
61+
62+
await ESBuild.build({
63+
entryPoints: [this.ProjectRoot + '/userscript/source/index-globalmatch.ts'],
64+
bundle: true,
65+
minify: this.Options!.Minify,
66+
outfile: `${this.ProjectRoot}/dist/tinyShield-GlobalMatch${this.Options!.BuildType === 'development' ? '.dev' : ''}.user.js`,
67+
banner: {
68+
js: Banner
69+
},
70+
target: ['es2024', 'chrome119', 'firefox142', 'safari26']
71+
})
72+
73+
Fs.rmSync(this.ProjectRoot + '/userscript/source/index-globalmatch.ts')
74+
}
75+
}

0 commit comments

Comments
 (0)