-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the ability to restore tokens from a JSON text file
previous methodology: ===================== * popup window: - dynamically creates an input file element - click event triggers the element to open the file chooser dialog - processes the list of selected files by: * reading the content of each file * parsing its JSON * passing the resulting object in a message to the background page * background page: - merges backup data with the tokens already saved in local storage - updates local storage with this new aggregate value - updates the popup window problem with previous methodology: ================================== * only works if the browser doesn't close the popup window before the data is passed to the background page * many browsers do close the popup window when the file input dialog is triggered for selection of input files new methodology: ================ * popup window: - sends a message to the background page * background page: - opens a new tab and loads a static html page * static html page: - dynamically creates an input file element - adds a click event handler to the element, which requires user interaction to trigger - the click event handler processes the list of selected files by: * reading the content of each file * parsing its JSON * passing the resulting object in a message to the background page - the click event handler also tracks the count of files pending * after the processing of all files is complete, sends a final message to the background page * background page: - merges backup data with the tokens already saved in local storage - updates local storage with this new aggregate value - closes the tab containing the static html page comparison between methodologies: ================================= * previous methodology: - pros: * simpler implementation * more elegant user experience - cons: * doesn't work in many browsers * new methodology: - pros: * works in all supported browsers - cons: * much more complicated implementation * less elegant user experience, which requires interaction with a standalone page in a new tab
- Loading branch information
1 parent
8283f46
commit 0ab56ce
Showing
9 changed files
with
163 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "privacy-pass", | ||
"version": "3.6.5", | ||
"version": "3.6.6", | ||
"private": true, | ||
"contributors": [ | ||
"Suphanat Chunhapanya <[email protected]>", | ||
|
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,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</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
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,78 @@ | ||
function handleBackupFileImport(event: Event): void { | ||
event.stopPropagation(); | ||
event.stopImmediatePropagation(); | ||
|
||
const input: HTMLInputElement = <HTMLInputElement>event.target; | ||
const files: FileList | null = input.files; | ||
if (files === null) return; | ||
|
||
let remaining_files = files.length; | ||
|
||
const onFileReadComplete = () => { | ||
remaining_files--; | ||
|
||
if (remaining_files <= 0) { | ||
chrome.runtime.sendMessage({ restore: true, tab: { close: true } }); | ||
} | ||
} | ||
|
||
for (let file_index=0; file_index < files.length; file_index++) { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = function(){ | ||
try { | ||
if ((typeof reader.result === 'string') && (reader.result.length > 0)) { | ||
const backupJSON: string = reader.result; | ||
const backup: {[key: string]: string[]} = JSON.parse(backupJSON); | ||
|
||
chrome.runtime.sendMessage({ restore: true, backup }); | ||
} | ||
} | ||
catch(e) {} | ||
onFileReadComplete(); | ||
}; | ||
|
||
reader.onerror = onFileReadComplete; | ||
reader.onabort = onFileReadComplete; | ||
|
||
reader.readAsText( | ||
files[file_index] | ||
); | ||
} | ||
} | ||
|
||
|
||
window.addEventListener('DOMContentLoaded', (event) => { | ||
event.stopPropagation(); | ||
event.stopImmediatePropagation(); | ||
|
||
const appName = chrome.i18n.getMessage('appName'); | ||
const ctaRestorePasses = chrome.i18n.getMessage('ctaRestorePasses'); | ||
|
||
window.document.title = appName + ': ' + ctaRestorePasses; | ||
|
||
const input = window.document.createElement('input'); | ||
input.setAttribute('type', 'file'); | ||
input.setAttribute('accept', 'text/plain, application/json, .txt, .json'); | ||
input.setAttribute('multiple', ''); | ||
input.addEventListener('change', handleBackupFileImport); | ||
|
||
const root = window.document.getElementById('root'); | ||
if (root !== null) { | ||
const heading = window.document.createElement('h2'); | ||
heading.appendChild( | ||
window.document.createTextNode(appName) | ||
); | ||
|
||
const subheading = window.document.createElement('h3'); | ||
subheading.appendChild( | ||
window.document.createTextNode(ctaRestorePasses) | ||
); | ||
|
||
root.appendChild(heading); | ||
root.appendChild(subheading); | ||
root.appendChild(input); | ||
} | ||
|
||
input.click(); | ||
}); |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"isolatedModules": false | ||
}, | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"." | ||
] | ||
} |
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 |
---|---|---|
|
@@ -36,6 +36,9 @@ | |
}, | ||
{ | ||
"path": "./src/popup" | ||
}, | ||
{ | ||
"path": "./src/restore" | ||
} | ||
] | ||
} |
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