fix(electron): 标签页内打开同域名链接时追加用户身份

Excel 预览等通过 openWindow 打开的 tab 模式窗口,其 setWindowOpenHandler
直接以原始 URL 创建新标签,未经过页面的 __onBeforeOpenWindow / userUrl,
客户端使用本地页面时新标签读不到登录态,/single/file/xxx 分享链接提示
"请登录后继续..." 或 "身份已失效"。

- web-tab-manager:tab 模式先交给 onBeforeOpenWindow 判断,同域名链接
  经 userUrl 追加身份;页面要求默认浏览器打开的交给系统;页面无该能力
  (如外站)时仍直接新开标签
- utils.onBeforeOpenWindow:透传页面返回值,以区分"要求默认浏览器打开"
  与"页面没有 __onBeforeOpenWindow",原调用方行为不变
This commit is contained in:
kuaifan 2026-09-30 23:16:37 +00:00
parent e45608df36
commit 4115291bff
2 changed files with 12 additions and 4 deletions

View File

@ -332,14 +332,14 @@ const utils = {
* 新窗口打开事件
* @param webContents
* @param url
* @returns {Promise<unknown>}
* @returns {Promise<unknown>} 页面未接管时 resolve;resolve(false) 表示页面明确要求用默认浏览器打开,resolve(undefined) 表示页面没有 __onBeforeOpenWindow
*/
onBeforeOpenWindow(webContents, url) {
return new Promise(resolve => {
const dataStr = JSON.stringify({url: url})
webContents.executeJavaScript(`if(typeof window.__onBeforeOpenWindow === 'function'){window.__onBeforeOpenWindow(${dataStr})}`, true).then(options => {
if (options !== true) {
resolve()
resolve(options)
}
}).catch(_ => {
resolve()

View File

@ -745,8 +745,16 @@ function createWebTabView(windowId, args) {
renderer.openExternal(url).catch(() => {})
})
} else if (url && url !== 'about:blank') {
// tab 模式下创建新标签
createWebTabWindow({ url, afterId: browserView.webContents.id, windowId: browserView.webTabWindowId })
// tab 模式下交给页面判断(同域名链接会追加用户身份后再打开),页面无此能力时直接创建新标签
const afterId = browserView.webContents.id
const windowId = browserView.webTabWindowId
utils.onBeforeOpenWindow(browserView.webContents, url).then(handled => {
if (handled === false) {
renderer.openExternal(url).catch(() => {})
} else {
createWebTabWindow({ url, afterId, windowId })
}
})
}
return { action: 'deny' }
})