尝试 html 输出放在 iframe 里

This commit is contained in:
fofolee 2022-04-21 23:00:26 +08:00
parent 6d1e7f8347
commit 3ede4cdd73
4 changed files with 60 additions and 26 deletions

View File

@ -211,22 +211,22 @@ if (process.platform !== 'linux') quickcommand.runInTerminal = function(cmdline,
} }
let getCommandToLaunchTerminal = (cmdline, dir) => { let getCommandToLaunchTerminal = (cmdline, dir) => {
let cd = '' let cd = ''
if (utools.isWindows()) { if (utools.isWindows()) {
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/') let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
// 直接 existsSync wt.exe 无效 // 直接 existsSync wt.exe 无效
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) { if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
cmdline = cmdline.replace(/"/g, `\\"`) cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"` if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"` command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
} else {
cmdline = cmdline.replace(/"/g, `^"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else { } else {
cmdline = cmdline.replace(/"/g, `^"`) cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&` if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
if (fs.existsSync('/Applications/iTerm.app')) { if (fs.existsSync('/Applications/iTerm.app')) {
command = `osascript -e 'tell application "iTerm" command = `osascript -e 'tell application "iTerm"
create window with default profile create window with default profile
@ -451,7 +451,7 @@ let parseStdout = stdout => stdout.map(x => parseItem(x)).join("\n")
// 屏蔽危险函数 // 屏蔽危险函数
window.getuToolsLite = () => { window.getuToolsLite = () => {
var utoolsLite = Object.assign({}, utools) var utoolsLite = Object.assign({}, _.cloneDeep(utools))
// if (utools.isDev()) return utoolsLite // if (utools.isDev()) return utoolsLite
// 数据库相关接口 // 数据库相关接口
delete utoolsLite.db delete utoolsLite.db
@ -473,12 +473,12 @@ window.getuToolsLite = () => {
let getSandboxFuns = () => { let getSandboxFuns = () => {
var sandbox = { var sandbox = {
fetch: fetch.bind(window),
utools: getuToolsLite(), utools: getuToolsLite(),
quickcommand, quickcommand,
electron, electron,
axios, axios,
Audio, Audio,
fetch,
_, _,
// 兼容老版本 // 兼容老版本
fs, fs,
@ -503,8 +503,7 @@ utools.isDev() && (window.godMode = code => eval(code))
// vm 模块将无法在渲染进程中使用,改用 ses 来执行代码 // vm 模块将无法在渲染进程中使用,改用 ses 来执行代码
window.evalCodeInSandbox = (code, userVars = {}) => { window.evalCodeInSandbox = (code, userVars = {}) => {
let sandbox = getSandboxFuns() let sandboxWithUV = Object.assign(userVars, getSandboxFuns())
let sandboxWithUV = Object.assign(userVars, sandbox)
try { try {
return new Compartment(sandboxWithUV).evaluate(code); return new Compartment(sandboxWithUV).evaluate(code);
} catch (error) { } catch (error) {

View File

@ -16,12 +16,19 @@
> >
</q-toolbar> </q-toolbar>
<q-card-section class="row items-center"> <q-card-section class="row items-center">
<iframe
ref="iframe"
:srcdoc="runResult"
frameborder="0"
v-if="htmlOutput"
></iframe>
<pre <pre
v-else
:class="{ :class="{
'text-red': !runResultStatus, 'text-red': !runResultStatus,
result: 1, result: 1,
}" }"
v-html="runResult" v-text="runResult"
></pre> ></pre>
</q-card-section> </q-card-section>
<q-card-actions align="right"> <q-card-actions align="right">
@ -31,14 +38,21 @@
</q-dialog> </q-dialog>
</div> </div>
<div v-else> <div v-else>
<iframe
ref="iframe"
:srcdoc="runResult"
frameborder="0"
v-if="htmlOutput"
></iframe>
<pre <pre
v-else
v-show="!!runResult" v-show="!!runResult"
:class="{ :class="{
'text-red': !runResultStatus, 'text-red': !runResultStatus,
'q-pa-md': 1, 'q-pa-md': 1,
result: 1, result: 1,
}" }"
v-html="runResult" v-text="runResult"
></pre> ></pre>
</div> </div>
</div> </div>
@ -49,7 +63,6 @@
import outputTypes from "../js/options/outputTypes.js"; import outputTypes from "../js/options/outputTypes.js";
import specialVars from "../js/options/specialVars.js"; import specialVars from "../js/options/specialVars.js";
import commandTypes from "../js/options/commandTypes.js"; import commandTypes from "../js/options/commandTypes.js";
import { event } from "quasar";
export default { export default {
data() { data() {
@ -61,6 +74,7 @@ export default {
listener: null, listener: null,
history: [], history: [],
historyIdx: null, historyIdx: null,
htmlOutput: false,
}; };
}, },
props: { props: {
@ -80,6 +94,9 @@ export default {
needTempPayload() { needTempPayload() {
return ["edit", "new", "config"].includes(this.action.type); return ["edit", "new", "config"].includes(this.action.type);
}, },
iframeCtw() {
return this.$refs?.iframe?.contentWindow;
},
}, },
methods: { methods: {
// //
@ -91,6 +108,7 @@ export default {
}, },
async fire(currentCommand) { async fire(currentCommand) {
currentCommand.cmd = this.assignSpecialVars(currentCommand.cmd); currentCommand.cmd = this.assignSpecialVars(currentCommand.cmd);
this.htmlOutput = currentCommand.output === "html";
let { hideWindow, outPlugin, action } = let { hideWindow, outPlugin, action } =
outputTypes[currentCommand.output]; outputTypes[currentCommand.output];
// //
@ -203,6 +221,7 @@ export default {
// //
showRunResult(content, isSuccess, action) { showRunResult(content, isSuccess, action) {
this.isResultShow = true; this.isResultShow = true;
this.setIframe();
this.runResultStatus = isSuccess; this.runResultStatus = isSuccess;
let contlength = content?.length || 0; let contlength = content?.length || 0;
if (contlength > this.resultMaxLength) if (contlength > this.resultMaxLength)
@ -239,6 +258,17 @@ export default {
document.removeEventListener("keydown", this.listener, true); document.removeEventListener("keydown", this.listener, true);
} }
}, },
setIframe() {
this.$nextTick(() => {
if (!this.iframeCtw) return;
let ctx = {
quickcommand,
utools,
parent: undefined,
};
Object.assign(this.iframeCtw, _.cloneDeep(ctx));
});
},
}, },
unmounted() { unmounted() {
this.stopRun(); this.stopRun();
@ -253,4 +283,8 @@ export default {
max-width: 100%; max-width: 100%;
margin: 0; margin: 0;
} }
iframe {
width: 100%;
height: 550px;
}
</style> </style>

View File

@ -80,7 +80,8 @@
<q-card-section class="q-gutter-sm"> <q-card-section class="q-gutter-sm">
<div>uTools 会员插件会员均可享受本插件的会员功能</div> <div>uTools 会员插件会员均可享受本插件的会员功能</div>
<div class="text-weight-bolder"> <div class="text-weight-bolder">
本插件会员仅需 {{ memberPrice }} 一次性付费uTools 会员享 9 折优惠 本插件会员仅需 {{ memberPrice }} 一次性付费uTools 会员享 9
折优惠
</div> </div>
<div> <div>
会员功能将在保障用户完整的插件功能体验的前提下提供以下个性化功能 会员功能将在保障用户完整的插件功能体验的前提下提供以下个性化功能
@ -158,7 +159,7 @@ export default {
}, },
methods: { methods: {
getUserInfo() { getUserInfo() {
Object.assign(this.userInfo, utools.getUser()); Object.assign(this.userInfo, this.$root.utools.whole.getUser());
let statisticsData = this.$root.utools.getDB( let statisticsData = this.$root.utools.getDB(
this.$root.utools.DBPRE.CFG + "statisticsData" this.$root.utools.DBPRE.CFG + "statisticsData"
); );

View File

@ -22,7 +22,7 @@ const outputTypes = {
name: "text", name: "text",
label: "纯文本输出", label: "纯文本输出",
icon: "text_snippet", icon: "text_snippet",
action: result => window.htmlEncode(result) action: result => result
}, },
html: { html: {
name: "html", name: "html",