問題描述
我正在開發一個托管在 SSO 環境中的電腦上的測試項目.我實際上指向的是托管在此環境中的服務器上的網頁,我需要訪問 cookie(剛剛得到它們)和請求的 http 標頭.我研究了文檔,似乎沒有 loadUrl()
方法的回調函數,也沒有我可以用來獲取這些標頭的選項.
I'm working on a test project hosted on a pc included in a SSO environment.
I'm actually pointing to a webpage hosted on a server inside this environment and I need to access the cookies (just got them) and the http headers of the request.
I studied the doc and it seems there isn't a callback function for the loadUrl()
method nor an option I can use to get those headers.
參考鏈接
https://electronjs.org/docs/api/browser-window
loadUrl
有第二個可選參數,定義為 Electron.loadURLOption,但它似乎沒有幫助
loadUrl
has a second optional parameter that is defined as an Electron.loadURLOption but it doesn't seem to be helpful
interface LoadURLOptions {
/**
* An HTTP Referrer url.
*/
httpReferrer?: (string) | (Referrer);
/**
* A user agent originating the request.
*/
userAgent?: string;
/**
* Extra headers separated by "
"
*/
extraHeaders?: string;
postData?: (UploadRawData[]) | (UploadFile[]) | (UploadBlob[]);
/**
* Base url (with trailing path separator) for files to be loaded by the data url.
* This is needed only if the specified url is a data url and needs to load other
* files.
*/
baseURLForDataURL?: string;
}
如果有任何幫助,我將不勝感激,謝謝
I'd appreciate any kind of help, thanks
推薦答案
您可以通過為 Web 上下文會話的 onCompleted
或 onHeadersReceived
回調注冊一個偵聽器來實現此目的webRequest
屬性,例如:
You can achieve this by registering a listener for the onCompleted
or onHeadersReceived
callback of the web context session's webRequest
property, eg:
const url = 'https://example.com/your/page';
browserWindow.webContents.session.webRequest.onCompleted({ urls: [url] }, (details) => {
// Access request headers via details.requestHeaders
// Access response headers via details.responseHeaders
});
browserWindow.loadURL(url);
onHeadersReceived
有點棘手,因為它需要調用傳遞的回調才能繼續頁面加載過程.
The onHeadersReceived
is a little trickier as it requires invoking the passed callback in order to continue the page loading process.
相關文檔
這篇關于Electron - 從 BrowserWindow 的 loadUrl() 中檢索 HTTP 響應標頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!