From 8eff9b5898b0d1c402f589d52c6a9def4f890c2f Mon Sep 17 00:00:00 2001 From: ahaoboy <504595380@qq.com> Date: Thu, 20 Jun 2024 17:11:11 +0800 Subject: [PATCH] v0.1.9-alpha.4 --- babel/hack.js | 6 +-- biome.json | 61 ++++++++++++++++++++++++++++++ package.json | 11 +++--- scripts.json | 5 ++- src/autoload-sub.meta.js | 7 ++++ src/autoload-sub.ts | 80 ++++++++++++++++++++++++++++++++++++++++ src/sub2clipboard.ts | 6 +-- 7 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 biome.json create mode 100644 src/autoload-sub.meta.js create mode 100644 src/autoload-sub.ts diff --git a/babel/hack.js b/babel/hack.js index b9e0393..3cff22d 100644 --- a/babel/hack.js +++ b/babel/hack.js @@ -5,8 +5,7 @@ const fnVar = (t) => t.functionExpression(null, [], t.blockStatement([])), ), ]); -module.exports = function ({ types: t }) { - return { +module.exports = ({ types: t }) => ({ visitor: { FunctionDeclaration(path) { path.node.body?.body?.unshift?.(fnVar(t)); @@ -15,5 +14,4 @@ module.exports = function ({ types: t }) { // path.node.body?.body?.unshift?.(fnVar(t)) }, }, - }; -}; + }); diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..4f52d3d --- /dev/null +++ b/biome.json @@ -0,0 +1,61 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.4.1/schema.json", + "organizeImports": { + "enabled": true + }, + "javascript": { + "formatter": { + "semicolons": "asNeeded" + } + }, + "formatter": { + "enabled": true, + "indentStyle": "space", + "indentWidth": 2 + }, + "files": { + "ignore": [ + "*.sh", + "target", + "output", + "*.stackdump", + "*.ps1", + "*.yaml", + "*.js", + "./mpv-*/dist/**", + "./mpv-*/es/**", + "node_modules", + ".vscode", + ".github", + "./common", + "./coverage", + "./html", + "CHANGELOG.json" + ] + }, + "linter": { + "enabled": true, + "rules": { + "style": { + "noNonNullAssertion": "off", + "noParameterAssign": "off" + }, + "complexity": { + "noBannedTypes": "off" + }, + "suspicious": { + "noArrayIndexKey": "off", + "noRedeclare": "off", + "noUnsafeDeclarationMerging": "off", + "noExplicitAny": "off", + "noImplicitAnyLet": "off", + "noFallthroughSwitchClause": "off", + "noAssignInExpressions": "off" + }, + "correctness": { + "useExhaustiveDependencies": "off" + }, + "recommended": true + } + } +} diff --git a/package.json b/package.json index 16fc6b9..8361f9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mpv-easy/mpsm-scripts", - "version": "0.1.9-alpha.3", + "version": "0.1.9-alpha.4", "files": ["bin", "dist", "es5", "bundle", "readme.md", "package.json"], "description": "@mpv-easy/mpsm-scripts", "main": "./dist/index.js", @@ -14,7 +14,8 @@ "dev:copy": "chokidar 'src/**/*' -c 'pnpm run build && pnpm run copy' -d 3000 --initial", "copy": "cp -r ./es5/*.js $MPV_SCRIPT_DIR/", "es5": "babel --presets @babel/preset-env ./bundle -d es5 --plugins ./babel/hack.js", - "lint": "biome check --apply-unsafe ./", + "lint:fix": "biome lint ./ --write --unsafe", + "lint": "biome check --apply-unsafe ./", "format": "biome format --write ./" }, "keywords": [], @@ -33,10 +34,10 @@ "@babel/core": "^7.23.9", "@babel/preset-env": "^7.23.9", "@babel/preset-typescript": "^7.23.3", - "@biomejs/biome": "^1.5.3", - "@mpv-easy/autoload": "0.1.9-alpha.8", + "@biomejs/biome": "^1.8.0", + "@mpv-easy/autoload": "0.1.9-alpha.8", "@mpv-easy/plugin": "0.1.9-alpha.8", - "@mpv-easy/tool": "0.1.9-alpha.8", + "@mpv-easy/tool": "0.1.9-alpha.9", "@mpv-easy/polyfill": "0.1.9-alpha.8", "@types/node": "^20.12.12", "@chokidar-napi/chokidar": "0.1.3", diff --git a/scripts.json b/scripts.json index e077af0..6aa786d 100644 --- a/scripts.json +++ b/scripts.json @@ -1,12 +1,13 @@ { "mpv-easy-es5": "https://github.com/mpv-easy/mpv-easy/releases/latest/download/mpv-easy-es5.js", - "mpv-easy-es6": "https://github.com/mpv-easy/mpv-easy/releases/latest/download/mpv-easy-es6.js", - "sub2clipboard": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/sub2clipboard.js", + "mpv-easy-es6": "https://github.com/mpv-easy/mpv-easy/releases/latest/download/mpv-easy-es6.js", + "sub2clipboard": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/sub2clipboard.js", "firequalizer15": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/firequalizer15.meta.lua", "visualizer": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/visualizer.meta.lua", "autocrop": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/autocrop.meta.lua", "acompressor": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/acompressor.meta.lua", "autoload": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/autoload.js", + "autoload-sub": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/autoload-sub.js", "speed": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/speed.js", "open-file-dialog": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/open-file-dialog.meta.lua", "vlc-aspect-ratio": "https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/vlc-aspect-ratio.js" diff --git a/src/autoload-sub.meta.js b/src/autoload-sub.meta.js new file mode 100644 index 0000000..7d9234a --- /dev/null +++ b/src/autoload-sub.meta.js @@ -0,0 +1,7 @@ +// ==UserScript== +// @name autoload-sub +// @version 0.1.1 +// @description Automatically load sub files +// @author mpv-easy +// @downloadURL https://github.com/mpv-easy/mpsm-scripts/releases/latest/download/autoload-sub.js +// ==/UserScript== diff --git a/src/autoload-sub.ts b/src/autoload-sub.ts new file mode 100644 index 0000000..a1a3587 --- /dev/null +++ b/src/autoload-sub.ts @@ -0,0 +1,80 @@ +import "@mpv-easy/polyfill"; +import { + isHttp, + fetch, + SubtitleTypes, + getenv, + getFileName, + joinPath, + writeFile, + commandv, + getPropertyNative, + type TrackItem, + existsSync, + observeProperty, + getProperty, + registerEvent, + mkdir, +} from "@mpv-easy/tool"; + +registerEvent("file-loaded", autoloadSub); +observeProperty("path", "string", autoloadSub); +registerEvent("start-file", autoloadSub); + +export function autoloadSub() { + const path = getProperty("path"); + if (!path?.length) { + return; + } + + const trackList = (getPropertyNative("track-list") || []).filter( + (i) => i.type === "sub", + ); + if (isHttp(path)) { + const list = SubtitleTypes.map((i) => { + const s = path.split(".").slice(0, -1); + s.push(i); + return s.join("."); + }); + const tmp = getenv("TMPDIR") || getenv("TMP") || getenv("tmp") || "./"; + for (const url of list) { + const name = getFileName(url); + if (!name?.length) { + continue; + } + + if (trackList.find((i) => i.title === name)) { + continue; + } + + try { + const resp = fetch(url); + + if (resp.status !== 200 || !resp.text?.length) { + continue; + } + + const subPath = joinPath(tmp, name); + writeFile(subPath, resp.text); + commandv("sub-add", subPath); + } catch (e) { + mkdir("./aaaa"); + } + } + } else { + const list = SubtitleTypes.map((i) => { + const s = path.split(".").slice(0, -1); + s.push(i); + return s.join("."); + }); + for (const url of list) { + const name = getFileName(url); + if (trackList.find((i) => i.title === name)) { + continue; + } + if (existsSync(url)) { + commandv("sub-add", url); + } + } + } +} diff --git a/src/sub2clipboard.ts b/src/sub2clipboard.ts index 139ba76..b71ddd2 100644 --- a/src/sub2clipboard.ts +++ b/src/sub2clipboard.ts @@ -2,7 +2,7 @@ import "@mpv-easy/polyfill"; import { setClipboard, print, observeProperty } from "@mpv-easy/tool"; observeProperty("sub-text", "string", (_, v) => { - if (v) { - setClipboard(v); - } + if (v) { + setClipboard(v); + } });