Created
January 9, 2019 08:15
-
-
Save vviikk/757d11dca95378be57bb6e5b3132dc63 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
<style> | |
.tabbar { | |
position: absolute; | |
left: 0; | |
top: 0; | |
bottom: 0; | |
width: 50px; | |
margin: 0; | |
padding: 0; | |
display: flex; | |
flex-direction: column; | |
} | |
.tabbar button { | |
width: 50px; | |
height: 50px; | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
<!-- All of the Node.js APIs are available in this renderer process. --> | |
We are using Node.js <script>document.write(process.versions.node)</script>, | |
Chromium <script>document.write(process.versions.chrome)</script>, | |
and Electron <script>document.write(process.versions.electron)</script>. | |
<script type="module"> | |
import {html, render} from 'https://unpkg.com/lit-html?module'; | |
// You can also require other files to run in this process | |
require('./renderer.js')(html, render) | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const {app, BrowserWindow, BrowserView, ipcMain, session } = require('electron') | |
const path = require('path') | |
const url = require('url') | |
// 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, viewManager | |
console.log(process.execPath) | |
app.setAppUserModelId(process.execPath) | |
ipcMain.on('move-to-tab', (evt, idx) => viewManager.moveToView(idx)) | |
ipcMain.on('init-services', (evt, services) => { | |
console.log('services to init', services) | |
viewManager = new ViewManager(mainWindow, services) | |
}) | |
const resetUserAgent = () => { | |
console.log('am i running') | |
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => { | |
details.requestHeaders['User-Agent'] = OPTIONS.viewOptions.userAgent | |
callback({ cancel: false, requestHeaders: details.requestHeaders }); | |
}); | |
} | |
function createWindow () { | |
// Create the browser window. | |
mainWindow = new BrowserWindow({width: 800, height: 600}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
// Open the DevTools. | |
mainWindow.webContents.openDevTools() | |
// Emitted when the window is closed. | |
mainWindow.on('closed', function () { | |
// Dereference the window object, usually you would store windows | |
// in an array if your app supports multi windows, this is the time | |
// when you should delete the corresponding element. | |
mainWindow = null | |
}) | |
// let view = new BrowserView({ | |
// webPreferences: { | |
// nodeIntegration: false, | |
// }, | |
// }) | |
// view.setBounds({ x: 0, y: 0, | |
// width: 800, | |
// height: 550, | |
// }) | |
// view.setAutoResize({ width: true, height: true }) | |
// // mainWindow.setBrowserView(view) | |
// view.webContents.loadURL('http://localhost:3000/not') | |
// view.webContents.executeJavaScript(` | |
// var base = document.createElement('base'); | |
// base.href = "http://localhost:3000/not"; | |
// document.getElementsByTagName('head')[0].appendChild(base); | |
// const OldNotification = window.Notification | |
// class Notification extends OldNotification { | |
// constructor(...args) { | |
// console.log(args) | |
// super(...args) | |
// } | |
// } | |
// window.Notification = Notification | |
// setTimeout(function(){ | |
// console.log("hello world") | |
// }, 3000)`, function(){console.log ('finish')}) | |
// view.webContents.openDevTools() | |
// view.webContents.on('console-message', console.log) | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.on('ready', createWindow) | |
// Quit when all windows are closed. | |
app.on('window-all-closed', function () { | |
// On OS X it is common for applications and their menu bar | |
// to stay active until the user quits explicitly with Cmd + Q | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
app.on('activate', function () { | |
// On OS X it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (mainWindow === null) { | |
createWindow() | |
} | |
}) | |
// In this file you can include the rest of your app's specific main process | |
// code. You can also put them in separate files and require them here. | |
class ViewManager { | |
constructor(browserWindow, services = []) { | |
this.mainWindow = browserWindow | |
this.views = [] | |
this.decorateURL = _ => _ | |
services.forEach(this.addView.bind(this)) | |
} | |
moveToView(idx) { | |
this.mainWindow.setBrowserView(this.views[idx]) | |
} | |
addView(url) { | |
const view = new BrowserView({ | |
webPreferences: { | |
nodeIntegration: false, | |
}, | |
}) | |
view.setBounds({ x: 50, y: 0, | |
width: 750, | |
height: 550, | |
}) | |
view.setAutoResize({ width: true, height: true }) | |
this.views.push(view) | |
mainWindow.setBrowserView(view) | |
view.webContents.loadURL(this.decorateURL(url)) | |
view.webContents.executeJavaScript(`setTimeout(function(){ | |
console.log("hello world") | |
}, 3000)`, function(){console.log ('finish')}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcRenderer } = require('electron') | |
module.exports = function (html, render) { | |
// 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 services = [ | |
'https://web.whatsapp.com', | |
'https://messenger.com' | |
] | |
ipcRenderer.send('init-services', services) | |
const moveToTab = idx => () => { | |
console.log(idx) | |
ipcRenderer.send('move-to-tab', idx) | |
} | |
const getIcon = url => url.split('https://')[1].split()[0] | |
const btnTemplate = idx => html`<button @click=${moveToTab(idx)}>${idx}</button>` | |
// Define a template | |
const tabs = (tabs) => html` | |
<ul class='tabbar'> | |
${services.map((service, index) => html`<li>${btnTemplate(index)}</li>`)} | |
</ul>`; | |
// Render the template to the document | |
render(tabs(services), document.body); | |
let myNotification = new Notification('Title', { | |
body: 'Lorem Ipsum Dolor Sit Amet' | |
}) | |
myNotification.onclick = () => { | |
console.log('Notification clicked') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment