支持从加密聊天打开隔离浏览器链接

This commit is contained in:
xhgit committed 2026-11-13 23:27:56 +08:00
1 parent f4ea1f8e03
commit 6786158a3e
8 files changed
+54 -8

No files matched your search

+29 -1
View File
@@ -38,6 +38,31 @@ const lines = computed(() => {
(!favoritesOnly.value || line.favorite) &&
(!query || [line.text, line.attachment?.name, line.quote].some((part) => part?.toLowerCase().includes(query))))
})
const internalBrowser = window.xhchat?.openBrowser
function messageParts(text: string) {
const parts: { text: string; url?: string }[] = []
let offset = 0
for (const match of text.matchAll(/https?:\/\/[^\s<>"']+/giu)) {
const label = match[0].replace(/[.,!?;:,。!?;:、))\]】]+$/u, '')
try {
const url = new URL(label)
if (url.username || url.password || label.length > 4096 ||
(url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname))) continue
if (match.index > offset) parts.push({ text: text.slice(offset, match.index) })
parts.push({ text: label, url: url.href })
offset = match.index + label.length
} catch {
continue
}
}
if (offset < text.length) parts.push({ text: text.slice(offset) })
return parts
}
function openLink(url: string) {
if (internalBrowser) void execute(() => internalBrowser(url))
}
let timer: ReturnType<typeof setInterval>
async function execute(action: () => Promise<void>) {
@@ -235,7 +260,10 @@ onBeforeUnmount(() => clearInterval(timer))
<span v-if="line.recalled" class="muted">消息已撤回</span>
<button v-else-if="line.attachment" type="button" class="plain" :disabled="busy"
@click="saveAttachment(line.attachment)">下载 {{ line.attachment.name }}</button>
<span v-else>{{ line.text }}</span><small v-if="view?.unsent.includes(line.id)">待发送</small>
<span v-else><template v-for="(part, index) in messageParts(line.text)" :key="index"><button v-if="part.url && internalBrowser"
type="button" class="chat-link" @click="openLink(part.url)">{{ part.text }}</button><a v-else-if="part.url"
class="chat-link" :href="part.url" target="_blank" rel="noopener noreferrer">{{ part.text }}</a><template v-else>{{ part.text }}</template></template></span>
<small v-if="view?.unsent.includes(line.id)">待发送</small>
<small v-if="line.edited && !line.recalled">已编辑</small>
<div class="message-actions">
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy || Boolean(editing)" @click="reply = line">引用</button>