Skip to content

Commit

Permalink
feat: surge2egern
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Nov 3, 2024
1 parent 46c706f commit 4fcf043
Show file tree
Hide file tree
Showing 6 changed files with 705 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"source.organizeImports.biome": "explicit"
},
"conventionalCommits.scopes": ["arguments-builder", "sgmoudle-tools"],
"cSpell.words": ["iringo", "MITM", "modkit", "rsbuild"]
"cSpell.words": ["egern", "iringo", "MITM", "modkit", "rsbuild"]
}
6 changes: 6 additions & 0 deletions packages/surge2egern/modern.config.ts
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',
});
22 changes: 22 additions & 0 deletions packages/surge2egern/package.json
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"
}
}
92 changes: 92 additions & 0 deletions packages/surge2egern/src/index.ts
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);
}
}
18 changes: 18 additions & 0 deletions packages/surge2egern/tsconfig.json
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"]
}
Loading

0 comments on commit 4fcf043

Please sign in to comment.