UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

114 lines 5.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.stepRecorder = void 0; const format_1 = __importDefault(require("date-fns/format")); exports.stepRecorder = { count: 0, initialized: false, init() { this.count = this.getKeys().length; const div = document.createElement('div'); div.id = 'recorder'; div.style = 'position: fixed; top: -12px; right: 300px; z-index: 9999; background: #fffbd0; padding: 10px; border: 1px solid #ccc; border-radius: 5px;'; div.innerHTML = `<div><span style="color: red; font-size: 17px; vertical-align: -2px;">◉</span> 正在录制中…</div> <div style="margin-top: -2px;">当前应用的操作步骤数:<span id="recorder-count">${this.count}</span></div> <div style="margin-top: 4px;"> <button onclick="$data.nasl.stepRecorder.exportExcel()">下载数据</button> </div> `; // <button onclick="$data.nasl.stepRecorder.clear()">清除数据</button> document.body.appendChild(div); const script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/xlsx-populate/1.21.0/xlsx-populate.min.js'; document.body.appendChild(script); this.initialized = true; }, clear() { // eslint-disable-next-line no-alert const result = confirm('确定要清空所有当前应用的操作记录吗?'); if (result) { this.getKeys().forEach((key) => localStorage.removeItem(key)); this.count = 0; document.getElementById('recorder-count').innerText = String(this.count); } }, getAppName() { return location.search.match(/appId=([^-]+)/)[1]; }, getKeys() { return Object.keys(localStorage).filter((key) => key.startsWith(`录-${this.getAppName()}-`)); }, record(actionMsg, actionItem, actionList) { try { const actionItemDetail = actionList?.[0]; const timestamp = new Date().toJSON(); // eslint-disable-next-line function-paren-newline localStorage.setItem(`录-${this.getAppName()}-${this.count + 1}`, JSON.stringify( // { // timestamp, // message: actionMsg + (actionItem?.action ? ' ' + actionItem.action : ''), // detail: { // action: actionItemDetail?.action, // path: actionItemDetail?.path, // object: { // concept: actionItemDetail?.object?.concept, // name: actionItemDetail?.object?.name, // } // }, // } [ timestamp, actionMsg, { undo: '撤销', redo: '重做' }[actionItem?.action], actionItemDetail?.action, actionItemDetail?.path, actionItemDetail?.object?.concept, actionItemDetail?.object?.name, ])); this.count++; document.getElementById('recorder-count').innerText = String(this.count); } catch (e) { console.log(e); } }, exportExcel() { window.XlsxPopulate.fromBlankAsync().then((workbook) => { const sheet = workbook.sheet(0); const firstRowData = ['操作时间', '操作信息', '是否为撤销重做', '(详情)action', '(详情)path', '(详情)concept', '(详情)name']; sheet.range(1, 1, 1, firstRowData.length).value([firstRowData]); sheet .row(1) .style({ bold: true, fill: 'E1EAFF', }) .height(22); [30, 30, 30, 14, 30, 18, 18].forEach((width, index) => sheet.column(index + 1).width(width)); this.getKeys() .sort() .forEach((key, index) => { const item = JSON.parse(localStorage[key]); item[0] = (0, format_1.default)(new Date(item[0]), 'yyyy-MM-dd HH:mm:ss'); sheet.range(index + 2, 1, index + 2, firstRowData.length).value([item]); }); // Write to file. return workbook.outputAsync().then((blob) => { const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); document.body.appendChild(a); a.href = url; a.download = `操作步骤统计-${this.getAppName()}-${(0, format_1.default)(new Date(), 'yyyyMMddHHmmss')}.xlsx`; a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); }); }); }, }; exports.default = exports.stepRecorder; //# sourceMappingURL=stepRecorder.js.map