类:WebRequest

    Process: Main
    此类不从 'electron' 模块导出. 它只能作为Electron API中其他方法的返回值。

    使用 SessionWebRequest 属性访问 WebRequest 类的实例。

    WebRequest 下的所有方法接收 filterlistener 两个参数。 当 API 中有事件被触发时, listener(details) 会被调用, details 中包含了请求的详细信息。

    ⚠️ Only the last attached listener will be used. Passing null as listener will unsubscribe from the event.

    filter 对象具有一个 url 属性, 它是一个 url 模式数组, 用于筛选出与 url 模式不匹配的请求。 如果省略 filter, 则所有请求都将匹配。

    对于某些事件, listener 是通过 callback 传递的, 当 listener 完成其工作时, 应使用 response 对象进行调用。

    以下事件会在Session实例触发。

    webRequest.onBeforeRequest([filter, ]listener)

    • filter WebRequestFilter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • uploadData
      • callback Function
        • response Object
          • cancel Boolean (可选)
          • redirectURL String (可选) - 原始请求被阻止发送或完成,而不是重定向到给定的URL。

    当请求即将发生时,调用listener(details, callback)

    uploadDataUploadData对象的一个数组。

    必须使用 response 对象调用callback

    Some examples of valid urls:

    1. 'http://foo:1234/'
    2. 'http://foo.com/'
    3. 'http://foo:1234/bar'
    4. '*://*/*'
    5. '*://example.com/*'
    6. '*://example.com/foo/*'
    7. 'http://*.foo:1234/'
    8. 'file://foo:1234/bar'
    9. 'http://foo:*/'
    10. '*://www.foo.com/'

    webRequest.onBeforeSendHeaders([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • requestHeaders Record<string, string>

    一旦请求头可用,在发送 HTTP 请求之前,listener 将以 listener(details, callback) 的形式被调用。 这可能发生在对服务器进行 TCP 连接之后,但在发送任何HTTP数据之前。

    webRequest.onSendHeaders([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • requestHeaders Record<string, string>

    在请求发送到服务器之前,listener将以listener(details)的形式被调用,在该侦听器被出发前,上一个对 onBeforeSendHeaders 响应的修改是可见的。

    webRequest.onHeadersReceived([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • statusLine String
        • statusCode Integer
        • responseHeaders Record<string, string[]> (optional)
      • callback Function
        • headersReceivedResponse Object
          • cancel Boolean (可选)
          • responseHeaders Record<string, string | string[]> (optional) - When provided, the server is assumed to have responded with these headers.
          • statusLine String (optional) - Should be provided when overriding responseHeaders to change header status otherwise original response header’s status will be used.

    当HTTP请求接收到报头后,会通过调用 listener(details, callback)方法来触发listener

    The callback has to be called with a response object.

    webRequest.onResponseStarted([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • Record<string, string[]> (optional)
        • fromCache Boolean - 表明响应是取自硬盘缓存。
        • statusLine String

    当收到响应体的第一个字节时, 将以 listener(details) 的形式来调用 listener。 对于 HTTP 请求而言,这意味着此时 HTTP 状态行和回应头已经可以读取了。

    webRequest.onBeforeRedirect([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • redirectURL String
        • statusCode Integer
        • statusLine String
        • ip String (可选) - 请求实际发送到的服务器 IP 地址。
        • fromCache Boolean
        • responseHeaders Record<string, string[]> (optional)

    当服务器的初始重定向即将发生时,将以 listener(details)的方式调用listener

    webRequest.onCompleted([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • responseHeaders Record<string, string[]> (optional)
        • fromCache Boolean
        • statusCode Integer
        • statusLine String
        • error String

    当请求完成时,将以 listener(details)的方式调用listener

    webRequest.onErrorOccurred([filter, ]listener)

    • filter (optional)
    • listener Function | null
      • details Object
        • id Integer
        • url String
        • method String
        • webContentsId Integer (可选)
        • webContents WebContents (optional)
        • frame WebFrameMain (optional)
        • resourceType String - Can be mainFrame, subFrame, stylesheet, script, image, font, object, xhr, ping, cspReport, media, webSocket or other.
        • referrer String
        • timestamp Double
        • fromCache Boolean
        • error String - 错误描述.