-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
143 lines (116 loc) · 4.06 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
141
142
143
document.addEventListener("DOMContentLoaded", function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const inputUrl = document.getElementById("inputUrl");
if (tabs[0] && tabs[0].url) {
inputUrl.value = tabs[0].url;
}
generateCitation();
});
document
.getElementById("inputUrl")
.addEventListener("input", generateCitation);
const inputs = document.querySelectorAll(
"#authorInput, #titleInput, #dateInput, #publisherInput"
);
for (const input of inputs) {
input.addEventListener("input", updateCitation);
}
});
function getTitleFromAnchoredElement(doc, anchor) {
const anchoredElement = doc.getElementById(anchor);
if (anchoredElement) {
return anchoredElement.textContent.replace("\n", " ");
}
return null;
}
function generateCitation() {
const inputUrl = document.getElementById("inputUrl").value;
if (!inputUrl) {
return;
}
const url = new URL(inputUrl);
const accessDate = formatAccessDate(new Date());
fetch(url)
.then((response) => response.text())
.then((html) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const author = doc.querySelector("meta[name='author']")
? doc.querySelector("meta[name='author']").content
: null;
let title;
if (url.hash) {
title = getTitleFromAnchoredElement(doc, url.hash.slice(1));
}
if (!title) {
title = doc.querySelector("meta[property='og:title']")
? doc.querySelector("meta[property='og:title']").content
: doc.querySelector("title")
? doc.querySelector("title").textContent
: "Unknown Title";
}
const domain = url.hostname.replace(/^www\./, "");
const publisher = doc.querySelector("meta[property='og:site_name']")
? doc.querySelector("meta[property='og:site_name']").content
: doc.querySelector("meta[name='publisher']")
? doc.querySelector("meta[name='publisher']").content
: domain;
const citation = `[${
!!author ? `${author} ` : ""
}"${title}" (${publisher}).](${url}) Retrieved ${accessDate}.`;
document.getElementById("authorInput").value = author;
document.getElementById("titleInput").value = title;
document.getElementById("dateInput").value = accessDate;
document.getElementById("publisherInput").value = publisher;
document.getElementById("citation").innerText = citation;
document.getElementById("copy").disabled = false;
})
.catch((error) => {
console.error("Error fetching the webpage:", error);
});
}
async function copyCitation() {
const citationElement = document.getElementById("citation");
const citation = citationElement.innerText;
const copyMessage = document.getElementById("copyMessage");
try {
await navigator.clipboard.writeText(citation);
copyMessage.textContent = "Citation copied to clipboard!";
copyMessage.classList.remove("hidden");
setTimeout(() => {
copyMessage.classList.add("hidden");
}, 3000); // Hide the message after 3 seconds
} catch (err) {
console.error("Failed to copy text: ", err);
}
}
function updateCitation() {
const author = document.getElementById("authorInput").value;
const title = document.getElementById("titleInput").value;
const inputUrl = document.getElementById("inputUrl").value;
const url = new URL(inputUrl);
const accessDate = formatAccessDate(new Date());
const citation = `[${author} "${title}"].](${url}) Retrieved ${accessDate}.`;
document.getElementById("citation").innerText = citation;
}
function formatAccessDate(date) {
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
return `${monthNames[monthIndex]} ${day}, ${year}`;
}
document.getElementById("copy").addEventListener("click", copyCitation);