- webContents·续2
- Event: ‘did-attach-webview’
- Event: ‘console-message’
- Event: ‘preload-error’
- Event: ‘ipc-message’
- Event: ‘ipc-message-sync’
- 事件: ‘desktop-capturer-get-sources’
- 事件: ‘remote-require’
- 事件: ‘remote-get-global’
- 事件: ‘remote-get-builtin’
- 事件: ‘remote-get-current-window’
- 事件: ‘remote-get-current-web-contents’
- 事件: ‘remote-get-guest-web-contents’
- 实例方法
- contents.loadURL(url[, options])
- contents.loadFile(filePath[, options])
- contents.downloadURL(url)
- contents.getURL()
- contents.getTitle()
- contents.isDestroyed()
- contents.focus()
- contents.isFocused()
- contents.isLoading()
- contents.isLoadingMainFrame()
- contents.isWaitingForResponse()
- contents.stop()
- contents.reload()
- contents.reloadIgnoringCache()
- contents.canGoBack()
- contents.canGoForward()
- contents.canGoToOffset(offset)
- contents.clearHistory()
- contents.goBack()
- contents.goForward()
- contents.goToIndex(index)
- contents.goToOffset(offset)
- contents.isCrashed()
- contents.setUserAgent(userAgent)
- contents.getUserAgent()
- contents.insertCSS(css[, options])
- contents.removeInsertedCSS(key)
- contents.executeJavaScript(code[, userGesture])
webContents·续2
-
webPreferences WebPreferences - The web preferences that will be used by the guest page. This object can be modified to adjust the preferences for the guest page.
-
params Record - The other <webview> parameters such as the src URL. This object can be modified to adjust the parameters of the guest page.
Emitted when a <webview>‘s web contents is being attached to this web contents. Calling event.preventDefault() will destroy the guest page.
This event can be used to configure webPreferences for the webContents of a <webview> before it’s loaded, and provides the ability to set settings that can’t be set via <webview> attributes.
Note: The specified preload script option will be appear as preloadURL (not preload) in the webPreferences object emitted with this event.
Event: ‘did-attach-webview’
返回:
-
event Event
-
webContents WebContents - The guest web contents that is used by the <webview>.
当<webview>被挂载到页面内容中时,触发该事件。
Event: ‘console-message’
返回:
-
event Event
-
level Integer
-
message String
-
line Integer
-
sourceId String
Emitted when the associated window logs a console message.
Event: ‘preload-error’
返回:
-
event Event
-
preloadPath String
-
error Error
Emitted when the preload script preloadPath throws an unhandled exception error.
Event: ‘ipc-message’
返回:
-
event Event
-
channel String
-
...args any[]
Emitted when the renderer process sends an asynchronous message via ipcRenderer.send().
Event: ‘ipc-message-sync’
返回:
-
event Event
-
channel String
-
...args any[]
Emitted when the renderer process sends a synchronous message via ipcRenderer.sendSync().
事件: ‘desktop-capturer-get-sources’
返回:
- event Event
Emitted when desktopCapturer.getSources() is called in the renderer process. Calling event.preventDefault() will make it return empty sources.
事件: ‘remote-require’
返回:
-
event IpcMainEvent
-
moduleName String
Emitted when remote.require() is called in the renderer process. 调用 event.preventDefault() 将阻止模块返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-global’
返回:
-
event IpcMainEvent
-
globalName String
Emitted when remote.getGlobal() is called in the renderer process. 调用 event.preventDefault() 将阻止全局返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-builtin’
返回:
-
event IpcMainEvent
-
moduleName String
Emitted when remote.getBuiltin() is called in the renderer process. 调用 event.preventDefault() 将阻止模块返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-current-window’
返回:
- event IpcMainEvent
Emitted when remote.getCurrentWindow() is called in the renderer process. 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-current-web-contents’
返回:
- event IpcMainEvent
Emitted when remote.getCurrentWebContents() is called in the renderer process. 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-guest-web-contents’
返回:
-
event IpcMainEvent
-
guestWebContents WebContents
Emitted when <webview>.getWebContents() is called in the renderer process. 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
实例方法
contents.loadURL(url[, options])
-
url String
-
options Object (optional)
-
httpReferrer (String | Referrer) (可选) - 一个 HTTP Referrer url。
-
userAgent String (可选) - 发起请求的 userAgent.
-
extraHeaders String (optional) - Extra headers separated by “\n”.
-
postData (UploadRawData[] | UploadFile[] | UploadBlob[]) (可选)
-
baseURLForDataURL String (可选) - 要加载的数据文件的根 url(带有路径分隔符). 只有当指定的 url是一个数据 url 并需要加载其他文件时,才需要这样做。
Returns Promise<void> - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load). A noop rejection handler is already attached, which avoids unhandled rejection errors.
Loads the url in the window. The url must contain the protocol prefix, e.g. the http:// or file://. If the load should bypass http cache then use the pragma header to achieve it.
const { webContents } = require('electron')const options = { extraHeaders: 'pragma: no-cache\n' }webContents.loadURL('https://github.com', options)
contents.loadFile(filePath[, options])
-
filePath String
-
options Object (optional)
-
query Record (optional) - Passed to url.format().
-
search String (可选) - 传递给 url.format().
-
hash String (可选) - 传递给 url.format().
Returns Promise<void> - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).
Loads the given file in the window, filePath should be a path to an HTML file relative to the root of your application. For instance an app structure like this:
| root| - package.json| - src| - main.js| - index.html
需要运行以下代码:
win.loadFile('src/index.html')
contents.downloadURL(url)
- url String
Initiates a download of the resource at url without navigating. The will-download event of session will be triggered.
contents.getURL()
Returns String - 当前页面的URL.
const { BrowserWindow } = require('electron')let win = new BrowserWindow({ width: 800, height: 600 })win.loadURL('http://github.com')let currentURL = win.webContents.getURL()console.log(currentURL)
contents.getTitle()
返回 String - 当前页面的标题.
contents.isDestroyed()
返回 Boolean -判断页面是否被销毁
contents.focus()
页面聚焦
contents.isFocused()
返回 Boolean - 判断页面是否聚焦
contents.isLoading()
返回 Boolean - 判断页面是否正在加载资源
contents.isLoadingMainFrame()
Returns Boolean - Whether the main frame (and not just iframes or frames within it) is still loading.
contents.isWaitingForResponse()
Returns Boolean - Whether the web page is waiting for a first-response from the main resource of the page.
contents.stop()
Stops any pending navigation.
contents.reload()
刷新当前页面
contents.reloadIgnoringCache()
忽略缓存强制刷新页面
contents.canGoBack()
返回Boolean,是否可以返回到上一个页面
contents.canGoForward()
返回Boolean ,是否可以进入下一个页面
contents.canGoToOffset(offset)
- offset Integer
Returns Boolean - Whether the web page can go to offset.
contents.clearHistory()
Clears the navigation history.
contents.goBack()
使浏览器回退到上一个页面。
contents.goForward()
使浏览器前进到下一个页面。
contents.goToIndex(index)
- index Integer
Navigates browser to the specified absolute web page index.
contents.goToOffset(offset)
- offset Integer
定位到相对于“当前入口”的指定的偏移。
contents.isCrashed()
Returns Boolean - Whether the renderer process has crashed.
contents.setUserAgent(userAgent)
- userAgent String
重写该页面的user agent
过时的
contents.getUserAgent()
返回 String - 当前页面的user agent.
过时的
contents.insertCSS(css[, options])
-
css String
-
options Object (optional)
-
cssOrigin String (optional) - Can be either ‘user’ or ‘author’; Specifying ‘user’ enables you to prevent websites from overriding the CSS you insert. Default is ‘author’.
Returns Promise<String> - A promise that resolves with a key for the inserted CSS that can later be used to remove the CSS via contents.removeInsertedCSS(key).
Injects CSS into the current web page and returns a unique key for the inserted stylesheet.
contents.on('did-finish-load', function () { contents.insertCSS('html, body { background-color: #f00; }')})
contents.removeInsertedCSS(key)
- key String
Returns Promise<void> - Resolves if the removal was successful.
Removes the inserted CSS from the current web page. The stylesheet is identified by its key, which is returned from contents.insertCSS(css).
contents.on('did-finish-load', async function () { const key = await contents.insertCSS('html, body { background-color: #f00; }') contents.removeInsertedCSS(key)})
contents.executeJavaScript(code[, userGesture])
-
code String
-
userGesture Boolean (optional) - Default is false.
