@@ -17,6 +17,8 @@ let difyfullScreenWindow = null
let drageWindow = null
let apiConfigWindow = null
let configWindow = null
let cookieLogTimer = null
import { clearAllSessionData } from './utils/cacheUtils.js'
// 权限授权
async function checkMediaAccess ( mediaType ) {
@@ -85,7 +87,18 @@ export async function createWindow() {
// 创建菜单
createMenu ( mainWindow , difyfullScreenWindow )
let code = ` localStorage.setItem("IsHsAiApp","IsHsAiApp");localStorage.setItem("HsAppCode", ${ import . meta . env . VITE _HsAppCode } ); `
const isDebugger = getStoreValue ( "isDebugger" ) ;
const localStorageStatements = [
` localStorage.setItem("IsHsAiApp","IsHsAiApp"); ` ,
` localStorage.setItem("HsAppCode", ${ import . meta . env . VITE _HsAppCode } ); ` ,
` localStorage.setItem("isDebugger", ${ isDebugger } ); `
] . join ( '\n' ) ;
let code = ` ${ localStorageStatements } ; ` ;
const isAdmin = getStoreValue ( "isAdmin" )
@@ -107,12 +120,17 @@ export async function createWindow() {
// 监听快捷键 F12 来打开/关闭 DevTools
mainWindow . webContents . on ( 'before-input-event' , ( event , input ) => {
const isDebugger = getStoreValue ( "isDebugger" ) ;
if ( isDebugger == 1 ) {
if ( input . key === 'F12' ) {
mainWindow . webContents . toggleDevTools ( )
} else if ( input . key === 'F5' ) {
logger . info ( '主 窗口 F5 快捷键触发' )
logger . info ( '全屏 窗口 F5 快捷键触发' )
mainWindow . reload ( )
}
}
} )
@@ -291,19 +309,30 @@ function registerShortcuts(window=null) {
logger . info ( ` F5快捷键注册 ${ success ? '成功' : '失败' } ` )
const isDebugger = getStoreValue ( "isDebugger" ) ;
if ( isDebugger == 1 ) {
const isRegistered _F12 = globalShortcut . isRegistered ( 'F12' ) ;
logger . info ( ` Is F12 registered: ${ isRegistered _F12 } ` ) ;
}
// 桌面端快要退出的时候,注销快捷键
app . on ( 'will-quit' , ( ) => {
globalShortcut . unregisterAll ( ) // 注销所有快捷键
} )
}
export async function createNewWindow ( url , access _token , refresh _token , sandbox = false ) {
const origin _url = url
logger . info ( '==========createNewWindow' )
logger . info ( '==========createNewWindow sandbox' , sandbox )
const csrf _token = getStoreValue ( "dify_csrf_token" )
await checkAndApplyDeviceAccessPrivilege ( )
@@ -313,6 +342,7 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
show : false ,
icon : icon ,
webPreferences : {
partition : 'persist:my-shared-session' ,
contextIsolation : false ,
nodeIntegration : true ,
nodeIntegrationInWorker : true ,
@@ -329,9 +359,25 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
registerShortcuts ( difyfullScreenWindow )
} )
let code = ` localStorage.setItem("IsHsAiApp","IsHsAiApp");localStorage.setItem("console_token"," ${ access _token } ");localStorage.setItem("refresh_token","12"); `
const isAdmin = getStoreValue ( "isAdmin" )
const hs _version = getStoreValue ( "hs_version" )
const dify _csrf _token = getStoreValue ( "dify_csrf_token" )
const cookieStatements = [
` document.cookie = "access_token= ${ access _token } ; path=/"; ` ,
` document.cookie = "refresh_token= ${ refresh _token } ; path=/"; ` ,
` document.cookie = "csrf_token= ${ dify _csrf _token } ; path=/"; ` ,
` document.cookie = "hs_version= ${ hs _version } ; path=/"; `
] . join ( '\n' ) ;
const localStorageStatements = [
` localStorage.setItem("IsHsAiApp", "IsHsAiApp"); ` ,
` localStorage.setItem("console_token", " ${ access _token } "); ` ,
` localStorage.setItem("refresh_token", "12"); `
] . join ( '\n' ) ;
let code = ` ${ localStorageStatements } \n ${ cookieStatements } ; ` ;
const isAdmin = getStoreValue ( "isAdmin" )
let display = '' ;
if ( isAdmin == true ) {
@@ -354,7 +400,7 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
`
}
code = code + ` document.cookie="hs_version= ${ hs _version } "; `
code = code + ` ; document.cookie="hs_version=${ hs _version } ; path=/ ";`
difyfullScreenWindow . webContents . on ( 'did-finish-load' , ( ) => {
difyfullScreenWindow . webContents
@@ -364,6 +410,7 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
. catch ( ( error ) => {
console . error ( 'Error:' , error )
} )
startCookieLogger ( 2000 )
let times = 0
let timer = setInterval ( ( ) => {
if ( times > 3 ) {
@@ -387,12 +434,16 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
// 监听快捷键 F12 来打开/关闭 DevTools
difyfullScreenWindow . webContents . on ( 'before-input-event' , ( event , input ) => {
const isDebugger = getStoreValue ( "isDebugger" ) ;
if ( isDebugger == 1 ) {
if ( input . key === 'F12' ) {
difyfullScreenWindow . webContents . toggleDevTools ( )
} else if ( input . key === 'F5' ) {
logger . info ( '全屏窗口 F5 快捷键触发' )
difyfullScreenWindow . reload ( )
}
}
} )
difyfullScreenWindow . webContents . setWindowOpenHandler ( ( details ) => {
@@ -403,6 +454,10 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
difyfullScreenWindow . on ( 'closed' , ( ) => {
logger . info ( '主窗口已关闭' )
difyfullScreenWindow = null
if ( cookieLogTimer ) {
clearInterval ( cookieLogTimer )
cookieLogTimer = null
}
} )
difyfullScreenWindow . webContents . on ( 'did-navigate' , ( event , url ) => {
@@ -544,6 +599,93 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f
}
/**
* 获取并打印全屏窗口的 Cookie 数据
* 每次调用从当前页面会话中读取 Cookie 并输出到控制台
*/
export async function logDifyFullScreenCookies ( ) {
try {
if ( ! difyfullScreenWindow || difyfullScreenWindow . isDestroyed ( ) ) {
return
}
const wc = difyfullScreenWindow . webContents
const currentURL = wc . getURL ( )
const sess = wc . session
let cookies = [ ]
if ( currentURL && typeof currentURL === 'string' && currentURL . length > 0 ) {
cookies = await sess . cookies . get ( { url : currentURL } )
} else {
cookies = await sess . cookies . get ( { } )
}
const simplified = cookies . map ( c => ( {
name : c . name ,
value : c . value ,
domain : c . domain ,
path : c . path ,
httpOnly : c . httpOnly ,
secure : c . secure ,
sameSite : c . sameSite
} ) )
logger . info ( 'difyfullScreenWindow cookies:' , simplified )
const needSync = new Set ( [ 'access_token' , 'csrf_token' , 'refresh_token' ] )
for ( const c of cookies ) {
if ( needSync . has ( c . name ) ) {
if ( c . name === 'access_token' ) {
setStoreValue ( 'dify_access_token' , c . value )
} else if ( c . name === 'csrf_token' ) {
setStoreValue ( 'dify_csrf_token' , c . value )
} else if ( c . name === 'refresh_token' ) {
setStoreValue ( 'dify_refresh_token' , c . value )
}
await setCookieToMainWindow ( c . name , c . value )
}
}
} catch ( error ) {
logger . error ( '获取 Cookie 失败:' , error )
}
}
/**
* 启动 Cookie 轮询打印
* 以指定间隔定时获取并打印 Cookie, 默认 2000ms
* 如果已在运行则不会重复启动
*/
export function startCookieLogger ( intervalMs = 2000 ) {
if ( cookieLogTimer ) {
return
}
cookieLogTimer = setInterval ( ( ) => {
logDifyFullScreenCookies ( )
} , intervalMs )
}
/**
* 将指定名称和值的 Cookie 写入主窗口
*/
async function setCookieToMainWindow ( name , value ) {
try {
if ( ! mainWindow || mainWindow . isDestroyed ( ) ) {
return
}
const targetBase = getStoreValue ( 'h5_client_url' ) || ''
const targetUrl = targetBase ? ` ${ targetBase } / ` : ( mainWindow . webContents . getURL ( ) || 'http://localhost/' )
const msess = mainWindow . webContents . session
await msess . cookies . set ( {
url : targetUrl ,
name ,
value ,
path : '/'
} )
await mainWindow . webContents . executeJavaScript ( ` document.cookie = " ${ name } = ${ value } ; path=/"; ` )
logger . info ( ` 已写入主窗口 Cookie: ${ name } ` )
} catch ( error ) {
logger . error ( '写入主窗口 Cookie 失败:' , error )
}
}
export function createDrageWindow ( ) {
logger . info ( '开始创建悬浮窗口' )
@@ -564,6 +706,7 @@ export function createDrageWindow() {
alwaysOnTop : true ,
autoHideMenuBar : true ,
webPreferences : {
partition : 'persist:my-shared-session' ,
contextIsolation : false ,
nodeIntegration : true ,
nodeIntegrationInWorker : true ,
@@ -761,6 +904,7 @@ export function createConfigWindow() {
parent : getMainWindow ( ) ,
modal : true ,
webPreferences : {
partition : 'persist:my-shared-session' ,
contextIsolation : false ,
nodeIntegration : true ,
nodeIntegrationInWorker : true ,