-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
140 lines (130 loc) · 4.62 KB
/
popup.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
document.addEventListener('DOMContentLoaded', function() {
reloadUI();
});
function reloadUI() {
browser.runtime.sendMessage({ command: "showLink" })
.then(response => {
if (!response.data) {
clearUI();
} else {
buildAppLinkUI(response.data)
}
})
.catch(error => console.error("Error:", error));
}
function buildAppLinkUI(appLink) {
const container = document.getElementById("content");
container.innerHTML = "";
title = document.createElement("h3")
title.textContent = "Latest:";
container.appendChild(title);
var a;
if (!appLink.url.includes("://")) { // Sometimes for the QR code we don't intercept links, still want to display it though
a = document.createElement("u");
} else {
a = document.createElement("a");
a.href = appLink.url;
}
if (appLink.url.length > 233) {
a.textContent = appLink.url.substring(0, 230) + "...";
} else {
a.textContent = appLink.url;
}
container.appendChild(a);
container.appendChild(document.createElement("br"));
container.appendChild(document.createElement("br"));
origin = document.createElement("b");
origin.textContent = "Origin: ";
container.appendChild(origin);
originLink = document.createElement("a");
originLink.textContent = appLink.origin;
originLink.href = appLink.origin;
container.appendChild(originLink);
container.appendChild(document.createElement("br"));
container.appendChild(document.createElement("br"));
visited = document.createElement("b");
visited.textContent = "Visited at: ";
container.appendChild(visited);
visitedDate = document.createElement("span");
visitedDate.textContent = appLink.visitedAt;
container.appendChild(visitedDate);
browser.storage.local.get("history").then(
(data) => {
console.log(data);
if (!data || !data.history) {
count = 0;
} else {
count = JSON.parse(data.history).length;
}
setCount(count);
},
(err) => {
console.log(`Error: ${err}`);
setCount(0)
}
)
}
function setCount(c) {
const count = document.getElementById("count");
count.textContent = `${c}`;
}
function clearUI() {
const container = document.getElementById("content");
container.innerHTML = "<u>Empty</u>";
setCount(0);
}
function setMessage(message) {
m = document.getElementById("message");
m.textContent = message;
setTimeout(() => message.textContent = "", 2000);
}
document.getElementById('export').addEventListener('click', function() {
browser.storage.local.get("history").then(
(data) => {
if (!data || !data.history || data.history.length === 0) {
setMessage("No history to download");
return;
}
var blob = new Blob([data.history], {type: "application/json"});
var url = URL.createObjectURL(blob);
var link = document.createElement("a");
link.href = url;
link.download = "appLinksHistory.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
setMessage("History downloaded!");
},
(error) => {
setMessage("No history to download");
}
);
});
document.getElementById('clear').addEventListener('click', async function() {
setMessage("Cleared all data");
clearUI();
await browser.runtime.sendMessage({ command: "clearData" });
});
document.getElementById("qr").addEventListener("click", function () {
chrome.tabs.captureVisibleTab(null, {}, function (imageString) {
var canvasElement = document.getElementById("screenshot");
var canvas = canvasElement.getContext("2d");
var img = new Image();
img.src = imageString;
img.onload = function() {
canvasElement.width = img.width;
canvasElement.height = img.height;
canvas.drawImage(img, 0, 0);
var imageData = canvas.getImageData(0, 0, canvasElement.width, canvasElement.height);
var code = jsQR(imageData.data, imageData.width, imageData.height, {
inversionAttempts: "dontInvert",
});
if (code) {
console.log(code.data);
browser.runtime.sendMessage({ command: "handleQrLink", data: code.data }).then((r) => reloadUI());
} else {
setMessage("Could not find QR code in page")
}
};
});
})