|
| 1 | +const fs = require('electron').remote.require('fs'); |
| 2 | +const os = require('electron').remote.require('os'); |
| 3 | +const path = require('electron').remote.require('path'); |
| 4 | +const {BrowserWindow} = require('electron').remote; |
| 5 | +const {clipboard} = require('electron'); |
| 6 | + |
| 7 | +const SERVER_URL = 'https://hackmd.io'; |
| 8 | + |
| 9 | +const isDarwin = os.platform() === 'darwin'; |
| 10 | + |
| 11 | +const winOption = { |
| 12 | + width: 1024, |
| 13 | + height: 768 |
| 14 | +} |
| 15 | + |
| 16 | +onload = () => { |
| 17 | + if (window.location.search !== '') { |
| 18 | + targetURL = window.location.search.match(/\?target=([^?]+)/)[1]; |
| 19 | + } else { |
| 20 | + targetURL = SERVER_URL; |
| 21 | + } |
| 22 | + |
| 23 | + document.body.innerHTML += `<webview src="${targetURL}" id="main-window" disablewebsecurity autosize="on" allowpopups allowfileaccessfromfiles></webview>`; |
| 24 | + |
| 25 | + const webview = document.getElementById("main-window"); |
| 26 | + |
| 27 | + webview.addEventListener('dom-ready', function(){ |
| 28 | + // set webview title |
| 29 | + document.querySelector('#navbar-container .title').innerHTML = webview.getTitle(); |
| 30 | + |
| 31 | + // set dark theme if in home page |
| 32 | + if (webview.getURL().split('?')[0].split('#')[0].match(/https:\/\/hackmd.io\/$/)) { |
| 33 | + document.querySelector('navbar').className = 'dark'; |
| 34 | + } else { |
| 35 | + document.querySelector('navbar').className = ''; |
| 36 | + } |
| 37 | + |
| 38 | + /* bind control buttons event */ |
| 39 | + document.querySelector('#navbar-container .navigate-back').onclick = () => { |
| 40 | + if (webview.canGoBack()) { |
| 41 | + webview.goBack(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + document.querySelector('#navbar-container .navigate-foward').onclick = () => { |
| 46 | + if (webview.canGoForward()) { |
| 47 | + webview.goForward(); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + document.querySelector('#navbar-container .home').onclick = () => { |
| 52 | + webview.loadURL(SERVER_URL); |
| 53 | + } |
| 54 | + |
| 55 | + document.querySelector('#navbar-container .refresh').onclick = () => { |
| 56 | + webview.loadURL(webview.getURL()); |
| 57 | + } |
| 58 | + |
| 59 | + document.querySelector('#navbar-container .title').onclick = () => { |
| 60 | + clipboard.writeText(webview.getURL()); |
| 61 | + new Notification('URL copied', { title: 'URL copied', body: webview.getURL() }); |
| 62 | + } |
| 63 | + |
| 64 | + // webview.openDevTools(); |
| 65 | + }); |
| 66 | + |
| 67 | + /* handle _target=blank pages */ |
| 68 | + webview.addEventListener('new-window', (event) => { |
| 69 | + new BrowserWindow( |
| 70 | + (isDarwin |
| 71 | + ? Object.assign({}, winOption, {titleBarStyle: 'hidden'}) |
| 72 | + : winOption) |
| 73 | + ).loadURL(`file://${path.join(__dirname, `index.html?target=${event.url}`)}`); |
| 74 | + }); |
| 75 | +} |
0 commit comments