-
Notifications
You must be signed in to change notification settings - Fork 27
/
qrcode.js
93 lines (86 loc) · 2.57 KB
/
qrcode.js
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
import "../css/popup.css";
import QRCode from "davidshimjs-qrcodejs";
import $ from "jquery";
import { Web3Storage } from "web3.storage";
import "regenerator-runtime/runtime";
// QR code js library
var QR_CODE = new QRCode("qrcode", {
width: 150,
height: 150,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.L,
});
// web3.storage API token
const token = process.env.API_TOKEN;
const client = new Web3Storage({ token });
function hideLoader(callback) {
$(".loader").hide(function () {
$("section.flex-center > svg").css("aria-disabled", false);
if (callback && typeof callback === "function") callback();
});
}
function showLoader() {
$(".loader").show(900);
$("section.flex-center > svg").css("aria-disabled", true);
$("#qrcode img").css("display", "none");
$(".output-label + div").text("");
}
function uploadCallback(cid, ipfsLink) {
// IPFS hash (CID)
document.getElementById("cid").textContent = cid;
// IPFS gateway link
document.getElementById("link").textContent = ipfsLink;
$("#svg-link")
.off()
.on("click", function () {
window.open(ipfsLink, "_blank").focus();
});
// Copy CID to clipboard when copy button is clicked
$("#svg-cid")
.off()
.on("click", function () {
navigator.clipboard.writeText(cid);
});
// Generate QR code
QR_CODE.makeCode(ipfsLink);
// Code to download qrcode
$("#svg-download")
.off()
.on("click", function () {
// gets the base64 source of the qr code image
var qrCodeSrc = document.querySelector("#qrcode img").src;
var a = document.createElement("a");
// an invisible a tag is given that href.
a.href = qrCodeSrc;
// filename for the qrcode is set
a.download = "dscan_QR.png";
document.body.appendChild(a);
// the a tag is clicked, triggering the download
a.click();
document.body.removeChild(a);
});
}
// Generate decentralized QR code from file
$("#fileUpload").on("change", async function () {
showLoader();
var files = fileUpload.files;
var name = files[0].name;
var cid = await client.put(files);
let ipfsLink = `https://ipfs.infura.io/ipfs/${cid}/${name}/`;
hideLoader(function () {
uploadCallback(cid, ipfsLink);
});
});
QR_CODE.clear();
// Generate decentralized QR code from folder
$("#folderUpload").on("change", async function () {
showLoader();
var files = folderUpload.files;
var cid = await client.put(files);
let ipfsLink = `https://ipfs.infura.io/ipfs/${cid}/`;
hideLoader(function () {
uploadCallback(cid, ipfsLink);
});
});
QR_CODE.clear();