- session·续1
- ses.setPermissionRequestHandler(handler)*handlerFunction | null
- ses.setPermissionCheckHandler(handler)*handlerFunction<Boolean> | null
- ses.clearHostResolverCache()ReturnsPromise- Resolves when the operation is complete.
- ses.allowNTLMCredentialsForDomains(domains)*domainsString - 一个逗号分隔的服务器列表, 用于收集已经启用身份验证的服务器。
- ses.setUserAgent(userAgent[, acceptLanguages])*userAgentString
- ses.getUserAgent()返回String- 当前会话的 user agent.
- ses.getBlobData(identifier)*identifierString - 有效的 UUID.
- ses.downloadURL(url)*urlString
- ses.createInterruptedDownload(options)*optionsObject
- ses.clearAuthCache(options)*options(RemovePassword | RemoveClientCertificate)
- ses.setPreloads(preloads)*preloadsString[] - 数组,该数组由所有需要进行预加载的脚本的绝对路径组成。
- ses.getPreloads()返回String[]返回一个数组,这个数组由已经注册过的预加载脚本的路径组成。
- ses.setSpellCheckerLanguages(languages)*languagesString[] - An array of language codes to enable the spellchecker for.
- ses.getSpellCheckerLanguages()ReturnsString[]- An array of language codes the spellchecker is enabled for. If this list is empty the spellchecker will fallback to usingen-US. By default on launch if this setting is an empty list Electron will try to populate this setting with the current OS locale. This setting is persisted across restarts.
- ses.setSpellCheckerDictionaryDownloadURL(url)*urlString - A base URL for Electron to download hunspell dictionaries from.
- ses.addWordToSpellCheckerDictionary(word)
- 实例属性
session·续1
调用 setCertificateVerifyProc(null)将恢复为默认证书验证过程。
const { BrowserWindow } = require('electron')
let win = new BrowserWindow()
win.webContents.session.setCertificateVerifyProc((request, callback) => {
const { hostname } = request
if (hostname === 'github.com') {
callback(0)
} else {
callback(-2)
}
})
ses.setPermissionRequestHandler(handler)*handlerFunction | null
webContentsWebContents - 请求权限的WebContents。 Please note that if the request comes from a subframe you should userequestingUrlto check the request origin. permissionString - 枚举 'media', 'geolocation', 'notifications', 'midiSysex', 'pointerLock', 'fullscreen', 'openExternal'. callbackFunction permissionGrantedBoolean - 允许或拒绝该权限. detailsObject - Some properties are only available on certain permission types. externalURLString (optional) - The url of theopenExternalrequest. mediaTypesString[] (optional) - The types of media access being requested, elements can bevideooraudiorequestingUrlString - The last URL the requesting frame loaded *isMainFrameBoolean - Whether the frame making the request is the main frame
设置可用于响应 session 的权限请求的处理程序。 调用 callback(true)将允许该权限, 调用 callback(false)将拒绝它。 若要清除处理程序, 请调用 setPermissionRequestHandler (null) 。
const { session } = require('electron')
session.fromPartition('some-partition').setPermissionRequestHandler((webContents, permission, callback) => {
if (webContents.getURL() === 'some-host' && permission === 'notifications') {
return callback(false) // denied.
}
callback(true)
})
ses.setPermissionCheckHandler(handler)*handlerFunction<Boolean> | null
webContentsWebContents - WebContents checking the permission. Please note that if the request comes from a subframe you should userequestingUrlto check the request origin. permissionString - Enum of 'media'. requestingOriginString - The origin URL of the permission check detailsObject - Some properties are only available on certain permission types. securityOriginString - The security orign of themediacheck. mediaTypeString - The type of media access being requested, can bevideo,audioorunknownrequestingUrlString - The last URL the requesting frame loaded isMainFrameBoolean - Whether the frame making the request is the main frame
Sets the handler which can be used to respond to permission checks for thesession. Returningtruewill allow the permission andfalsewill reject it. To clear the handler, callsetPermissionCheckHandler(null).
const { session } = require('electron')
session.fromPartition('some-partition').setPermissionCheckHandler((webContents, permission) => {
if (webContents.getURL() === 'some-host' && permission === 'notifications') {
return false // denied
}
return true
})
ses.clearHostResolverCache()ReturnsPromise- Resolves when the operation is complete.
清除主机解析程序的缓存。
ses.allowNTLMCredentialsForDomains(domains)*domainsString - 一个逗号分隔的服务器列表, 用于收集已经启用身份验证的服务器。
动态设置是否始终为 HTTP NTLM 发送凭据或协商身份验证。
const { session } = require('electron')
// 以 "example.com"、"foobar.com"、"baz" 结尾的 url 用于身份验证。
session.defaultSession.allowNTLMCredentialsForDomains('*example.com, *foobar.com, *baz')
// 所有的 url 都可以用作身份验证
session.defaultSession.allowNTLMCredentialsForDomains('*')
ses.setUserAgent(userAgent[, acceptLanguages])*userAgentString
*acceptLanguagesString (可选)
覆盖当前会话的userAgent和acceptLanguages.acceptLanguages必须是用逗号分隔的语言代码列表,例如“en-US,fr,de,ko,zh-CN,ja”.
这不会影响现有的WebContents, 并且每个WebContents都可以使用webContents.setUserAgent重写会话范围的user agent。
ses.getUserAgent()返回String- 当前会话的 user agent.
ses.getBlobData(identifier)*identifierString - 有效的 UUID.
ReturnsPromise- resolves with blob data.
ses.downloadURL(url)*urlString
Initiates a download of the resource aturl. The API will generate a DownloadItem that can be accessed with the will-download event.
Note: This does not perform any security checks that relate to a page's origin, unlike webContents.downloadURL.
ses.createInterruptedDownload(options)*optionsObject
pathString - 下载的绝对路径. urlChainString[] - 完整的 url 下载地址. mimeTypeString (可选) offsetInteger - 下载的开始范围. lengthInteger - 下载的总长度。 lastModifiedString (optional) - Last-Modified header value. eTagString (optional) - ETag header value. startTimeDouble (optional) - 下载的时间是从 UNIX 时代以来的秒数开始的。
允许从上一个Session恢复 cancelled 或 interrupted 下载。 该 API 将生成一个 DownloadItem , 可使用 will-download 事件进行访问。 DownloadItem 将不具有与之关联的任何 WebContents , 并且初始状态将为 interrupted 。 只有在 DownloadItem 上调用 resume API 时, 才会启动下载。
ses.clearAuthCache(options)*options(RemovePassword | RemoveClientCertificate)
ReturnsPromise- resolves when the session’s HTTP authentication cache has been cleared.
ses.setPreloads(preloads)*preloadsString[] - 数组,该数组由所有需要进行预加载的脚本的绝对路径组成。
Adds scripts that will be executed on ALL web contents that are associated with this session just before normalpreloadscripts run.
ses.getPreloads()返回String[]返回一个数组,这个数组由已经注册过的预加载脚本的路径组成。
ses.setSpellCheckerLanguages(languages)*languagesString[] - An array of language codes to enable the spellchecker for.
The built in spellchecker does not automatically detect what language a user is typing in. In order for the spell checker to correctly check their words you must call this API with an array of language codes. You can get the list of supported language codes with theses.availableSpellCheckerLanguagesproperty.
Note: On macOS the OS spellchecker is used and will detect your language automatically. This API is a no-op on macOS.
ses.getSpellCheckerLanguages()ReturnsString[]- An array of language codes the spellchecker is enabled for. If this list is empty the spellchecker will fallback to usingen-US. By default on launch if this setting is an empty list Electron will try to populate this setting with the current OS locale. This setting is persisted across restarts.
Note: On macOS the OS spellchecker is used and has it's own list of languages. This API is a no-op on macOS.
ses.setSpellCheckerDictionaryDownloadURL(url)*urlString - A base URL for Electron to download hunspell dictionaries from.
By default Electron will download hunspell dictionaries from the Chromium CDN. If you want to override this behavior you can use this API to point the dictionary downloader at your own hosted version of the hunspell dictionaries. We publish ahunspell_dictionaries.zipfile with each release which contains the files you need to host here, the file server must be case insensitive you must upload each file twice, once with the case it has in the ZIP file and once with the filename as all lower case.
If the files present inhunspell_dictionaries.zipare available athttps://example.com/dictionaries/language-code.bdic` then you should call this api with ses.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/'). Please note the trailing slash. The URL to the dictionaries is formed as ${url}${filename}.
Note: On macOS the OS spellchecker is used and therefore we do not download any dictionary files. This API is a no-op on macOS.
ses.addWordToSpellCheckerDictionary(word)
word String - The word you want to add to the dictionary
Returns Boolean - Whether the word was successfully written to the custom dictionary.
*Note: On macOS and Windows 10 this word will be written to the OS custom dictionary as well
实例属性
以下属性在` Session </ 0>实例上可用:
