better-staridc-MNBT
1<template>
2 <div class="td-plugin-page">
3 <div class="td-plugin-head">
4 <div class="td-plugin-title">
5 <i class="mdi mdi-puzzle"></i>
6 <span>{{ pageTitle }}</span>
7 </div>
8 <div class="td-plugin-actions">
9 <t-button theme="default" variant="outline" size="small" @click="reload" :loading="loading">
10 <i class="mdi mdi-refresh"></i> 刷新
11 </t-button>
12 <t-button theme="default" variant="outline" size="small" @click="openInNewTab">
13 <i class="mdi mdi-open-in-new"></i> 新窗口
14 </t-button>
15 </div>
16 </div>
17 <div class="td-plugin-frame-wrap">
18 <div v-if="loading" class="td-plugin-loading">
19 <t-loading text="加载中…" size="large" />
20 </div>
21 <iframe
22 v-show="!loading"
23 ref="iframeRef"
24 :src="iframeSrc"
25 @load="onLoad"
26 frameborder="0"
27 class="td-plugin-iframe"
28 ></iframe>
29 </div>
30 </div>
31</template>
32
33<script setup>
34import { ref, computed, watch, onMounted, onBeforeUnmount, nextTick } from 'vue'
35import { useRoute } from 'vue-router'
36
37const route = useRoute()
38const iframeRef = ref(null)
39const loading = ref(true)
40const pageTitle = ref('插件页面')
41
42const iframeSrc = computed(() => {
43 const p = route.query.p || ''
44 const page = route.query.page || ''
45 if (!p) return ''
46 let url = './plugin.php?p=' + encodeURIComponent(p)
47 if (page) url += '&page=' + encodeURIComponent(page)
48 return url
49})
50
51function onLoad() {
52 loading.value = false
53 adjustHeight()
54 extractTitle()
55}
56
57function adjustHeight() {
58 const iframe = iframeRef.value
59 if (!iframe) return
60 try {
61 const doc = iframe.contentDocument || iframe.contentWindow.document
62 if (!doc) return
63 // 重置 height 为 auto 以获取真实高度
64 iframe.style.height = 'auto'
65 const height = Math.max(
66 doc.body.scrollHeight,
67 doc.documentElement.scrollHeight,
68 doc.body.offsetHeight,
69 doc.documentElement.offsetHeight,
70 )
71 iframe.style.height = (height + 20) + 'px'
72 } catch (e) {
73 // 跨域无法读取,设置一个默认高度
74 iframe.style.height = '80vh'
75 }
76}
77
78function extractTitle() {
79 const iframe = iframeRef.value
80 if (!iframe) return
81 try {
82 const doc = iframe.contentDocument || iframe.contentWindow.document
83 if (!doc) return
84 const title = doc.title || doc.querySelector('h1, h2, h3')?.textContent
85 if (title) pageTitle.value = title
86 } catch (e) {
87 // 跨域忽略
88 }
89}
90
91function reload() {
92 loading.value = true
93 if (iframeRef.value) {
94 iframeRef.value.src = iframeSrc.value
95 }
96}
97
98function openInNewTab() {
99 if (iframeSrc.value) {
100 window.open(iframeSrc.value, '_blank')
101 }
102}
103
104// 监听窗口大小变化,重新调整高度
105function onResize() {
106 adjustHeight()
107}
108
109// 监听路由参数变化,重新加载
110watch(
111 () => route.fullPath,
112 () => {
113 if (iframeSrc.value) {
114 loading.value = true
115 nextTick(() => {
116 if (iframeRef.value) {
117 iframeRef.value.src = iframeSrc.value
118 }
119 })
120 }
121 },
122)
123
124onMounted(() => {
125 window.addEventListener('resize', onResize)
126 // 从路由参数初始化标题
127 const p = route.query.p || ''
128 const page = route.query.page || ''
129 if (p && page) {
130 pageTitle.value = p + ' / ' + page
131 } else if (p) {
132 pageTitle.value = p
133 }
134})
135
136onBeforeUnmount(() => {
137 window.removeEventListener('resize', onResize)
138})
139</script>
140
141<style scoped>
142.td-plugin-page {
143 display: flex;
144 flex-direction: column;
145 height: 100%;
146 background: var(--td-bg);
147}
148.td-plugin-head {
149 display: flex;
150 align-items: center;
151 justify-content: space-between;
152 padding: 14px 20px;
153 background: var(--td-surface);
154 border-bottom: 1px solid var(--td-border);
155 gap: 12px;
156}
157.td-plugin-title {
158 display: flex;
159 align-items: center;
160 gap: 8px;
161 font-size: 15px;
162 font-weight: 600;
163 color: var(--td-text);
164}
165.td-plugin-title i {
166 font-size: 18px;
167 color: var(--td-brand);
168}
169.td-plugin-actions {
170 display: flex;
171 gap: 8px;
172}
173.td-plugin-frame-wrap {
174 flex: 1;
175 position: relative;
176 overflow: hidden;
177}
178.td-plugin-loading {
179 position: absolute;
180 inset: 0;
181 display: grid;
182 place-items: center;
183 background: var(--td-bg);
184}
185.td-plugin-iframe {
186 width: 100%;
187 height: 100%;
188 border: none;
189 display: block;
190 min-height: calc(100vh - var(--td-header-height) - 56px);
191}
192</style>