-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browserAction): Quick Upload PoC
Upload action opens temporary tab which in turn opens file picker. There are two hacks/workarounds: 1) Upload is done in temporary tab due to WebExtension bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1292701 2) Browserified Buffer is included twice because ipfs.add expects it BUT js-ipfs-api does not seem to expose own embedded version: ipfs-inactive/js-ipfs-http-client#528 Anyway, this cool PoC closes #184
- Loading branch information
Showing
9 changed files
with
92 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<link rel="stylesheet" href="browser-action.css"/> | ||
</head> | ||
|
||
<body> | ||
<img src="../../icons/ipfs-logo-on.svg" id="icon"/> | ||
<p> | ||
<span id="quickUploadMessage" data-i18n="panel_quick-upload"></span><input type="file" id="quickUploadInput" style="display: none" /> | ||
</p> | ||
<script src="../lib/data-i18n.js"></script> | ||
<script src="../lib/npm/buffer.js"></script> | ||
<script src="quick-upload.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
/* eslint-env browser, webextensions */ | ||
|
||
const Buffer = window.Buffer.Buffer // workaround for https://github.com/ipfs/js-ipfs-api/issues/528 | ||
const quickUploadInput = document.getElementById('quickUploadInput') | ||
const quickUploadMessage = document.getElementById('quickUploadMessage') | ||
|
||
function onQuickUploadInputChange (event) { | ||
const file = event.target.files[0] | ||
browser.runtime.getBackgroundPage() | ||
.then(bg => { | ||
let reader = new FileReader() | ||
reader.onloadend = () => { | ||
const buffer = Buffer.from(reader.result) | ||
bg.ipfs.add(buffer, (err, result) => { | ||
if (err || !result) { | ||
// keep upload tab and display error message in it | ||
quickUploadMessage.innerHTML = `Unable to upload to IPFS API: <br><code><pre>${err}</pre></code>` | ||
} else { | ||
// close upload tab as it will be replaced with a new tab with uploaded content | ||
browser.tabs.getCurrent().then(tab => { | ||
browser.tabs.remove(tab.id) | ||
}) | ||
} | ||
// execute handler | ||
return bg.uploadResultHandler(err, result) | ||
}) | ||
} | ||
reader.readAsArrayBuffer(file) | ||
}) | ||
.catch(error => { console.error(`Unable to perform quick upload due to ${error}`) }) | ||
} | ||
|
||
function initUpload () { | ||
quickUploadInput.onchange = onQuickUploadInputChange | ||
quickUploadInput.click() | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', initUpload) |
This file contains 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