-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
90 lines (80 loc) · 3.31 KB
/
popup.js
File metadata and controls
90 lines (80 loc) · 3.31 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
function getLocalizedMessage(messageName) {
return browser.i18n.getMessage(messageName);
}
function getCurrentTabUrl() {
browser.tabs
.query({ active: true, currentWindow: true })
.then((tabs) => {
if (tabs.length > 0) {
const currentTab = tabs[0];
document.getElementById("longUrlInput").value = currentTab.url;
}
})
.catch((error) => {
console.error("Erreur lors de la récupération de l'onglet :", error);
});
}
function copyUrl() {
var input = document.getElementById("shtUrl");
input.select();
input.setSelectionRange(0, 99999);
document.execCommand("copy");
document.getElementById("tooltipTxt").style.visibility = 'visible';
setTimeout(function() {
document.getElementById("tooltipTxt").style.visibility = 'hidden';
}, 2000);
}
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("shtUrl").addEventListener("click", copyUrl);
document.getElementById("copyBtn").addEventListener("click", copyUrl);
getCurrentTabUrl();
document.getElementById("shortenBtn").innerHTML = '<i class="fa-solid fa-down-left-and-up-right-to-center"></i>' + getLocalizedMessage("shortenButtonLabel");
document.getElementById("longUrlInput").placeholder = getLocalizedMessage("LabelLongUrl");
document.getElementById("emailInput").placeholder = getLocalizedMessage("LabelEmail");
document.getElementById("shortenBtn").addEventListener("click", function () {
const apiUrl = "https://joliurl.com/api/url"; // JoliURL API
const form = document.getElementById("formJoliUrl");
const formData = new FormData(form);
fetch(apiUrl, {
method: "POST",
body: formData,
})
.then((response) => {
if (response.ok) {
return response.json();
} else if (response.status === 400) {
return response.json();
} else {
throw new Error("Network response was not ok");
}
})
.then((data) => {
if (typeof data.error !== "undefined") {
document.getElementById("respKo").style.display = "block";
document.getElementById("respOk").style.display = "none";
switch (data.code) {
case 103 :
document.getElementById("respKo").innerHTML = getLocalizedMessage("errorUrlMessage");
break;
case 104:
document.getElementById("respKo").innerHTML = getLocalizedMessage("errorEmailMessage");
break;
default:
document.getElementById("respKo").innerHTML = getLocalizedMessage("defaultErrorMessage");
}
} else {
document.getElementById("respOk").style.display = "block";
document.getElementById("respKo").style.display = "none";
document.getElementById("shtUrl").value = data.short_url;
document.getElementById("shortenBtn").style.display = "none";
document.getElementById("sMessage").innerHTML = getLocalizedMessage("defaultSuccessMessage");
document.getElementById("tooltipTxt").innerText = getLocalizedMessage("copiedMessage");
}
})
.catch((error) => { // default error message
console.error("Error:", error);
document.getElementById("respKo").innerHTML = getLocalizedMessage("defaultErrorMessage");
});
return false;
});
});