* 设置请求拦截器 * @returns 返回监听器函数
(
sess: Electron.Session,
headers: Record<string, string>
)
| 134 | * @returns 返回监听器函数 |
| 135 | */ |
| 136 | private setupRequestInterceptor( |
| 137 | sess: Electron.Session, |
| 138 | headers: Record<string, string> |
| 139 | ): |
| 140 | | (( |
| 141 | details: Electron.OnBeforeSendHeadersListenerDetails, |
| 142 | callback: (response: Electron.BeforeSendResponse) => void |
| 143 | ) => void) |
| 144 | | null { |
| 145 | try { |
| 146 | // 使用 onBeforeSendHeaders 拦截请求并修改请求头 |
| 147 | const listener = ( |
| 148 | details: Electron.OnBeforeSendHeadersListenerDetails, |
| 149 | callback: (response: Electron.BeforeSendResponse) => void |
| 150 | ): void => { |
| 151 | // 合并用户设置的请求头 |
| 152 | const requestHeaders = { |
| 153 | ...details.requestHeaders, |
| 154 | ...headers |
| 155 | } |
| 156 | |
| 157 | callback({ |
| 158 | requestHeaders |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | sess.webRequest.onBeforeSendHeaders( |
| 163 | { |
| 164 | urls: ['http://*/*', 'https://*/*'] |
| 165 | }, |
| 166 | listener |
| 167 | ) |
| 168 | |
| 169 | return listener |
| 170 | } catch (error) { |
| 171 | console.error('[PluginHttp] 设置请求拦截器失败:', error) |
| 172 | return null |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * 移除请求拦截器 |