-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
705 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig, moduleTools } from '@modern-js/module-tools'; | ||
|
||
export default defineConfig({ | ||
plugins: [moduleTools()], | ||
buildPreset: 'npm-library', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@iringo/surge2egern", | ||
"version": "1.0.0", | ||
"main": "dist/lib/index.js", | ||
"module": "dist/es/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"scripts": { | ||
"dev": "modern build -w", | ||
"build": "modern build" | ||
}, | ||
"keywords": [], | ||
"author": "baranwang", | ||
"license": "MIT", | ||
"description": "", | ||
"dependencies": { | ||
"ora": "^8.1.1", | ||
"puppeteer": "^23.6.1" | ||
}, | ||
"devDependencies": { | ||
"@modern-js/module-tools": "^2.60.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import ora from 'ora'; | ||
import puppeteer, { type Page, type Browser, type ElementHandle } from 'puppeteer'; | ||
|
||
export class Surge2Egern { | ||
#browser!: Browser; | ||
|
||
#page!: Page; | ||
|
||
#initPromise: Promise<void>; | ||
|
||
constructor() { | ||
this.#initPromise = this.#init(); | ||
} | ||
|
||
async #init() { | ||
this.#browser = await puppeteer.launch(); | ||
this.#page = await this.#browser.newPage(); | ||
|
||
await this.#page.goto('https://gen.egernapp.com/'); | ||
} | ||
|
||
async #findElement(title: string) { | ||
await this.#initPromise; | ||
|
||
const handle = await this.#page.evaluateHandle( | ||
(option) => { | ||
const titleEl = Array.from(document.querySelectorAll('h1')).find((el) => el.textContent === option.title); | ||
if (!titleEl) { | ||
return null; | ||
} | ||
const result = titleEl.closest('div')?.querySelectorAll('.relative textarea'); | ||
if (!result) { | ||
return null; | ||
} | ||
const [input, output] = Array.from(result); | ||
return { input, output }; | ||
}, | ||
{ | ||
title, | ||
}, | ||
); | ||
|
||
const inputHandle = await handle.getProperty('input'); | ||
const outputHandle = await handle.getProperty('output'); | ||
|
||
const inputElement = inputHandle.asElement() as ElementHandle<HTMLInputElement>; | ||
const outputElement = outputHandle.asElement() as ElementHandle<HTMLInputElement>; | ||
if (!inputElement || !outputElement) { | ||
throw new Error('Element not found'); | ||
} | ||
return { inputElement, outputElement }; | ||
} | ||
|
||
async #transform(title: string, text: string) { | ||
const spinner = ora(`正在${title}`).start(); | ||
try { | ||
const { inputElement, outputElement } = await this.#findElement(title); | ||
|
||
// 清空输入框的内容 | ||
await this.#page.evaluate((input) => { | ||
input.value = ''; | ||
}, inputElement); | ||
|
||
// 输入新的内容 | ||
await inputElement.type(text); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
|
||
// 获取输出框的值 | ||
const outputValue = await this.#page.evaluate((output) => { | ||
return output.value; | ||
}, outputElement); | ||
|
||
await this.#browser.close(); | ||
spinner.succeed(`${title}成功`); | ||
|
||
return outputValue; | ||
} catch (error) { | ||
await this.#browser.close(); | ||
spinner.fail(`${title}失败`); | ||
throw error; | ||
} | ||
} | ||
|
||
transformModule(module: string) { | ||
return this.#transform('模块配置转换', module); | ||
} | ||
|
||
async transformRules(rules: string) { | ||
return this.#transform('规则集合转换', rules); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2021", | ||
"lib": ["DOM", "ESNext"], | ||
"module": "ES2020", | ||
"strict": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"jsx": "preserve", | ||
"resolveJsonModule": true, | ||
"moduleResolution": "Bundler", | ||
"useDefineForClassFields": true | ||
}, | ||
"include": ["src"] | ||
} |
Oops, something went wrong.