Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (89 loc) · 2.75 KB
/
Copy pathscript.js
File metadata and controls
110 lines (89 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let shouldStoreData = true;
async function storeData(qrData) {
shouldStoreData = false;
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"values": qrData
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://jqrscanner-production.up.railway.app/addData", requestOptions)
.then(response => response.text())
.then(result => {
console.log(result);
alert("saved");
shouldStoreData = true;
})
.catch(error => console.log('error', error));
}
function displayInfo(label, value) {
let resultList = document.getElementById('resultList');
// Create a new span element
let resultItem = document.createElement('span');
// Set the content of the span
resultItem.innerHTML = `<p><strong>${label}:</strong> ${value}</p>`;
// Add the new span at the beginning of the list
resultList.insertBefore(resultItem, resultList.firstChild);
}
async function onScanSuccess(qrCodeMessage) {
const vcardSplit = qrCodeMessage.split("\n");
let firstName = "";
let lastName = "";
let company = "";
let email = "";
for (let index in vcardSplit) {
let partInfo = vcardSplit[index].trim();
const infoSplit = partInfo.split(":");
if (infoSplit.length >= 2) {
const key = infoSplit[0].trim();
const value = infoSplit.slice(1).join(":").trim();
console.log("Key:", key);
console.log("Value:", value);
if (key === "N;CHARSET=UTF-8") {
console.log("Processing Name:", value);
const nameSplit = value.split(";");
console.log("Name Split:", nameSplit);
if (nameSplit.length >= 2) {
firstName = nameSplit[1];
lastName = nameSplit[0];
} else {
lastName = nameSplit[0];
}
} else if (key === "ORG") {
company = value;
} else if (key === "EMAIL") {
email = value;
} else if (key.startsWith("ORG")) {
company = value;
}
}
}
// Display relevant information
displayInfo("First Name", firstName);
displayInfo("Last Name", lastName);
displayInfo("Company", company);
displayInfo("Email", email);
// Save the data to a Google Sheet
if (shouldStoreData) {
await storeData([firstName + " " + lastName, company, email]);
}
// Log information to the console for testing
console.log("First Name:", firstName);
console.log("Last Name:", lastName);
console.log("Company:", company);
console.log("Email:", email);
}
function onScanError(errorMessage) {
// handle scan error
}
var html5QrcodeScanner = new Html5QrcodeScanner(
"reader", {
fps: 10,
qrbox: 250
});
html5QrcodeScanner.render(onScanSuccess, onScanError);