diff --git a/public/tool.html b/public/tool.html index 7b081c9edaa9ce125799c91ff9c44ae90cdaa6d7..6910afb39996508e94deeb4c020a728b73d3898f 100644 --- a/public/tool.html +++ b/public/tool.html @@ -10,6 +10,5 @@
-
diff --git a/src/tool/clipboard.js b/src/tool/clipboard.js index d111d4bc1429ecdf7eca3fac8d9e77d216b16c9a..f3c429c259405b8f30e1f2f0d185b5b6279efd51 100644 --- a/src/tool/clipboard.js +++ b/src/tool/clipboard.js @@ -1,29 +1,49 @@ -import {isUtools} from '../helper' +import {Base64} from "js-base64"; + // 剪贴板操作 -export const copy = (data,successCallback)=>{ - document.querySelector( - '#clipboard').innerHTML = '' - document.querySelector('#clipboard-text').value = data - document.querySelector('#clipboard-text').select() - if (document.execCommand('copy')) { - successCallback && successCallback() +export const copy = (data, successCallback) => { + try { + navigator.clipboard.writeText(data).then(function () { + successCallback && successCallback() + }, function (e) { + console.log('copy failed', e) + }); + } catch (e) { + console.log('copy error', e) } - document.querySelector('#clipboard').innerHTML = '' } -export const paste = ()=>{ - document.querySelector('#clipboard').innerHTML = '' - document.querySelector('#clipboard-text').select() - document.execCommand('paste') - let r = document.querySelector('#clipboard-text').value || document.querySelector('#clipboard-text').innerHTML - document.querySelector('#clipboard').innerHTML = '' - return r ? r : '' +export const paste = async () => { + return new Promise((resolve) => { + try { + navigator.clipboard.readText().then((text) => { + return resolve(text ? text : "") + }).catch((e) => { + console.error(e) + return resolve("") + }) + } catch { + resolve("") + } + }); } -export const copyImage = (imageBase64,successCallback = "")=>{ - if (isUtools && imageBase64){ - window.utools.copyImage(imageBase64) - successCallback && successCallback() +export const copyImage = (imageBase64, successCallback = "") => { + if (imageBase64) { + try { + let arr = imageBase64.split(',') + let mime = arr[0].match(/:(.*?);/)[1]; + let data = [new window.ClipboardItem({ + [mime]: new Blob([Base64.toUint8Array(arr[1])], {type: mime}) + })]; + navigator.clipboard.write(data).then(function () { + successCallback && successCallback() + }, function (e) { + console.log('copy image failed', e) + }); + } catch (e) { + console.log('copy image error', e) + } } } diff --git a/src/tool/model.js b/src/tool/model.js index e279bd839ac8d6fbfca62d547865e6562e08ba79..498e45fbd97b678aa9b9c970402cae305e3f4738 100644 --- a/src/tool/model.js +++ b/src/tool/model.js @@ -51,38 +51,74 @@ const debounceSaveToolDataMethod = _.debounce(function () { return history(debounceSaveToolData['tool']).push(debounceSaveToolData['data']) }, 1000) + +const appendData = async function (field = "", check = "") { + const result = (data = "") => { + if (data){ + if ( + !check + || (_.isFunction(check) && check(data)) // 函数校验 + ){ + return field ? {[field]: data} : data + } + } + return field ? {} : "" + } + return new Promise(async (resolve) => { + try { + // 使用固定输入数据 + if (fixeInputData) { + return resolve(result(fixeInputData)) + } + if (setting.autoReadCopy()) { + let paste = (await clipboard.paste()).trim() + if (paste) { + resolve(result(paste)) + } + } + resolve(result()) + } catch { + resolve(result()) + } + }); +} + export const plugin = { install: function (Vue) { - Vue.prototype.$getToolData = function (clipboardField = '',clipboardFieldRegex = "") { - let data = history(model.getCurrentTool()).current() - if (clipboardField) { - let inputData = ""; - if (fixeInputData) { // 使用固定输入数据 - inputData = fixeInputData - fixeInputData = "" - } else if (setting.autoReadCopy()) { - let paste = clipboard.paste() - if (!data[clipboardField] && paste) { - if (setting.autoReadCopyFilter()) { - paste = paste.trim() - } - inputData = paste + Vue.prototype.$initToolData = function (input = "", inputCheck = "", field = "current", isLoadHistory = true) { + let current = _.cloneDeep(this[field]) + if (isLoadHistory) { + Object.assign(current, this.$getToolData()) + } + if (!input) { + this[field] = current + return; + } + + // 初始化默认值 + if (!(input in current)){ + current[input] = ""; + } + + // 保存默认值 + let inputDefault = current[input] + current[input] = ""; + + appendData(input, inputCheck).then((append) => { + for (let key of Object.keys(append)) { + if ((key in current) && !current[key]) { + current[key] = append[key] } } - if (inputData){ - if ( - !(clipboardFieldRegex instanceof RegExp) - || - ( - clipboardFieldRegex instanceof RegExp - && clipboardFieldRegex.test(inputData) - ) - ){ - data[clipboardField] = inputData - } + if (!current[input]){ + // 使用默认值 + current[input] = inputDefault } - } - return data + this[field] = current + }) + } + Vue.prototype.$getToolData = function () { + return _.cloneDeep(history(model.getCurrentTool()).current()) } Vue.prototype.$saveToolData = function (data, ignoreDebounce = false) { if (ignoreDebounce) { diff --git a/src/views/tool/ascii.vue b/src/views/tool/ascii.vue index a8370eb07115342e20bb3661fed59ddbaffb61ec..c182bf8b3cb91cb86a79fe0e5f8a0d277f4419b1 100644 --- a/src/views/tool/ascii.vue +++ b/src/views/tool/ascii.vue @@ -67,7 +67,7 @@ export default { }, }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { handle() { diff --git a/src/views/tool/barcode.vue b/src/views/tool/barcode.vue index 0b70c0ce5aaa5f92a01413890b24ba07d29d4d53..da05b4da0aca6905c2491afbc15e0bb5ec28e06c 100644 --- a/src/views/tool/barcode.vue +++ b/src/views/tool/barcode.vue @@ -2,7 +2,7 @@
+ class="barcode" v-show="!validStr" style="cursor:pointer">

