From 8fa856158a20ae4ffc763970c61a040e99b0d73f Mon Sep 17 00:00:00 2001 From: mauve Date: Tue, 7 Jul 2020 21:39:36 -0400 Subject: [PATCH] New style, saveAs, improve focus, prevent duplicating history --- README.md | 8 ++++-- app/pages/welcome.html | 25 ++++++++++++++-- app/protocols/index.js | 12 +++++--- app/ui/context-menus.js | 48 +++++++++++++++++++++++++++++++ app/ui/electron-browser-view.js | 4 +++ app/ui/index.html | 51 +++++++++++++++++++++++++++------ app/ui/script.js | 9 ++++-- app/ui/welcome.html | 39 ------------------------- 8 files changed, 137 insertions(+), 59 deletions(-) delete mode 100644 app/ui/welcome.html diff --git a/README.md b/README.md index ca46131..1eb9eac 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ PRs for more protocols are welcome. - Autocomplete URLs from history (type in the URL bar) - Persist open windows when quitting - Basic Chrome Extension support (hardcoded into the source for now) +- Save files from pages (any protocol) ![Agregore demo](agregore-demo-1.gif) @@ -58,7 +59,7 @@ Feel free to open a Github issue if you wish to tackle one of the items on the r This project uses the [StandardJS](https://standardjs.com/) code style. Please format your code with `standard --fix` or run `npm run lint`. -## Roadmap +## Roadmap / TODOs - [x] Basic browser features - [x] Navigate to URL @@ -75,7 +76,7 @@ This project uses the [StandardJS](https://standardjs.com/) code style. Please f - [x] Dev tools - [x] `ctrl+[` and `ctrl+]` for navigating history - [x] `ctrl+l` for selecting the navigation bar - - [ ] saveAs context menu (using fetch and fs.createWriteStream()) + - [x] saveAs context menu (using fetch and fs.createWriteStream()) - [x] Persist windows on application quit - [ ] fetch API for hyperdrives [GH issue](https://github.com/cliqz-oss/dat-webext/issues/159) - [ ] Creating an archive (scoped to page origin) @@ -110,6 +111,9 @@ This project uses the [StandardJS](https://standardjs.com/) code style. Please f - [ ] Developer options page - [ ] Track extensions in a DB - [ ] Drag and drop extension folder +- [ ] Configure top-level page to load from URL + - [ ] Give access to Electron APIs + - [ ] Shortcut to agregore libraries like `electron-browser-view.js' - [ ] Password / Account management for web pages [using native OS APIs](https://github.com/atom/node-keytar) - [ ] Private browsing mode - [ ] PWA support diff --git a/app/pages/welcome.html b/app/pages/welcome.html index 871708d..c8b8968 100644 --- a/app/pages/welcome.html +++ b/app/pages/welcome.html @@ -8,6 +8,14 @@ diff --git a/app/protocols/index.js b/app/protocols/index.js index e0514a7..2ca4e1a 100644 --- a/app/protocols/index.js +++ b/app/protocols/index.js @@ -25,18 +25,22 @@ function registerPriviledges () { ]) } -async function setupProtocols ({ protocol }) { +async function setupProtocols (session) { + const { protocol: sessionProtocol } = session + app.setAsDefaultProtocolClient('hyper') app.setAsDefaultProtocolClient('dat') const hyperProtocolHandler = await createHyperHandler() - protocol.registerStreamProtocol('hyper', hyperProtocolHandler) + sessionProtocol.registerStreamProtocol('hyper', hyperProtocolHandler) + globalProtocol.registerStreamProtocol('hyper', hyperProtocolHandler) const browserProtocolHandler = await createBrowserHandler() - protocol.registerStreamProtocol('agregore-browser', browserProtocolHandler) + sessionProtocol.registerStreamProtocol('agregore-browser', browserProtocolHandler) const datProtocolHandler = await createDatHandler() - protocol.registerStreamProtocol('dat', datProtocolHandler) + sessionProtocol.registerStreamProtocol('dat', datProtocolHandler) + globalProtocol.registerStreamProtocol('dat', datProtocolHandler) /* app.setAsDefaultProtocolClient('ipfs') diff --git a/app/ui/context-menus.js b/app/ui/context-menus.js index 877ae8f..1157f9f 100644 --- a/app/ui/context-menus.js +++ b/app/ui/context-menus.js @@ -16,6 +16,8 @@ exports.pageContextMenu = function (event, params) { navigationGroup(this.webContents, params), historyBufferGroup(params), linkGroup(params), + linkGroup(params), + saveGroup(params), editGroup(params), developmentGroup(this.webContents, params) ]) @@ -138,3 +140,49 @@ function linkGroup ({ linkURL }) { }) ] } + +function saveGroup ({ srcURL }) { + return !srcURL.length ? null : [ + new MenuItem({ + label: 'Save As', + click: (_, browserWindow) => saveAs(srcURL, browserWindow) + }) + ] +} + +async function saveAs (link, browserWindow) { + const fs = remote.require('fs') + const path = remote.require('path').posix + const pump = require('pump') + const { dialog } = remote + const { Readable } = require('stream') + + const name = path.basename(link) + + const response = await window.fetch(link) + + const { filePath } = await dialog.showSaveDialog(browserWindow, { + defaultPath: name + }) + + if (!filePath) return + + pump( + Readable.from(consumeBody(response.body)), + fs.createWriteStream(filePath) + ) +} + +async function * consumeBody (body) { + const reader = body.getReader() + + try { + const { done, value } = await reader.read() + + if (done) return + + yield value + } finally { + reader.releaseLock() + } +} diff --git a/app/ui/electron-browser-view.js b/app/ui/electron-browser-view.js index d4d6c3f..f4439df 100644 --- a/app/ui/electron-browser-view.js +++ b/app/ui/electron-browser-view.js @@ -164,6 +164,10 @@ class BrowserViewElement extends HTMLElement { for (const name of BrowserViewElement.METHODS()) { this[name] = (...args) => this.view.webContents[name](...args) } + + this.addEventListener('focus', () => { + if (this.view) this.view.webContents.focus() + }) } connectedCallback () { diff --git a/app/ui/index.html b/app/ui/index.html index afd2f2b..10b0c72 100644 --- a/app/ui/index.html +++ b/app/ui/index.html @@ -8,6 +8,13 @@ @@ -78,6 +111,6 @@ - + diff --git a/app/ui/script.js b/app/ui/script.js index 0ca2483..9141503 100644 --- a/app/ui/script.js +++ b/app/ui/script.js @@ -179,6 +179,11 @@ function looksLikeDomain (string) { } function navigateTo (url) { - webview.src = url - webview.focus() + if (webview.getURL() === url) { + console.log('Reloading') + webview.reload() + } else { + webview.src = url + webview.focus() + } } diff --git a/app/ui/welcome.html b/app/ui/welcome.html deleted file mode 100644 index 4504fed..0000000 --- a/app/ui/welcome.html +++ /dev/null @@ -1,39 +0,0 @@ - -Agregore Browser - Welcome - - - - - - - - - -

Agregore

- -

-Check out some of these dweb links. -

- - - -

- Otherwise, try entering a hyper:// or dat:// URL in the address bar. -