New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
indexeddb-files: example using the IndexedDB API to store and retrieve files #171
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# indexeddb-files | ||
|
||
This add-on shows how to read and save files using the IndexedDB API and the subset of the "File and Directory Entries API" supported on Firefox. | ||
|
||
## What it does ## | ||
|
||
The extension includes: | ||
|
||
* a browser action which opens a popup with a list of examples | ||
* 2 file manipulations examples (that are opened in a tab by clicking their related link) | ||
|
||
## What it shows ## | ||
|
||
- **Save files into IndexedDB**: which shows how to read a local directory tree and save all its content into IndexedDB files, then read files from indexedDB and download them as a single zip files. | ||
- **Create temporary files**: which shows how to use the MutableFile API exposed by the IndexedDB API to create mutable files and save them using the IndexedDB API. | ||
|
||
## More docs on MDN ## | ||
|
||
- File and Directory Entries API: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API/Firefox_support | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory | ||
* https://developer.mozilla.org/en-US/docs/Web/API/File | ||
* https://developer.mozilla.org/en-US/docs/Web/API/Blob | ||
* https://developer.mozilla.org/en-US/docs/Web/API/FileReader | ||
|
||
- IndexedDB API: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API | ||
|
||
- IndexedDB MutableFile API: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/IDBMutableFile | ||
|
||
- Quota limits and Storage types: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria#Firefox_specifics | ||
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API#Storage_limits_and_eviction_criteria | ||
|
||
## Acknowledgements | ||
|
||
* WebExtension icon courtesy of [icons8.com](http://icons8.com). |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
{ | ||
"description": "Save and Load Files using IndexedDB", | ||
"manifest_version": 2, | ||
"name": "indexeddb-fs", | ||
"version": "1.0", | ||
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/indexeddb-fs-webext", | ||
"icons": { | ||
"48": "icons/files-50.png" | ||
}, | ||
|
||
"browser_action": { | ||
"default_icon": { | ||
"48": "icons/files-50.png" | ||
}, | ||
"default_title": "IndexedDB Files", | ||
"default_popup": "popup.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,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>IndexedDB files handling demos</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<h4>IndexedDB files handling demos:</h4> | ||
<ol> | ||
<li><a href="#" id="reading-saving-files-example">Read a local directory tree and save its content into IndexedDB</a></li> | ||
<li><a href="#" id="mutable-files-example">Create temporary files and save them using IndexedDB</a></li> | ||
</ol> | ||
<script src="popup.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,7 @@ | ||
document.querySelector("#reading-saving-files-example").onclick = () => { | ||
browser.tabs.create({url: "/tabs/save-local-dirtree.html", active: true}); | ||
}; | ||
|
||
document.querySelector("#mutable-files-example").onclick = () => { | ||
browser.tabs.create({url: "/tabs/mutablefile.html", active: true}); | ||
}; |
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,44 @@ | ||
// Log text and images utilities. | ||
const dbLogsEl = document.querySelector("#indexed-logs"); | ||
|
||
document.querySelector("#clear-logs").onclick = () => { | ||
while (dbLogsEl.firstChild) { | ||
dbLogsEl.removeChild(dbLogsEl.firstChild); | ||
} | ||
}; | ||
|
||
function dbLogHTMLElement(el) { | ||
dbLogsEl.appendChild(el); | ||
|
||
// Scroll to the bottom automatically. | ||
dbLogsEl.scrollTop = dbLogsEl.scrollHeight; | ||
} | ||
|
||
function dbLog(...args) { | ||
dbLogHTMLElement(document.createTextNode('\n' + args.join(" "))); | ||
} | ||
|
||
function logIDBFileLink({prefixText, fileIDBKey, suffixText, onClickHandler }) { | ||
if (prefixText) { | ||
dbLogHTMLElement(document.createTextNode(prefixText)); | ||
} | ||
|
||
const linkEl = document.createElement("a"); | ||
linkEl.setAttribute("href", "#"); | ||
linkEl.textContent = fileIDBKey; | ||
if (typeof onClickHandler == "function") { | ||
linkEl.onclick = () => { | ||
onClickHandler(fileIDBKey); | ||
}; | ||
} | ||
|
||
dbLogHTMLElement(linkEl); | ||
|
||
if (suffixText) { | ||
dbLogHTMLElement(document.createTextNode(suffixText)); | ||
} | ||
} | ||
|
||
function logImage(imageUrl) { | ||
|
||
} |
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,30 @@ | ||
.overflow-scroll { | ||
max-height: 200px; | ||
overflow: auto; | ||
border: 1px dashed grey; | ||
padding: 0.5em; | ||
} | ||
|
||
#logs-panel { | ||
background: #EEEEEE; | ||
padding: 2em; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
body { | ||
display: flex; | ||
} | ||
|
||
main { | ||
flex: 1 1 auto; | ||
} | ||
|
||
aside { | ||
flex: 1 1 auto; | ||
min-width: 50%; | ||
} | ||
|
||
details > div { | ||
padding: 1em; | ||
} |
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,69 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Create and save temporary files</title> | ||
<meta charset="utf-8"> | ||
<link rel="stylesheet" type="text/css" href="common-tab.css"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Create and save temporary files</h1> | ||
|
||
<p>Create a temporary file, give it some data and a name, then save it using indexedDB.</p> | ||
|
||
<details id="step-01"> | ||
<summary>1. Create a MutableFile</summary> | ||
<div> | ||
<div> | ||
<input type="button" id="create-file" value="Create a MutableFile" disabled> | ||
<input type="button" id="get-metadata" value="Get Metadata" disabled> | ||
</div> | ||
</div> | ||
</details> | ||
|
||
<details id="step-02"> | ||
<summary>2. Append data to the MutableFile</summary> | ||
<div> | ||
<div> | ||
<textarea id="file-data" placeholder="data to save into the file..." | ||
style="width: 100%;" disabled></textarea> | ||
|
||
<input type="button" id="save-data" value="Append data" disabled> | ||
</div> | ||
|
||
</div> | ||
</details> | ||
|
||
<details id="step-03"> | ||
<summary>3. Save to IndexedDB</summary> | ||
<div> | ||
<input id="filename-to-save" placeholder="filename to save..." disabled> | ||
<input type="button" id="persist-file" value="Save to IndexedDB key" disabled> | ||
</div> | ||
</details> | ||
|
||
<details id="step-04"> | ||
<summary>4. Retrieve saved files</summary> | ||
<div> | ||
<input type="button" id="list-saved-files" value="List all the saved files" disabled> | ||
|
||
<div> | ||
<input id="filename-to-read" placeholder="filename to read..." disabled> | ||
<input type="button" id="read-file" value="Read saved file" disabled> | ||
</div> | ||
</div> | ||
</details> | ||
</main> | ||
|
||
<aside> | ||
<div id="logs-panel"> | ||
<pre id="indexed-logs" class="overflow-scroll">Logs from the IndexedDB operations...</pre> | ||
|
||
<button id="clear-logs">Clear Logs</button> | ||
</div> | ||
</aside> | ||
|
||
<script src="common-log-helpers.js"></script> | ||
<script src="mutablefile.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a shame the MutableFile docs are so poor :(.