Skip to content

Commit 4bbd56e

Browse files
authored
Fix JSON parsing issues when copy & pasting wallet data from PDF (#2355)
* remove any newline characters when users pastes wallet jsons * make regex for cleaning text more comprehensive
1 parent 8f9c95f commit 4bbd56e

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

src/cryptoadvance/specter/templates/wallet/new_wallet/components/import_wallet_account_map.jinja

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<textarea class="mt-3" id="txt" name="wallet_data" placeholder="Enter your wallet data here"></textarea>
1313

1414
<div class="popup hidden rounded-md overflow-hidden" id="popup">
15-
<video muted playsinline id="qr-video"></video>
15+
<video muted playsinline id="qr-video"></video>
1616
</div>
1717

1818
<div>
@@ -34,7 +34,7 @@
3434

3535

3636
<script type="module">
37-
import QrScanner from "{{ url_for('static', filename='qr/qr-scanner.min.js') }}";
37+
import QrScanner from "{{ url_for('static', filename='qr/qr-scanner.min.js') }}";
3838
document.addEventListener("DOMContentLoaded", function(){
3939
QrScanner.WORKER_PATH = "{{ url_for('static', filename='qr/qr-scanner-worker.min.js') }}";
4040
@@ -68,31 +68,45 @@
6868

6969
<script type="text/javascript">
7070
document.addEventListener("DOMContentLoaded", function(){
71-
var el = document.getElementById("file");
72-
var txt = document.getElementById("txt");
71+
var el = document.getElementById("file");
72+
const txtTextarea = document.getElementById("txt");
7373
74-
if (el != null) {
75-
el.addEventListener("change", (e) => {
76-
files = e.currentTarget.files;
77-
console.log(files);
74+
if (el != null) {
75+
el.addEventListener("change", (e) => {
76+
files = e.currentTarget.files;
77+
console.log(files);
7878
for(let i=0; i<files.length; i++){
79-
console.log(files[i].name);
80-
let reader = new FileReader();
81-
reader.onload = function(e) {
82-
document.getElementById("txt").value = reader.result;
83-
}
84-
reader.readAsText(files[i]);
85-
}
86-
});
87-
}
88-
});
89-
90-
function onCancelOverlay() {
79+
console.log(files[i].name);
80+
let reader = new FileReader();
81+
reader.onload = function (e) {
82+
txtTextarea.value = reader.result;
83+
}
84+
reader.readAsText(files[i]);
85+
}
86+
});
87+
}
88+
89+
txtTextarea.addEventListener("paste", function (event) {
90+
event.preventDefault()
91+
const clipboardData = event.clipboardData || window.clipboardData
92+
const pastedText = clipboardData.getData("text/plain")
93+
const cleanedText = pastedText.replace(/("label":\s?"[^"]*")|\s/g, (_, label) => {
94+
if (label) {
95+
return label.replace(/\n/g, ' ') // Convert newline characters to spaces within labels
96+
} else {
97+
return ''; // Remove whitespace characters otherwise
98+
}
99+
})
100+
txtTextarea.value = cleanedText
101+
});
102+
});
103+
104+
function onCancelOverlay() {
91105
if (window.scanner) {
92106
document.getElementById("popup").style.display = 'none';
93107
window.scanner.stop();
94108
}
95-
}
109+
}
96110
97111
// Deletes the global scanner property again if there is a reload
98112
window.addEventListener('unload', () => {

0 commit comments

Comments
 (0)