{{ validStr }}

@@ -146,7 +146,7 @@ import JsBarcode from 'jsbarcode' export default { created() { - this.current = Object.assign(this.current, this.$getToolData("text")) + this.$initToolData('text') }, computed: { showText() { diff --git a/src/views/tool/base64.vue b/src/views/tool/base64.vue index 17509802c33f8b123ebdfd9a36dade87335842cc..77da74a3b75f93e9d7fbd987fba633719742dfd3 100644 --- a/src/views/tool/base64.vue +++ b/src/views/tool/base64.vue @@ -38,7 +38,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData('input')) + this.$initToolData('input') }, methods: { handle(v) { @@ -52,10 +52,10 @@ export default { } else{ this.current.output = Base64.decode(this.current.input) + this.$clipboardCopy(this.current.output) } } this.current.operation = v - this.$clipboardCopy(this.current.output) this.$saveToolData(this.current) } }, @@ -69,9 +69,9 @@ export default { }, exportFile() { let arr = this.current.input.split(','), mime = arr[0].match(/:(.*?);/)[1]; - let objectUrl = window.URL.createObjectURL(new Blob([new Blob([Base64.toUint8Array(arr[1])], {type: mime})], {type: mime})); + let objectUrl = window.URL.createObjectURL(new Blob([Base64.toUint8Array(arr[1])], {type: mime})); let aEle = document.createElement("a"); - aEle.download = `ctools-base64-decode-${moment().unix()}` + (mimeType.extension(mime) ? `.${mimeType.extension(mime)}` : ""); + aEle.download = `ctool-base64-decode-${moment().unix()}` + (mimeType.extension(mime) ? `.${mimeType.extension(mime)}` : ""); aEle.href = objectUrl; aEle.click(); aEle.remove(); diff --git a/src/views/tool/code.vue b/src/views/tool/code.vue index 9af3db641107c39852279819aea9aa7256f81d55..251e09417389c3f18812052b9b011cf295d74198 100644 --- a/src/views/tool/code.vue +++ b/src/views/tool/code.vue @@ -53,7 +53,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData("content")) + this.$initToolData('content') }, methods: { handle(language) { diff --git a/src/views/tool/crontab.vue b/src/views/tool/crontab.vue index 868d7b79fed9b68e699e3942d5cd7981ab592d49..0c849510c9c17cfd997f1b03a5f8e4ba2eb2a6e8 100644 --- a/src/views/tool/crontab.vue +++ b/src/views/tool/crontab.vue @@ -6,14 +6,15 @@ {{ $t('crontab_expression') }} - + -
+
@@ -35,6 +36,7 @@ import moment from "moment" import {getCurrentLocale} from "../../i18n"; import heightResize from "./components/heightResize"; import autoHeightTextarea from "./components/autoHeightTextarea"; + export default { components: { heightResize, @@ -63,7 +65,14 @@ export default { }, }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData('input', (data) => { + try { + cronstrue.toString(data) + } catch { + return false + } + return true + }) this.example.data = this.example.data.map((item) => { return { example: item, @@ -75,16 +84,15 @@ export default { conversion(input) { return cronstrue.toString(input, {locale: this.locale}) }, - resize(height){ + resize(height) { this.referenceHeight = height } }, data() { return { - referenceHeight:101, + referenceHeight: 101, current: { - input: "2 */5 * * 2-5", - operation: "check" + input: "2 */5 * * 2-5" }, special: { columns: [ diff --git a/src/views/tool/decimalConvert.vue b/src/views/tool/decimalConvert.vue index db84fd5307b7be79c561ec4907aa50b8ed47a93b..2f8e516fe4d06f1b081a99c80923122513aab842 100644 --- a/src/views/tool/decimalConvert.vue +++ b/src/views/tool/decimalConvert.vue @@ -42,7 +42,7 @@ import Radix from './library/radix.js' const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@"; export default { created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, watch: { convert: function (val) { diff --git a/src/views/tool/diffs.vue b/src/views/tool/diffs.vue index 5b29ff3871f3587b71f5f741c0f6fd5c29b08429..0a92506a47c266426cbf595f6accfeb5199db96b 100644 --- a/src/views/tool/diffs.vue +++ b/src/views/tool/diffs.vue @@ -63,7 +63,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { setLanguage(lang) { diff --git a/src/views/tool/encrypt.vue b/src/views/tool/encrypt.vue index 37b009132bdf9baacbf18b78b72b3fcd2879a62d..d7b6b06bb5e9377da1936cf38face9f9680fee96 100644 --- a/src/views/tool/encrypt.vue +++ b/src/views/tool/encrypt.vue @@ -40,7 +40,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/hash.vue b/src/views/tool/hash.vue index f65fbffcab7c1ff392a3c18946db1c399bbff8cc..f7f2f7d7f9871b0bb9795ee3b83d189a660980b7 100644 --- a/src/views/tool/hash.vue +++ b/src/views/tool/hash.vue @@ -21,7 +21,7 @@ import crypto from "crypto-js" const sm = require('sm-crypto'); export default { created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, computed: { md5() { diff --git a/src/views/tool/hexString.vue b/src/views/tool/hexString.vue index 3090b8d7743e53206a284dd6560d1e60846f36a6..8872b9d6d65f45706cf44a03ea9a769b00b9e14f 100644 --- a/src/views/tool/hexString.vue +++ b/src/views/tool/hexString.vue @@ -25,7 +25,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(type) { diff --git a/src/views/tool/html.vue b/src/views/tool/html.vue index 6e00f6589fffdcda9e8e6a98b36f94e2f298e44a..4de5abd3a607c0285b23fe01625561efdfd8ddab 100644 --- a/src/views/tool/html.vue +++ b/src/views/tool/html.vue @@ -22,7 +22,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/ip.vue b/src/views/tool/ip.vue index a1d0f9c081d279f6afa88017c3a69327dd140c3e..025b49f94efa0dabffbd38497ca9bcfcde063615 100644 --- a/src/views/tool/ip.vue +++ b/src/views/tool/ip.vue @@ -32,7 +32,12 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input',(input)=>{ + return ( + /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(input) + || input === "localhost" + ) + }) }, methods: { handle() { diff --git a/src/views/tool/json.vue b/src/views/tool/json.vue index 5659942df7c1a31f1246d67711c5addfd638b678..3be68babdd21a9546a827ad1d69b0279217f2cd3 100644 --- a/src/views/tool/json.vue +++ b/src/views/tool/json.vue @@ -23,7 +23,7 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData('content')) + this.$initToolData('content') }, methods: { handle(v) { diff --git a/src/views/tool/jsonToObject.vue b/src/views/tool/jsonToObject.vue index 483a211a3492f402462e273f8ed6361795599843..2c337226d8e6cdc5f84672b33372963e1eeb7cd3 100644 --- a/src/views/tool/jsonToObject.vue +++ b/src/views/tool/jsonToObject.vue @@ -13,7 +13,8 @@
- + @@ -22,7 +23,8 @@ - + @@ -28,7 +34,10 @@ {{ $t('timestamp_output') }} @@ -57,10 +66,14 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData('input')) - if (!this.current.input) { - this.current.input = moment().format('YYYY-MM-DD HH:mm:ss') - } + this.$initToolData('input', (data) => { + return ( + (new RegExp(/^\d+-\d+-\d+ \d+:\d+:\d+$/)).test(data) + || (new RegExp(/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+$/)).test(data) + || (new RegExp(/^\d{10}$/)).test(data) + || (new RegExp(/^\d{13}$/)).test(data) + ) + }) }, mounted() { this.timer = setInterval(() => { @@ -127,7 +140,7 @@ export default { }, methods: { copy(data) { - if (data){ + if (data) { this.$clipboardCopy(data, true) } }, @@ -147,7 +160,7 @@ export default { data() { return { current: { - input: '' + input: moment().format('YYYY-MM-DD HH:mm:ss') }, timer: null, timestamp: 0, diff --git a/src/views/tool/unicode.vue b/src/views/tool/unicode.vue index e5ea41b339a0a5e73b1ff3d7f5d23f802ebb9152..417b5ed10a2438fc855d41dc159f57109ffe2af7 100644 --- a/src/views/tool/unicode.vue +++ b/src/views/tool/unicode.vue @@ -36,7 +36,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(operation) { diff --git a/src/views/tool/url.vue b/src/views/tool/url.vue index dd4233cdb5e137839697593333b9deec2ed135d5..f57d783120dad87ec391f454fd591077de38fa6a 100644 --- a/src/views/tool/url.vue +++ b/src/views/tool/url.vue @@ -21,7 +21,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/uuid.vue b/src/views/tool/uuid.vue index b6805579709096786da637088f76f7a326431028..5a26b53852890de9345d7a096d8235203d2c72d9 100644 --- a/src/views/tool/uuid.vue +++ b/src/views/tool/uuid.vue @@ -40,7 +40,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, mounted() { if (this.current.result.length < 1){ diff --git a/src/views/tool/variableConversion.vue b/src/views/tool/variableConversion.vue index ac2c1ff92915004c10f01cce27ff285ed66c9e27..903d900cf2fc1ea709e306fe5ed19c8c9556fecf 100644 --- a/src/views/tool/variableConversion.vue +++ b/src/views/tool/variableConversion.vue @@ -40,7 +40,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { copy(type) { diff --git a/src/views/tool/websocket.vue b/src/views/tool/websocket.vue index 975f254d98f2435701e81b8b71c37eee0429cedf..1d52c1068b7c58078fd15131be21efd302961310 100644 --- a/src/views/tool/websocket.vue +++ b/src/views/tool/websocket.vue @@ -70,7 +70,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { handle() {