├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── release.yml ├── .gitignore ├── .prettierrc.js ├── CHANGELOG.md ├── LICENSE ├── README.md ├── docs └── images │ └── preview.png ├── package.json ├── rollup.config.js ├── scripts ├── build-zip.js ├── config.js ├── init-appcast.js └── init-info.js ├── src ├── appcast.json ├── global.d.ts ├── info.json ├── lang.ts ├── libs │ └── human-string.js ├── main.ts ├── translate.ts └── util.ts ├── tsconfig.eslint.json ├── tsconfig.json └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "modules": false, 7 | "targets": { 8 | "esmodules": true 9 | } 10 | } 11 | ], 12 | "@babel/preset-typescript" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | 4 | .DS_Store 5 | 6 | *.log 7 | 8 | .vscode -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { es6: true, node: true }, 4 | parser: '@typescript-eslint/parser', 5 | parserOptions: { 6 | ecmaVersion: 2020, 7 | sourceType: 'module', 8 | project: './tsconfig.eslint.json', 9 | }, 10 | plugins: ['import', 'prettier'], 11 | extends: [ 12 | 'airbnb-typescript/base', 13 | 'plugin:@typescript-eslint/recommended', 14 | 'plugin:@typescript-eslint/recommended-requiring-type-checking', 15 | ], 16 | settings: { 17 | 'import/parsers': { 18 | '@typescript-eslint/parser': ['.ts', '.tsx'], 19 | }, 20 | }, 21 | globals: { 22 | $log: false, 23 | $info: false, 24 | $option: false, 25 | $http: false, 26 | $file: false, 27 | $data: false, 28 | }, 29 | rules: { 30 | 'no-var': 'off', 31 | 'vars-on-top': 'off', 32 | 'prefer-const': 'off', 33 | 'no-return-assign': 'off', 34 | 'consistent-return': 'off', 35 | 'prefer-destructuring': 'off', 36 | 'no-underscore-dangle': 'off', 37 | 'object-curly-newline': 'off', 38 | // 'import/no-unresolved': 'error', 39 | 'import/no-mutable-exports': 'off', 40 | 'import/prefer-default-export': 'off', 41 | 'import/no-extraneous-dependencies': 'off', 42 | 'import/no-named-as-default-member': 'off', 43 | 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 44 | 'max-len': ['error', { ignoreComments: true, code: 120, ignoreStrings: true }], 45 | '@typescript-eslint/require-await': 'off', 46 | '@typescript-eslint/no-unsafe-call': 'off', 47 | '@typescript-eslint/no-explicit-any': 'off', 48 | '@typescript-eslint/no-var-requires': 'off', 49 | '@typescript-eslint/no-unsafe-return': 'off', 50 | '@typescript-eslint/no-throw-literal': 'off', 51 | '@typescript-eslint/naming-convention': 'off', 52 | '@typescript-eslint/no-floating-promises': 'off', 53 | '@typescript-eslint/no-unsafe-assignment': 'off', 54 | '@typescript-eslint/no-unsafe-member-access': 'off', 55 | '@typescript-eslint/restrict-template-expressions': 'off', 56 | '@typescript-eslint/explicit-module-boundary-types': 'off', 57 | '@typescript-eslint/quotes': ['error', 'single', { allowTemplateLiterals: true }], 58 | }, 59 | }; 60 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | tags: 5 | - '*' 6 | 7 | jobs: 8 | build: 9 | runs-on: macos-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | with: 13 | fetch-depth: 0 14 | token: ${{ secrets.GIT_TOKEN }} 15 | 16 | - uses: actions/setup-node@v2-beta 17 | with: 18 | node-version: '14' 19 | 20 | - name: Install Dependencies And Build 21 | run: | 22 | yarn install 23 | yarn run build 24 | 25 | - name: Commit files 26 | run: | 27 | git config --global user.name 'roojay520' 28 | git config --global user.email 'roojay520@users.noreply.github.com' 29 | git commit -am "chore: 更新版本文件" 30 | 31 | - name: Push changes 32 | uses: ad-m/github-push-action@master 33 | with: 34 | github_token: ${{ secrets.GIT_TOKEN }} 35 | 36 | - uses: ncipollo/release-action@v1 37 | with: 38 | artifacts: 'release/*.bobplugin' 39 | token: ${{ secrets.GIT_TOKEN }} 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | banner.user.js 106 | 107 | release -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | // .prettierrc.js 2 | module.exports = { 3 | // 一行最多 120 字符 4 | printWidth: 120, 5 | // 使用 2 个空格缩进 6 | tabWidth: 2, 7 | // 不使用缩进符,而使用空格 8 | useTabs: false, 9 | // 行尾需要有分号 10 | semi: true, 11 | // 使用单引号 12 | singleQuote: true, 13 | // 对象的 key 仅在必要时用引号 14 | quoteProps: 'as-needed', 15 | // jsx 不使用单引号,而使用双引号 16 | jsxSingleQuote: false, 17 | // 末尾需要有逗号 18 | trailingComma: 'all', 19 | // 大括号内的首尾需要空格 20 | bracketSpacing: true, 21 | // jsx 标签的反尖括号需要换行 22 | jsxBracketSameLine: false, 23 | // 箭头函数,只有一个参数的时候,也需要括号 24 | arrowParens: 'always', 25 | // 每个文件格式化的范围是文件的全部内容 26 | rangeStart: 0, 27 | rangeEnd: Infinity, 28 | // 不需要写文件开头的 @prettier 29 | requirePragma: false, 30 | // 不需要自动在文件开头插入 @prettier 31 | insertPragma: false, 32 | // 使用默认的折行标准 33 | proseWrap: 'preserve', 34 | // 根据显示样式决定 html 要不要折行 35 | htmlWhitespaceSensitivity: 'css', 36 | // vue 文件中的 script 和 style 内不用缩进 37 | vueIndentScriptAndStyle: false, 38 | // 换行符使用 lf 39 | endOfLine: 'lf', 40 | // 格式化嵌入的内容 41 | embeddedLanguageFormatting: 'auto', 42 | }; 43 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.0.5](https://github.com/roojay520/bobplug-nbnhhsh/compare/v0.0.4...v0.0.5) (2021-07-08) 2 | 3 | 4 | ### Bug Fixes 5 | 6 | * 修复更新地址 ([17b57e2](https://github.com/roojay520/bobplug-nbnhhsh/commit/17b57e239ed718fc5eae7fbca6347c3f3693f9bd)) 7 | 8 | 9 | 10 | ## [0.0.4](https://github.com/roojay520/bobplug-nbnhhsh/compare/v0.0.3...v0.0.4) (2021-07-08) 11 | 12 | 13 | 14 | ## [0.0.3](https://github.com/roojay520/bobplug-nbnhhsh/compare/v0.0.1...v0.0.3) (2021-07-08) 15 | 16 | 17 | 18 | ## [0.0.2](https://github.com/roojay520/bobplug-nbnhhsh/compare/v0.0.1...v0.0.2) (2021-07-08) 19 | 20 | 21 | 22 | ## 0.0.1 (2021-07-08) 23 | 24 | 25 | ### Features 26 | 27 | * 实现基础功能 ([e916bde](https://github.com/roojay520/bobplug-nbnhhsh/commit/e916bde81b157d2e47e1a60cb23b011762d08e1c)) 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 roojay520 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bobplug-nbnhhsh 2 | 3 | > 这是 [Bob](https://ripperhe.gitee.io/bob/#/) 的一个插件 4 | 5 | ## 特性 6 | 7 | - 支持缓存查询结果(默认缓存过期时间为一周); 8 | 9 | ## 安装 10 | 11 | 1. 安装 [Bob](https://ripperhe.gitee.io/bob/#/general/quickstart/install) (version >= 0.50) 12 | 2. 下载插件: [bobplug-nbnhhsh](https://github.com/roojay520/bobplug-nbnhhsh/releases) 13 | 3. 插件安装: [Bob 插件安装文档说明](https://ripperhe.gitee.io/bob/#/general/quickstart/plugin?id=%e5%ae%89%e8%a3%85%e6%8f%92%e4%bb%b6) 14 | 15 | ## 预览 16 | 17 | ![Preview](./docs/images/preview.png) 18 | -------------------------------------------------------------------------------- /docs/images/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roojay520/bobplug-nbnhhsh/4eb6cf7487aa98fea0f311fae84c7dd227af664d/docs/images/preview.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bobplug-nbnhhsh", 3 | "version": "0.0.5", 4 | "description": "拼音首字母缩写释义工具", 5 | "homepage": "https://github.com/roojay520/bobplug-nbnhhsh", 6 | "repository": "https://github.com/roojay520/bobplug-nbnhhsh.git", 7 | "author": "roojay520", 8 | "license": "MIT", 9 | "main": "src/main.js", 10 | "keywords": [ 11 | "bobplugin", 12 | "translate" 13 | ], 14 | "scripts": { 15 | "clear": "rimraf ./dist && rimraf ./release", 16 | "initInfo": "node ./scripts/init-info.js", 17 | "install": "npm run initInfo", 18 | "dev": "cross-env NODE_ENV=development rollup -c rollup.config.js --watch", 19 | "build": "npm run clear && npm run initInfo && cross-env NODE_ENV=production rollup -c && node ./scripts/build-zip.js", 20 | "type-check": "tsc --noEmit --allowJs", 21 | "type-check:watch": "tsc --noEmit --allowJs --watch", 22 | "prettier:fix": "prettier --config .prettierrc.js --write .", 23 | "eslint": "eslint . -c .eslintrc.js --ext .ts,.tsx,.js", 24 | "eslint:fix": "eslint . -c .eslintrc.js --fix --ext .ts,.tsx,.js", 25 | "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", 26 | "version": "npm run initInfo && npm run changelog && git add CHANGELOG.md src/info.json" 27 | }, 28 | "dependencies": { 29 | "@bob-plug/core": "^0.1.3", 30 | "is-english": "^1.3.0", 31 | "querystring": "^0.2.1" 32 | }, 33 | "devDependencies": { 34 | "@babel/core": "^7.14.6", 35 | "@babel/preset-env": "^7.14.7", 36 | "@babel/preset-typescript": "^7.14.5", 37 | "@rollup/plugin-babel": "^5.3.0", 38 | "@rollup/plugin-commonjs": "^19.0.0", 39 | "@rollup/plugin-json": "^4.1.0", 40 | "@rollup/plugin-node-resolve": "^13.0.0", 41 | "@types/eslint-plugin-prettier": "^3.1.0", 42 | "@typescript-eslint/eslint-plugin": "^4.28.2", 43 | "@typescript-eslint/parser": "^4.28.2", 44 | "adm-zip": "^0.5.5", 45 | "conventional-changelog-cli": "^2.1.1", 46 | "cross-env": "^7.0.3", 47 | "esbuild": "^0.12.15", 48 | "eslint": "^7.30.0", 49 | "eslint-config-airbnb-typescript": "^12.3.1", 50 | "eslint-import-resolver-typescript": "^2.4.0", 51 | "eslint-plugin-import": "^2.23.4", 52 | "eslint-plugin-prettier": "^3.4.0", 53 | "fs-extra": "^10.0.0", 54 | "prettier": "^2.3.2", 55 | "rimraf": "^3.0.2", 56 | "rollup": "^2.52.8", 57 | "rollup-plugin-copy": "^3.4.0", 58 | "rollup-plugin-esbuild": "^4.5.0", 59 | "rollup-plugin-polyfill-node": "^0.6.2", 60 | "typescript": "^4.3.5" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import copy from 'rollup-plugin-copy'; 3 | import json from '@rollup/plugin-json'; 4 | import babel from '@rollup/plugin-babel'; 5 | import esbuild from 'rollup-plugin-esbuild'; 6 | import commonjs from '@rollup/plugin-commonjs'; 7 | import resolve from '@rollup/plugin-node-resolve'; 8 | import nodePolyfills from 'rollup-plugin-polyfill-node'; 9 | import packageJson from './package.json'; 10 | 11 | const pkg = `${packageJson.name}.bobplugin`; 12 | 13 | const RollupConfig = { 14 | input: path.join(__dirname, './src/main.ts'), 15 | output: { 16 | format: 'cjs', 17 | exports: 'auto', 18 | file: path.join(__dirname, `./dist/${pkg}/main.js`), 19 | globals: { 20 | $util: '$util', 21 | $http: '$http', 22 | $info: '$info', 23 | $option: '$option', 24 | $log: '$log', 25 | $data: '$data', 26 | $file: '$file', 27 | } 28 | }, 29 | plugins: [ 30 | copy({ 31 | targets: [ 32 | { src: './src/info.json', dest: `dist/${pkg}/` }, 33 | { src: './src/libs', dest: `dist/${pkg}/` }, 34 | ], 35 | }), 36 | json({ namedExports: false }), 37 | resolve({ 38 | extensions: ['.js', '.ts', '.json'], 39 | preferBuiltins: false, 40 | }), 41 | commonjs(), 42 | nodePolyfills(), 43 | babel({ 44 | extensions: ['.js', '.ts'], 45 | babelHelpers: 'bundled', 46 | exclude: 'node_modules/**', 47 | }), 48 | esbuild({ 49 | // All options are optional 50 | include: /\.[jt]?s$/, // default, inferred from `loaders` option 51 | exclude: /node_modules/, // default 52 | sourceMap: false, // default 53 | minify: process.env.NODE_ENV === 'production', 54 | target: 'es6', // default, or 'es20XX', 'esnext', 55 | // Add extra loaders 56 | loaders: { 57 | // Add .json files support 58 | // require @rollup/plugin-commonjs 59 | '.json': 'json', 60 | }, 61 | }), 62 | ], 63 | external: ['crypto-js'] 64 | }; 65 | 66 | export default RollupConfig; -------------------------------------------------------------------------------- /scripts/build-zip.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: roojay 3 | * @Description: 生成最终以 .bobplugin 结尾的安装包文件 4 | */ 5 | 6 | const path = require('path'); 7 | const AdmZip = require('adm-zip'); 8 | const initAppcast = require('./init-appcast'); 9 | const plugInfo = require('../src/info.json'); 10 | const config = require('./config'); 11 | 12 | const pkg = `${config.pkgName}-v${plugInfo.version}.bobplugin`; 13 | const pkgPath = path.resolve(__dirname, `../release/${pkg}`); 14 | 15 | const zip = new AdmZip(); 16 | zip.addLocalFolder(path.resolve(__dirname, `../dist/${config.pkgName}.bobplugin`)); 17 | zip.writeZip(pkgPath); 18 | 19 | initAppcast(); 20 | -------------------------------------------------------------------------------- /scripts/config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: roojay 3 | * @Description: 打包相关的配置文件 4 | */ 5 | 6 | const config = { 7 | pkgName: 'bobplug-nbnhhsh', 8 | github:{ 9 | username: 'roojay520', 10 | repository: 'bobplug-nbnhhsh' 11 | } 12 | }; 13 | 14 | module.exports = config; 15 | -------------------------------------------------------------------------------- /scripts/init-appcast.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: roojay 3 | * @Description: 根据配置文件(config.js,info.json) 生成 appcast.json 版本更新文件 4 | */ 5 | 6 | const path = require('path'); 7 | const fs = require('fs-extra'); 8 | const crypto = require('crypto'); 9 | 10 | const config = require('./config'); 11 | const plugInfo = require('../src/info.json'); 12 | const plugAppcast = require('../src/appcast.json'); 13 | 14 | const pkg = `${config.pkgName}-v${plugInfo.version}.bobplugin`; 15 | const repositoryUrl = `https://github.com/${config.github.username}/${config.github.repository}`; 16 | const releaseUrl = `${repositoryUrl}/releases/download`; 17 | 18 | module.exports = () => { 19 | const pkgPath = path.resolve(__dirname, `../release/${pkg}`); 20 | const appcastPath = path.join(__dirname, '../src/appcast.json'); 21 | 22 | const fileBuffer = fs.readFileSync(pkgPath); 23 | const sum = crypto.createHash('sha256'); 24 | sum.update(fileBuffer); 25 | const hex = sum.digest('hex'); 26 | 27 | const version = { 28 | version: plugInfo.version, 29 | desc: `${repositoryUrl}/blob/master/CHANGELOG.md`, 30 | sha256: hex, 31 | url: `${releaseUrl}/v${plugInfo.version}/${pkg}`, 32 | minBobVersion: plugInfo.minBobVersion, 33 | }; 34 | 35 | let versions = (plugAppcast && plugAppcast.versions) || []; 36 | if (!Array.isArray(versions)) versions = []; 37 | const index = versions.findIndex((v) => v.version === plugInfo.version); 38 | if (index === -1) { 39 | versions.splice(0, 0, version); 40 | } else { 41 | versions.splice(index, 1, version); 42 | } 43 | const appcastData = { identifier: plugInfo.identifier, versions }; 44 | fs.outputJSONSync(appcastPath, appcastData, { spaces: 2 }); 45 | }; 46 | -------------------------------------------------------------------------------- /scripts/init-info.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: roojay 3 | * @Description: 使用 package.json 里面的部分字段(version, author, homepage, description)覆盖 info.json 里面的数据 4 | */ 5 | const path = require('path'); 6 | const fs = require('fs-extra'); 7 | 8 | const config = require('./config'); 9 | const info = require('../src/info.json'); 10 | const packageJson = require('../package.json'); 11 | 12 | // eslint-disable-next-line max-len 13 | const appcast = `https://raw.githubusercontent.com/${config.github.username}/${config.github.repository}/master/src/appcast.json`; 14 | 15 | const { version, author = '', homepage = '', description = '' } = packageJson; 16 | const infoData = { ...info, version, author, homepage, summary: description, appcast }; 17 | const infoPath = path.join(__dirname, '../src/info.json'); 18 | 19 | fs.outputJSONSync(infoPath, infoData, { spaces: 2 }); 20 | -------------------------------------------------------------------------------- /src/appcast.json: -------------------------------------------------------------------------------- 1 | { 2 | "identifier": "com.roojay.bobplug.bobplugnbnhhsh", 3 | "versions": [ 4 | { 5 | "version": "0.0.5", 6 | "desc": "https://github.com/roojay520/bobplug-nbnhhsh/blob/master/CHANGELOG.md", 7 | "sha256": "473309d385e2715cc061d5992217b0ae4a40e1b524db875f0e43522a9d6b3b2d", 8 | "url": "https://github.com/roojay520/bobplug-nbnhhsh/releases/download/v0.0.5/bobplug-nbnhhsh-v0.0.5.bobplugin", 9 | "minBobVersion": "0.5.0" 10 | }, 11 | { 12 | "version": "0.0.3", 13 | "desc": "https://github.com/roojay520/bobplug-nbnhhsh/blob/master/CHANGELOG.md", 14 | "sha256": "382575ae7a58865b5d8260e6cf0e4210eec7a36e9e72da3e7ae37ae3c0bbfd75", 15 | "url": "https://github.com/roojay520/bobplug-nbnhhsh/releases/download/v0.0.3/bobplug-nbnhhsh-v0.0.3.bobplugin", 16 | "minBobVersion": "0.5.0" 17 | }, 18 | { 19 | "version": "0.0.1", 20 | "desc": "https://github.com/roojay520/bobplug-nbnhhsh/blob/master/CHANGELOG.md", 21 | "sha256": "34be259df063f52ec9b43aaa5b55623e488f34dc71db6c731416b768ff713e94", 22 | "url": "https://github.com/roojay520/bobplug-nbnhhsh/releases/download/v0.0.1/bobplug-nbnhhsh-v0.0.1.bobplugin", 23 | "minBobVersion": "0.5.0" 24 | } 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /src/global.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'is-english' 2 | -------------------------------------------------------------------------------- /src/info.json: -------------------------------------------------------------------------------- 1 | { 2 | "identifier": "com.roojay.bobplug.bobplugnbnhhsh", 3 | "category": "translate", 4 | "version": "0.0.5", 5 | "name": "能不能好好说话", 6 | "summary": "拼音首字母缩写释义工具", 7 | "author": "roojay520", 8 | "appcast": "https://raw.githubusercontent.com/roojay520/bobplug-nbnhhsh/master/src/appcast.json", 9 | "homepage": "https://github.com/roojay520/bobplug-nbnhhsh", 10 | "icon": "001", 11 | "minBobVersion": "0.5.0", 12 | "options": [ 13 | { 14 | "identifier": "cache", 15 | "type": "menu", 16 | "title": "缓存", 17 | "defaultValue": "enable", 18 | "menuValues": [ 19 | { 20 | "title": "开启", 21 | "value": "enable" 22 | }, 23 | { 24 | "title": "关闭", 25 | "value": "disable" 26 | } 27 | ] 28 | } 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /src/lang.ts: -------------------------------------------------------------------------------- 1 | import { Language } from '@bob-plug/core'; 2 | 3 | type ILang = [Language, string]; 4 | 5 | // https://ripperhe.gitee.io/bob/#/plugin/addtion/language 6 | // Bob 语种标识和第三方语种标识符映射关系 [['bob语种, '第三方接口语种']] 7 | var languageList: ILang[] = [ 8 | ['auto', 'auto'], 9 | ['zh-Hans', 'zh-CN'], 10 | ['zh-Hant', 'zh-TW'], 11 | ['en', 'en'], 12 | ]; 13 | 14 | // Bob 语种标识符 15 | var standardLangMap = new Map(languageList); 16 | // 第三方语种标识符 17 | var noStandardLangMap = new Map(languageList.map(([standardLang, lang]) => [lang, standardLang])); 18 | 19 | // Bob 语种标识符转服务商语种标识符 20 | function standardToNoStandard(lang: Language) { 21 | return standardLangMap.get(lang); 22 | } 23 | 24 | // 服务商语种标识符转 Bob 语种标识符 25 | function noStandardToStandard(lang: string) { 26 | return noStandardLangMap.get(lang); 27 | } 28 | 29 | // 获取支持的语种 30 | function getSupportLanguages() { 31 | return languageList.map(([standardLang]) => standardLang); 32 | } 33 | 34 | export { getSupportLanguages, standardToNoStandard, noStandardToStandard }; 35 | -------------------------------------------------------------------------------- /src/libs/human-string.js: -------------------------------------------------------------------------------- 1 | // https://github.com/sindresorhus/humanize-string 2 | 3 | var decamelize = (text, separator = '_') => { 4 | if (!(typeof text === 'string' && typeof separator === 'string')) { 5 | throw new TypeError('The `text` and `separator` arguments should be of type `string`'); 6 | } 7 | return text 8 | .replace(/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu, `$1${separator}$2`) 9 | .replace(/(\p{Uppercase_Letter}+)(\p{Uppercase_Letter}\p{Lowercase_Letter}+)/gu, `$1${separator}$2`); 10 | }; 11 | 12 | var humanizeString = (input) => { 13 | let _input = input; 14 | if (typeof _input !== 'string') { 15 | throw new TypeError('Expected a string'); 16 | } 17 | _input = decamelize(_input, ''); 18 | _input = _input 19 | .replace(/[]+/g, ' ') 20 | .replace(/([_-])+/g, ' $1 ') 21 | // https://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines#answer-3469155:~:text=Use%20a%20double%2Dnegative 22 | .replace(/[^\S\r\n]{2,}/g, ' ') 23 | .trim(); 24 | _input = `${_input.charAt(0).toUpperCase()}${_input.slice(1)}`; 25 | 26 | // https://github.com/roojay520/bobplugin-google-translate/issues/2 27 | if (!_input.includes(' ')) return _input.toLowerCase(); 28 | 29 | return _input; 30 | }; 31 | 32 | module.exports = humanizeString; 33 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import * as Bob from '@bob-plug/core'; 2 | import { getSupportLanguages } from './lang'; 3 | 4 | import { _translate } from './translate'; 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | // 使用 bob 实现的 require 方法加载本地库, 13 | var formatString = require('./libs/human-string'); 14 | 15 | 16 | export function supportLanguages(): Bob.supportLanguages { 17 | return getSupportLanguages(); 18 | } 19 | 20 | 21 | // https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate 22 | export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) { 23 | const { text = '', detectFrom, detectTo } = query; 24 | const str = formatString(text); 25 | const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache') }; 26 | let res = _translate(str, params); 27 | 28 | res 29 | .then((result) => completion({ result })) 30 | .catch((error) => { 31 | Bob.api.$log.error(JSON.stringify(error)); 32 | if (error?.type) return completion({ error }); 33 | completion({ error: Bob.util.error('api', '插件出错', error) }); 34 | }); 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/translate.ts: -------------------------------------------------------------------------------- 1 | import * as Bob from '@bob-plug/core'; 2 | 3 | import isEnglish from 'is-english'; 4 | 5 | interface QueryOption { 6 | to?: Bob.Language; 7 | from?: Bob.Language; 8 | cache?: string; 9 | timeout?: number; 10 | } 11 | 12 | var resultCache = new Bob.CacheResult('translate-result'); 13 | 14 | /** 15 | * @description 翻译 16 | * @param {string} text 需要翻译的文字内容 17 | * @param {object} [options={}] 18 | * @return {object} 一个符合 bob 识别的翻译结果对象 19 | */ 20 | async function _translate(text: string, options: QueryOption = {}): Promise { 21 | const { from = 'auto', to = 'auto', cache = 'enable', timeout = 10000 } = options; 22 | const cacheKey = `${text}${from}${to}`; 23 | if (cache === 'enable') { 24 | const _cacheData = resultCache.get(cacheKey); 25 | if (_cacheData) return _cacheData; 26 | } else { 27 | resultCache.clear(); 28 | } 29 | 30 | const result: Bob.TranslateResult = { from, to, toParagraphs: ['暂无释义'] }; 31 | try { 32 | if (isEnglish(text) && text.length < 100) { 33 | const api = `https://lab.magiconch.com/api/nbnhhsh/guess`; 34 | const [err, res] = await Bob.util.asyncTo( 35 | Bob.api.$http.post({ 36 | timeout, 37 | url: api, 38 | header: { 39 | 'Content-Type': 'application/json;charset=UTF-8', 40 | }, 41 | body: { text }, 42 | }), 43 | ); 44 | if (err) Bob.api.$log.error(err); 45 | const resData = res?.data; 46 | // [{"name":"lol","trans":["大笑","英雄联盟"]}] 47 | const str: string[] = []; 48 | resData.forEach((row: any) => { 49 | if (Bob.util.isArrayAndLenGt(row?.trans, 0)) { 50 | str.push(...row.trans); 51 | } 52 | }); 53 | if (str.length) result.toParagraphs = [str.join('; ')]; 54 | } 55 | } catch (error) { 56 | throw Bob.util.error('api', '数据解析错误出错', error); 57 | } 58 | 59 | if (cache === 'enable') { 60 | resultCache.set(cacheKey, result); 61 | } 62 | return result; 63 | } 64 | 65 | export { _translate }; 66 | -------------------------------------------------------------------------------- /src/util.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable max-len */ 2 | export var userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36`; 3 | -------------------------------------------------------------------------------- /tsconfig.eslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "include": [ 4 | "src/**/*.ts", 5 | "types/**/*.ts", 6 | "./src/**/*.js", 7 | "./.eslintrc.js", 8 | "./rollup.config.js", 9 | "./scripts/**/*.js" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": ["src/**/*.ts", "types/**/*.ts"], 3 | "exclude": ["node_modules", "dist"], 4 | "compilerOptions": { 5 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 6 | /* Basic Options */ 7 | // "incremental": true, /* Enable incremental compilation */ 8 | "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, 9 | "module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, 10 | // "lib": [], /* Specify library files to be included in the compilation. */ 11 | "allowJs": true /* Allow javascript files to be compiled. */, 12 | // "checkJs": true, /* Report errors in .js files. */ 13 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 14 | "declaration": true /* Generates corresponding '.d.ts' file. */, 15 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 16 | "sourceMap": true /* Generates corresponding '.map' file. */, 17 | // "outFile": "./", /* Concatenate and emit output to single file. */ 18 | "outDir": "./dist" /* Redirect output structure to the directory. */, 19 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 20 | // "composite": true, /* Enable project compilation */ 21 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 22 | // "removeComments": true, /* Do not emit comments to output. */ 23 | // "noEmit": true, /* Do not emit outputs. */ 24 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 25 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 26 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 27 | 28 | /* Strict Type-Checking Options */ 29 | "strict": true /* Enable all strict type-checking options. */, 30 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 31 | "strictNullChecks": true /* Enable strict null checks. */, 32 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 33 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 34 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 35 | "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */, 36 | "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */, 37 | 38 | /* Additional Checks */ 39 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 40 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 41 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 42 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 43 | 44 | /* Module Resolution Options */ 45 | "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */, 46 | "baseUrl": "./" /* Base directory to resolve non-absolute module names. */, 47 | // "paths": { } /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */, 48 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 49 | // "typeRoots": ["node_modules/@types", "./types"] /* List of folders to include type definitions from. */, 50 | // "types": ["node"] /* Type declaration files to be included in compilation. */, 51 | "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */, 52 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, 53 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 54 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 55 | 56 | /* Source Map Options */ 57 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 58 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 59 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 60 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 61 | 62 | /* Experimental Options */ 63 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 64 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 65 | 66 | /* Advanced Options */ 67 | "skipLibCheck": true /* Skip type checking of declaration files. */, 68 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 69 | } 70 | } 71 | --------------------------------------------------------------------------------