From 8f2d332cda552540dbb4df21e4591c92ea878697 Mon Sep 17 00:00:00 2001 From: fofolee Date: Mon, 25 Apr 2022 11:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20showDevTools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/sandbox.html | 0 plugin/lib/sandbox.js | 7 ++++++ plugin/lib/utoolsLite.js | 14 ++++++++++++ plugin/preload.js | 46 +++++++++++++++++++++------------------- 4 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 plugin/lib/sandbox.html create mode 100644 plugin/lib/sandbox.js create mode 100644 plugin/lib/utoolsLite.js diff --git a/plugin/lib/sandbox.html b/plugin/lib/sandbox.html new file mode 100644 index 0000000..e69de29 diff --git a/plugin/lib/sandbox.js b/plugin/lib/sandbox.js new file mode 100644 index 0000000..aaf1463 --- /dev/null +++ b/plugin/lib/sandbox.js @@ -0,0 +1,7 @@ +const nodeFns = require("./nodeFns") +window._ = require("lodash") +window.utools = require("./utoolsLite")() + +Object.keys(nodeFns).forEach(key => { + window[key] = nodeFns[key] +}) diff --git a/plugin/lib/utoolsLite.js b/plugin/lib/utoolsLite.js new file mode 100644 index 0000000..32b34f9 --- /dev/null +++ b/plugin/lib/utoolsLite.js @@ -0,0 +1,14 @@ +const getuToolsLite = () => { + var utoolsLite = Object.assign({}, _.cloneDeep(utools)) + // if (utools.isDev()) return utoolsLite + const dbBlackList = ['db', 'dbStorage', 'removeFeature', 'setFeature', 'onDbPull'] + const payBlackList = ['fetchUserServerTemporaryToken', 'getUserServerTemporaryToken', 'openPayment', 'fetchUserPayments'] + const etcBlackList = ['onPluginEnter', 'onPluginOut', 'createBrowserWindow'] + _.concat(dbBlackList, payBlackList, etcBlackList).forEach(item => { + delete utoolsLite[item] + }) + Object.freeze(utoolsLite) + return utoolsLite +} + +module.exports = getuToolsLite diff --git a/plugin/preload.js b/plugin/preload.js index 9b96eda..566d98c 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -12,6 +12,7 @@ const kill = require('tree-kill') require('ses') window._ = require("lodash") +window.getuToolsLite = require("./lib/utoolsLite") window.yuQueClient = axios.create({ baseURL: 'https://www.yuque.com/api/v2/', headers: { @@ -61,6 +62,7 @@ const shortCodes = [ const ctlKey = utools.isMacOs() ? 'command' : 'control' +const createBrowserWindow = utools.createBrowserWindow window.quickcommand = { // 模拟复制操作 simulateCopy: function() { @@ -173,6 +175,28 @@ window.quickcommand = { process.execPath.replace(/\/Frameworks\/.*/, "/MacOS/uTools") : process.execPath child_process.exec(uToolsPath, () => {}) + }, + + // 显示一个全功能的 devTools + showDevTools: function() { + const browserWindow = createBrowserWindow('lib/sandbox.html', { + show: false, + title: '快捷命令', + webPreferences: { + preload: 'lib/sandbox.js', + }, + }, () => { + browserWindow.webContents.openDevTools({ + mode: 'detach' + }) + let timer = setInterval(() => { + if (!browserWindow.webContents.isDevToolsOpened()) { + clearInterval(timer) + browserWindow.destroy() + } + }, 1000) + }) + return browserWindow } } @@ -450,28 +474,6 @@ let parseItem = item => { let parseStdout = stdout => stdout.map(x => parseItem(x)).join("\n") -// 屏蔽危险函数 -window.getuToolsLite = () => { - var utoolsLite = Object.assign({}, _.cloneDeep(utools)) - // if (utools.isDev()) return utoolsLite - // 数据库相关接口 - delete utoolsLite.db - delete utoolsLite.dbStorage - delete utoolsLite.removeFeature - delete utoolsLite.setFeature - delete utoolsLite.onDbPull - // 支付相关接口 - delete utoolsLite.fetchUserServerTemporaryToken - delete utoolsLite.getUserServerTemporaryToken - delete utoolsLite.openPayment - delete utoolsLite.fetchUserPayments - // 其他 - delete utoolsLite.onPluginEnter - delete utoolsLite.onPluginOut - Object.freeze(utoolsLite) - return utoolsLite -} - let getSandboxFuns = () => { var sandbox = { fetch: fetch.bind(window),