Tauri 中嵌入百度网页:从 iframe 到 Webview 的迁移实践
问题描述
在开发 Tauri 桌面应用时,我们曾尝试在一个插件窗口中嵌入百度首页。最初使用 iframe 实现,但遇到了点击无响应的问题。最终通过迁移到 Tauri 的 Webview API 成功解决。
问题背景
我们的应用基于 Tauri 2.0 + Vue 3 + TypeScript 技术栈。需求是在 src/plugins/baidu/index.vue 中实现一个显示百度首页的插件窗口,同时保留窗口控制按钮(最小化、最大化、关闭)。
初次尝试:使用 iframe
实现代码
<template>
<main data-tauri-drag-region>
<ActionBar
:shrink="false"
:max-w="true"
:icon-color="'black'"
:top-win-label="WebviewWindow.getCurrent().label"
:current-label="WebviewWindow.getCurrent().label"
/>
<iframe src="https://www.baidu.com" frameborder="0"></iframe>
</main>
</template>
<script setup lang="ts">
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
</script>
遇到的问题
实现后,用户反馈:打开百度网页之后点击没反应。
问题分析
通过分析发现,问题出在 iframe 的跨域限制上:
- X-Frame-Options 限制:百度在响应头中设置了
X-Frame-Options: SAMEORIGIN,禁止在 iframe 中嵌入。 - 跨域安全策略:现代浏览器出于安全考虑,阻止了 iframe 内的点击事件。
- 用户体验差:即使能加载,iframe 内的交互也会受到各种限制。
iframe 方案在嵌入第三方网站(尤其是大型网站如百度)时存在根本性限制,不是技术实现的问题,而是浏览器安全策略的限制。
解决方案:使用 Tauri Webview API
研究现有实现
为了找到正确的实现方式,我研究了项目中其他使用外部链接的插件,比如动态内容插件和聊天机器人插件。在 src/components/rightBox/chatBox/Bot.vue 中找到了关键实现逻辑。
Webview API 的优势
相比 iframe,Tauri Webview API 具有以下优势:
- 无跨域限制:使用系统级 webview 组件,不受浏览器同源策略限制。
- 完整交互支持:支持所有正常的网页交互(点击、输入、导航等)。
- 更好的性能:原生组件,性能优于 iframe。
- 灵活的窗口管理:可以精确控制位置、大小、焦点等。
实现迁移
首先,我们需要将 iframe 替换为一个容器 div,用于挂载 Webview。
<template>
<main data-tauri-drag-region>
<ActionBar
:shrink="false"
:max-w="true"
:icon-color="'black'"
:top-win-label="currentWindow.label"
:current-label="currentWindow.label"
/>
<div data-tauri-drag-region>
<div ref="webviewContainer"></div>
</div>
</main>
</template>
接下来是核心的 Webview 创建逻辑。这里需要注意获取正确的窗口实例,并处理生命周期。

