From 3b7db145cf85c4178354176bc0f7b9803bf44354 Mon Sep 17 00:00:00 2001 From: baiy Date: Mon, 19 Apr 2021 17:43:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=94=AF=E6=8C=81utools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + README.md | 5 +- package.json | 2 +- public/setting.html | 14 ----- public/utools.html | 33 +++++++++++ src/{ => adapter/chrome}/manifest.json | 1 - src/adapter/utools/plugin.json | 16 ++++++ src/config.js | 37 ++++++++++++ src/helper.js | 21 ++++--- src/setting.js | 12 ---- src/tool.js | 18 +++++- src/tool.vue | 7 ++- src/tool/adapter.js | 80 ++++++++++++++++++++++++++ src/tool/config.js | 48 ++++++---------- src/views/setting/block.vue | 39 ++++++++----- src/{ => views/setting}/setting.vue | 2 +- vue.config.js | 21 +++---- 17 files changed, 256 insertions(+), 102 deletions(-) delete mode 100644 public/setting.html create mode 100644 public/utools.html rename src/{ => adapter/chrome}/manifest.json (96%) create mode 100644 src/adapter/utools/plugin.json create mode 100644 src/config.js delete mode 100644 src/setting.js create mode 100644 src/tool/adapter.js rename src/{ => views/setting}/setting.vue (86%) diff --git a/.gitignore b/.gitignore index e5984ee..17ad632 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ /desktop/node_modules /dist /public/manifest.json +/public/plugin.json +/public/README.md /id_rsa.pem # local env files diff --git a/README.md b/README.md index 5a437cc..01becd5 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ # 程序开发常用工具 使用过程中的任何问题或者需要新的工具欢迎提交`Issue`,新工具如果可以提供实现代码就完美了O(∩_∩)O -## 安装 +## chrome 安装 - 方法1: 在 [Chrome 网上应用店](https://chrome.google.com/webstore/detail/ipfcebkfhpkjeikaammlkcnalknjahmh)安装 - 方法2: [下载 .crx 安装包](https://github.com/baiy/Ctool/releases/latest)手动安装 [猛戳这里查看手动安装教程](http://www.cnplugins.com/tool/outline-install-crx-file.html) - 方法3: [百度网盘下载](https://pan.baidu.com/s/1mhWbqWC) 安装方法和方法2一致 - > 方法2 / 方法3 不定期维护 仅供网络环境特别恶劣的同学使用 +## 支持 utools + ## 功能列表 |功能|说明|离线使用| |---|---|---| diff --git a/package.json b/package.json index cc84f10..b677d45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "c-tool", - "version": "1.4.3", + "version": "1.4.4", "private": true, "scripts": { "serve": "vue-cli-service serve --port 8081", diff --git a/public/setting.html b/public/setting.html deleted file mode 100644 index b297d34..0000000 --- a/public/setting.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - 设置 - - -
-
-
- - diff --git a/public/utools.html b/public/utools.html new file mode 100644 index 0000000..02431f4 --- /dev/null +++ b/public/utools.html @@ -0,0 +1,33 @@ + + + + + + + Ctool 程序开发常用工具 + + + + + \ No newline at end of file diff --git a/src/manifest.json b/src/adapter/chrome/manifest.json similarity index 96% rename from src/manifest.json rename to src/adapter/chrome/manifest.json index cf7589d..5ac9296 100644 --- a/src/manifest.json +++ b/src/adapter/chrome/manifest.json @@ -12,7 +12,6 @@ "default_title": "常用开发工具", "default_popup": "index.html" }, - "options_page": "setting.html", "icons": { "16": "img/icon128.png", "48": "img/icon128.png", diff --git a/src/adapter/utools/plugin.json b/src/adapter/utools/plugin.json new file mode 100644 index 0000000..48113c0 --- /dev/null +++ b/src/adapter/utools/plugin.json @@ -0,0 +1,16 @@ +{ + "pluginName": "Ctool", + "description": "Ctool 程序开发常用工具", + "author": "baiy", + "homepage": "https://github.com/baiy/Ctool", + "main": "utools.html", + "version": "##version##", + "logo": "img/icon128.png", + "pluginSetting": { + "single": true + }, + "features": "##features##", + "development": { + "main": "http://localhost:8081/utools.html" + } +} \ No newline at end of file diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..8971dfd --- /dev/null +++ b/src/config.js @@ -0,0 +1,37 @@ +const category = [ + {'name': 'common', 'title': '常用工具'}, + {'name': 'encryption', 'title': '加密解密'}, + {'name': 'conversion', 'title': '编码转换'}, + {'name': 'serialize', 'title': '序列化'}, + {'name': 'other', 'title': '其他工具'}, +] + +const tool = [ + {'name': 'hash', 'title': '哈希(hash)', 'cat': ['encryption']}, + {'name': 'encrypt', 'title': '加密/解密', 'cat': ['encryption']}, + {'name': 'base64', 'title': 'BASE64编码', 'cat': ['encryption']}, + {'name': 'json', 'title': 'JSON工具', 'cat': ['conversion', 'serialize']}, + {'name': 'url', 'title': 'URL编码', 'cat': ['conversion']}, + {'name': 'timestamp', 'title': '时间戳', 'cat': ['conversion']}, + {'name': 'qrCode', 'title': '二维码', 'cat': ['other']}, + {'name': 'pinyin', 'title': '汉字转拼音', 'cat': ['conversion']}, + {'name': 'ip', 'title': 'IP地址查询', 'cat': ['other']}, + {'name': 'code', 'title': '代码格式化', 'cat': ['other']}, + {'name': 'unicode', 'title': 'Unicode', 'cat': ['conversion']}, + {'name': 'decimalConvert', 'title': '进制转换', 'cat': ['conversion']}, + {'name': 'regex', 'title': '正则表达式', 'cat': ['other']}, + {'name': 'randomString', 'title': '随机字符生成', 'cat': ['other']}, + {'name': 'serializeConversion', 'title': '序列化转换', 'cat': ['conversion', 'serialize']}, + {'name': 'diffs', 'title': '文本差异化对比', 'cat': ['other']}, + {'name': 'crontab', 'title': 'crontab校验', 'cat': ['other']}, + {'name': 'websocket', 'title': 'websocket调试', 'cat': ['other']}, + {'name': 'unit', 'title': '单位换算', 'cat': ['other']}, + {'name': 'time', 'title': '时间计算器', 'cat': ['other']}, + {'name': 'uuid', 'title': 'UUID生成', 'cat': ['other']}, + {'name': 'jsonToObject', 'title': 'JSON转实体类', 'cat': ['conversion', 'serialize']}, +] + +module.exports = { + category, + tool +} \ No newline at end of file diff --git a/src/helper.js b/src/helper.js index b84b473..e7969a1 100644 --- a/src/helper.js +++ b/src/helper.js @@ -1,5 +1,13 @@ import {stringify as queryStringify} from "query-string" +export const env = function (key) { + return process['ctool'][key] ? process['ctool'][key] : ""; +}; + +export const isChrome = !!env('isChrome') +export const isWeb = !!env('isWeb') +export const isUtools = !!env('isUtools') + export const trim = function (str, char, type) { if (char) { if (type === 'left') { @@ -19,14 +27,13 @@ export const inArray = function (value, arr) { }; export const openTab = function (url) { - if (!chrome.tabs) { - return window.open(url); + if (isChrome && chrome.tabs) { + return chrome.tabs.create({url: url, selected: true}); } - chrome.tabs.create({url: url, selected: true}); -}; - -export const env = function (key) { - return process['ctool'][key] ? process['ctool'][key] : ""; + if (isUtools && window.utools){ + return window.utools.shellOpenExternal(url) + } + return window.open(url); }; export const stat = function (action, data = {}) { diff --git a/src/setting.js b/src/setting.js deleted file mode 100644 index 19447d4..0000000 --- a/src/setting.js +++ /dev/null @@ -1,12 +0,0 @@ -import Vue from 'vue' -import ViewUI from 'view-design'; -import 'view-design/dist/styles/iview.css'; -import App from './setting.vue' - -Vue.config.productionTip = false; - -Vue.use(ViewUI); - -new Vue({ - render: h => h(App) -}).$mount('#app'); diff --git a/src/tool.js b/src/tool.js index 6389deb..c6d5129 100644 --- a/src/tool.js +++ b/src/tool.js @@ -3,8 +3,10 @@ import ViewUI from 'view-design' import 'view-design/dist/styles/iview.css' import router from './tool.router' import optionBlock from './components/optionBlock' -import { plugin as modelPlugin } from './tool/model' +import model, {plugin as modelPlugin} from './tool/model' import App from './tool.vue' +import {isUtools} from './helper' +import config from "./tool/config"; Vue.config.productionTip = false @@ -13,7 +15,18 @@ Vue.use(modelPlugin) Vue.component('option-block', optionBlock); (function () { - if (document.body.clientWidth > 900) { + if (isUtools) { + let tool = window.utools.db.get("_current_tool_"); + if (tool && tool.data) { + let cat = config.getToolDefaultCategory(tool.data); + if (cat) { + model.setCategoryHistory(cat) + model.setToolHistory(cat, tool.data) + } + } + } + + if (document.body.clientWidth > 900 || isUtools) { console.log('调整窗口大小') const page = document.getElementById('page') page.style.width = 'auto' @@ -21,7 +34,6 @@ Vue.component('option-block', optionBlock); page.style.height = 'auto' } })() - new Vue({ router, render: h => h(App), diff --git a/src/tool.vue b/src/tool.vue index 7bd5d4b..979262f 100644 --- a/src/tool.vue +++ b/src/tool.vue @@ -10,7 +10,7 @@ {{ cat.title }} - + @@ -70,7 +70,7 @@ import settingBlock from "./views/setting/block" import model from './tool/model' import historyFactory from './tool/history' import {setLoadHistoryIndex} from './tool/history' -import { openTab } from './helper' +import { openTab,isUtools } from './helper' export default { components: { @@ -78,6 +78,7 @@ export default { }, data () { return { + isUtools:isUtools, category: config.category, currentCategory: '', currentTool: '', @@ -145,7 +146,7 @@ export default { this.settingShow = true; break case '_new': - openTab(window.location.href) + openTab(window.location.href) break case '_history': this.history() diff --git a/src/tool/adapter.js b/src/tool/adapter.js new file mode 100644 index 0000000..974c538 --- /dev/null +++ b/src/tool/adapter.js @@ -0,0 +1,80 @@ +let path = require('path'); +// 运行平台适配 +let platform = process.env.hasOwnProperty('npm_config_adapter') ? process.env.npm_config_adapter : ""; +platform = ["chrome", 'utools'].includes(platform) ? platform : "web" + +const IS_CHROME = "chrome" === platform +const IS_UTOOLS = "utools" === platform +const IS_WEB = "web" === platform + +const chromeConfigWrite = () => { + let fs = require('fs'); + // 移除环境配置文件 + let manifestPath = path.join(__dirname, '../../public/manifest.json'); + fs.unlink(manifestPath, () => { + }); + if (IS_CHROME) { + fs.readFile(path.join(__dirname, "../adapter/chrome/manifest.json"), 'utf8', function (err, files) { + if (err) return console.log(err); + let result = files.replace(/##version##/g, process.env.npm_package_version); + fs.writeFile(manifestPath, result, 'utf8', function (err) { + if (err) return console.log(err); + }); + }); + } +} + +const utoolsConfigWrite = () => { + let fs = require('fs'); + // 移除环境配置文件 + let fileArr = ['plugin.json', 'README.md'] + fileArr.forEach((file) => { + let filePath = path.join(__dirname, '../../public/' + file); + fs.unlink(filePath, () => { + }); + }) + + if (IS_UTOOLS) { + const toolConfig = require('../config') + let pluginPath = path.join(__dirname, '../../public/plugin.json'); + fs.readFile(path.join(__dirname, "../adapter/utools/plugin.json"), 'utf8', function (err, files) { + if (err) return console.log(err); + + let features = [ + { + "code": "ctool", + "explain": "程序开发常用工具", + "cmds": ['ctool', '程序开发常用工具'] + }, + ...toolConfig.tool.map((item) => { + return { + "code": "ctool-" + item.name, + "explain": item.title, + "cmds": [item.name, item.title, "ctool-" + item.name] + } + }) + ] + let result = files + .replace(/##version##/g, process.env.npm_package_version) + .replace(/"##features##"/g, JSON.stringify(features)); + fs.writeFile(pluginPath, result, 'utf8', function (err) { + if (err) return console.log(err); + }); + }); + let readmePath = path.join(__dirname, '../../public/README.md'); + fs.copyFile(path.join(__dirname, "../../README.md"), readmePath, function (err) { + if (err) return console.log(err); + }); + } +} + +module.exports = { + platform: platform, + isChrome: IS_CHROME, + isWeb: IS_WEB, + isUtools: IS_UTOOLS, + initialize: function () { + chromeConfigWrite(); + utoolsConfigWrite(); + } +} \ No newline at end of file diff --git a/src/tool/config.js b/src/tool/config.js index 574421f..58a70b6 100644 --- a/src/tool/config.js +++ b/src/tool/config.js @@ -6,9 +6,9 @@ export const TOOL_DATA_EXPIRY = 3600 * 24 // 徽章过期时间(天) export const BADGE_EXPIRY = 10 // 分类徽章 -export const BADGE_CATEGORY = ['serialize','other'] +export const BADGE_CATEGORY = [] // 工具徽章 -export const BADGE_TOOL = ['uuid','serializeConversion'] +export const BADGE_TOOL = [] // 默认常用工具 export const DEFAULT_COMMON_TOOL = [ 'hash', 'encrypt', 'json', 'base64', 'url', 'timestamp', @@ -16,37 +16,11 @@ export const DEFAULT_COMMON_TOOL = [ 'decimalConvert', 'randomString', 'diffs', ] -const category = [ - {'name': 'common', 'title': '常用工具'}, - {'name': 'encryption', 'title': '加密解密'}, - {'name': 'conversion', 'title': '编码转换'}, - {'name': 'serialize', 'title': '序列化'}, - {'name': 'other', 'title': '其他工具'}, -] +const toolConfig = require('../config') -const tool = [ - {'name': 'hash', 'title': '哈希(hash)', 'cat': ['encryption']}, - {'name': 'encrypt', 'title': '加密/解密', 'cat': ['encryption']}, - {'name': 'base64', 'title': 'BASE64编码', 'cat': ['encryption']}, - {'name': 'json', 'title': 'JSON工具', 'cat': ['conversion', 'serialize']}, - {'name': 'url', 'title': 'URL编码', 'cat': ['conversion']}, - {'name': 'timestamp', 'title': '时间戳', 'cat': ['conversion']}, - {'name': 'qrCode', 'title': '二维码', 'cat': ['other']}, - {'name': 'pinyin', 'title': '汉字转拼音', 'cat': ['conversion']}, - {'name': 'ip', 'title': 'IP地址查询', 'cat': ['other']}, - {'name': 'code', 'title': '代码格式化', 'cat': ['other']}, - {'name': 'unicode', 'title': 'Unicode', 'cat': ['conversion']}, - {'name': 'decimalConvert', 'title': '进制转换', 'cat': ['conversion']}, - {'name': 'regex', 'title': '正则表达式', 'cat': ['other']}, - {'name': 'randomString', 'title': '随机字符生成', 'cat': ['other']}, - {'name': 'serializeConversion', 'title': '序列化转换', 'cat': ['conversion', 'serialize']}, - {'name': 'diffs', 'title': '文本差异化对比', 'cat': ['other']}, - {'name': 'crontab', 'title': 'crontab校验', 'cat': ['other']}, - {'name': 'websocket', 'title': 'websocket调试', 'cat': ['other']}, - {'name': 'unit', 'title': '单位换算', 'cat': ['other']}, - {'name': 'time', 'title': '时间计算器', 'cat': ['other']}, - {'name': 'uuid', 'title': 'UUID生成', 'cat': ['other']}, -] +const category = toolConfig.category + +const tool = toolConfig.tool // 徽章是否显示 const badgeIsShow = function () { @@ -72,6 +46,15 @@ const getToolTitle = function (name) { return '' } +const getToolDefaultCategory = function (name) { + for (let i = 0; i < tool.length; i++) { + if (tool[i].name === name) { + return tool[i].cat[0] + } + } + return '' +} + const getSetting = function (name, defaultValue = null) { let setting = cache.getNoVersion('setting', {}) return !setting.hasOwnProperty(name) ? defaultValue : setting[name] @@ -99,6 +82,7 @@ export default { return inArray(cat, t.cat) }) }, + getToolDefaultCategory, badgeToolIsShow(tool) { return badgeIsShow() && inArray(tool, BADGE_TOOL) }, diff --git a/src/views/setting/block.vue b/src/views/setting/block.vue index a0d84f5..a7f0463 100644 --- a/src/views/setting/block.vue +++ b/src/views/setting/block.vue @@ -1,29 +1,40 @@