web前端技術分享Electron之Renderer Process API
本文由小千給大家分享Renderer Process API,首先Renderer API 主要包括 remote、Browser window proxy、desktop Capture
Renderer Process API remote Browser Window Proxy desktop Capture
1、remote (服務端物件)
1.1 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<button type="button" name="button" id="test-button">Fullscreen</button><br>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <strong><script>document.write( process.versions.node)</script></strong>,
and Electron <strong><script>document.write( process.versions.electron )</script></strong>.
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
1.2 renderer.js
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const remote = require('electron').remote
const { app, dialog, BrowserWindow } = remote
const button = document.getElementById('test-button')
button.addEventListener('click', e => {
// dialog.showMessageBox({ message: 'Dialog invoked from Renderer process' })
// let secWin = new BrowserWindow({
// width: 400, height: 350
// })
// secWin.loadFile('index.html')
// console.log( remote.getGlobal('myglob') )
// app.quit()
let win = remote.getCurrentWindow()
win.maximize()
})
1.3 main.js
// Modules
const {app, BrowserWindow} = require('electron')
global['myglob'] = 'A var set in main.js'
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
// Create a new BrowserWindow when `app` is ready
function createWindow () {
mainWindow = new BrowserWindow({
width: 1000, height: 800,
webPreferences: { nodeIntegration: true }
})
// Load index.html into the new BrowserWindow
mainWindow.loadFile('index.html')
// Open DevTools - Remove for PRODUCTION!
mainWindow.webContents.openDevTools();
// Listen for window being closed
mainWindow.on('closed', () => {
mainWindow = null
})
}
// Electron `app` is ready
app.on('ready', createWindow)
// Quit when all windows are closed - (Not macOS - Darwin)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// When app icon is clicked and app is running, (macOS) recreate the BrowserWindow
app.on('activate', () => {
if (mainWindow === null) createWindow()
})
2、Browser Window Proxy (瀏覽器視窗代理)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<h3><a href="#" Window</a></h3>
<h3><a href="#" Window</a></h3>
<h3><a href="#" Fonts</a></h3>
<script>
let win
const newWin = () => {
win = window.open(' '_blank', 'width=500,height=450,alwaysOnTop=1')
}
const closeWin = () => {
win.close()
}
const styleWin = () => {
win.eval("document.getElementsByTagName('body')[0].style.fontFamily = 'Comic Sans MS'")
}
</script>
</body>
</html>
3、webFrame
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<img src=" alt=""><br>
<button Zoom</button>
<button Zoom</button>
<button Zoom</button>
<script>
const { webFrame } = require('electron')
const zoomUp = () => {
webFrame.setZoomLevel( webFrame.getZoomLevel() + 1 )
}
const zoomDown = () => {
webFrame.setZoomLevel( webFrame.getZoomLevel() - 1 )
}
const zoomReset = () => {
webFrame.setZoomLevel( 1 )
}
console.log( webFrame.getResourceUsage() )
</script>
</body>
</html>
4、desktopCapturer(桌面快照)
4.1 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<img width="100%" src="" id="screenshot"><br>
<button id="screenshot-button">Get Screenshot</button>
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
4.2 renderer.js
const { desktopCapturer } = require('electron')
document.getElementById('screenshot-button').addEventListener('click', () => {
desktopCapturer.getSources({ types:['window'], thumbnailSize: {width:1920, height:1080} }, (error, sources) => {
console.log(sources)
document.getElementById('screenshot').src = sources[0].thumbnail.toDataURL()
})
})
希望本文的分享能幫到大家!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31548651/viewspace-2775253/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- web前端培訓分享Electron之Main Process APIWeb前端AIAPI
- web前端技術分享Electron之IPC 通訊Web前端
- web前端培訓分享Electron之IPC 通訊Web前端
- web前端技術分享之頁面元素水平居中Web前端
- web前端技術分享:常用JavaScript框架有哪些?Web前端JavaScript框架
- web前端技術分享:使用react實現簡易路由Web前端React路由
- 經驗分享:如何系統學習 Web 前端技術?Web前端
- 前端技術演進(一):Web前端技術基礎前端Web
- 好程式設計師web前端技術分享css盒模型程式設計師Web前端CSS模型
- 如何在Web前端實現CAD圖文字全文搜尋功能之技術分享Web前端
- 如何提升web前端技術?Web前端
- Web前端是什麼?Web前端包括哪些技術?Web前端
- 好程式設計師web前端技術分享媒體查詢程式設計師Web前端
- web前端技術分享:koa中介軟體是如何實現的?Web前端
- web前端技術Mongoose詳解Web前端Go
- #web前端技術使用總結Web前端
- Web前端技術分享:全棧工程師常用的開發工具Web前端全棧工程師
- web前端技術分享:多行文字溢位問題解決方案Web前端
- web前端技術分享:詳解模組化require 和 import的區別Web前端UIImport
- Web前端技術分享:Javascript中的內建物件陣列講解Web前端JavaScript物件陣列
- web前端技術分享:前端開發與後端開發的區別是什麼?Web前端後端
- Web前端十種常用的技術Web前端
- web前端常用技術點001Web前端
- web前端必備技術有哪些?Web前端
- Web前端開發掌握的技術Web前端
- Web前端技術分享:什麼是塊元素?什麼是行內元素?Web前端
- Web前端技術分享:網頁排版佈局常見問題彙總Web前端網頁
- Web前端技術分享:學習HTML和CSS的5大理由Web前端HTMLCSS
- web技術分享| 虛擬列表實現Web
- 前端技術分享:JavaScript正則全面解析前端JavaScript
- 入門web前端需要掌握的技術Web前端
- 新手來看,常用的web前端技術Web前端
- 好程式設計師web前端技術分享移動端頁面佈局程式設計師Web前端
- 公司內部技術分享之Vue.js和前端工程化Vue.js前端
- 前端非同步技術之Promise前端非同步Promise
- 前端技術整理之fetch前端
- 好程式設計師web前端技術之CSS3過渡程式設計師Web前端CSSS3
- 好程式設計師web前端分享SDK與API之間的關係和聯絡程式設計師Web前端API