diff --git a/packages/shared/package.json b/packages/shared/package.json index 02c876e2..f9fe1776 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -30,6 +30,7 @@ "dependencies": { "@any-reader/core": "workspace:^", "@any-reader/epub": "workspace:^", + "axios": "^1.7.2", "chardet": "^2.0.0", "fs-extra": "^11.1.1", "iconv-lite": "^0.6.3", diff --git a/packages/shared/rollup.config.js b/packages/shared/rollup.config.js index dfe09732..dbb54318 100644 --- a/packages/shared/rollup.config.js +++ b/packages/shared/rollup.config.js @@ -25,6 +25,7 @@ const external = [ // 'lodash-es', 'lowdb', 'uuid', + 'axios', ] const plugins = [ diff --git a/packages/shared/src/api.ts b/packages/shared/src/api.ts index a9c32b68..ef704bd6 100644 --- a/packages/shared/src/api.ts +++ b/packages/shared/src/api.ts @@ -252,4 +252,5 @@ export function useApi(register: any, { CONFIG_PATH, bookDir }: any) { register('post@batchUpdateRules', async (data: any) => success(await batchUpdateRules(data))) register('post@delRules', async (data: any) => success(await delRules(data))) register('post@updateRuleSort', async (data: any) => success(await ruleFileManager.updateRuleSort(data && data.id))) + register('post@importRules', async (data: any) => success(await ruleFileManager.importRules(data && data.url))) } diff --git a/packages/shared/src/ruleFileManager.ts b/packages/shared/src/ruleFileManager.ts index a1433edc..bfd2eb5d 100644 --- a/packages/shared/src/ruleFileManager.ts +++ b/packages/shared/src/ruleFileManager.ts @@ -4,6 +4,7 @@ import { v4 as uuidV4 } from 'uuid' import _ from 'lodash-es' import type { Low } from 'lowdb/lib' import { JSONFilePreset } from 'lowdb/node' +import axios from 'axios' import type { Rule } from '@any-reader/core' import { BOOK_SOURCE_PATH } from './constants' @@ -83,3 +84,33 @@ export async function updateRuleSort(ids: string[]) { } writeDB() } + +/** + * + * @param rule + * @returns {boolean} + */ +export function isRule(rule: any): boolean { + if (typeof rule === 'string') + return rule.startsWith('eso://:') + + if (typeof rule !== 'object') + return false + + return rule.id && rule.host && rule.contentType +} + +export async function importRules(url: string) { + const res = await axios.create().get(url).catch((e) => { + console.warn(e) + }) + if (!res || Array.isArray(res?.data)) + return + + for (const rule of res.data) { + if (isRule(rule)) + await update(rule).catch(() => {}) + } + + return res.data.length +} diff --git a/packages/web/electron/main.ts b/packages/web/electron/main.ts index 2eac1321..94639729 100644 --- a/packages/web/electron/main.ts +++ b/packages/web/electron/main.ts @@ -6,7 +6,11 @@ app.whenReady().then(async () => { const win = new BrowserWindow({ title: 'AnyReader', titleBarStyle: 'hidden', - webPreferences: { nodeIntegration: true, contextIsolation: false } + webPreferences: { + nodeIntegration: true, + contextIsolation: false, + webSecurity: false + } }); api.init(); diff --git a/packages/web/src/api/index.ts b/packages/web/src/api/index.ts index e3aff395..4a378262 100644 --- a/packages/web/src/api/index.ts +++ b/packages/web/src/api/index.ts @@ -187,3 +187,12 @@ export function updateRuleSort(data: any) { data }); } + +// 导入规则 +export function importRules(data: any) { + return request({ + method: 'post', + url: 'importRules', + data + }); +} diff --git a/packages/web/src/pages/pc/rules/ImportRules.vue b/packages/web/src/pages/pc/rules/ImportRules.vue new file mode 100644 index 00000000..2c103080 --- /dev/null +++ b/packages/web/src/pages/pc/rules/ImportRules.vue @@ -0,0 +1,40 @@ + + + diff --git a/packages/web/src/pages/pc/rules/index.vue b/packages/web/src/pages/pc/rules/index.vue index 4995ec1d..87ad1cd5 100644 --- a/packages/web/src/pages/pc/rules/index.vue +++ b/packages/web/src/pages/pc/rules/index.vue @@ -10,13 +10,17 @@ +
- -