-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (73 loc) · 2.43 KB
/
app.js
File metadata and controls
86 lines (73 loc) · 2.43 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
// Read values from URL
const params = new URLSearchParams(window.location.search);
// Object to hold the data for the preview
const data = {
song: params.get("song-link"),
from: params.get("from-name"),
to: params.get("to-name"),
note: params.get("your-note"),
};
// Helper function to set text content of an element by ID
function setText(id, value) {
const element = document.getElementById(id);
if (element) {
element.textContent = value ?? "";
}
}
// Set the Spotify player URL based on the provided song link
function setSpotifyPlayer(songUrl) {
const player = document.getElementById("spotify-player");
if (!player || !songUrl) return;
const embedUrl = songUrl.includes("open.spotify.com/")
? songUrl.replace("open.spotify.com/", "open.spotify.com/embed/")
: songUrl;
player.src = embedUrl;
}
// Initialize the preview page with the data from the URL
function init() {
if (!data.song || !data.from || !data.to || !data.note) {
alert("Looks like something’s missing. Fill in all fields to preview your note.");
}
setText("from", `- ${data.from}`);
setText("to", data.to);
setText("message", data.note ? `"${data.note}"` : "");
setSpotifyPlayer(data.song);
}
// Build the URL for the view page with the current data
function buildViewUrl() {
const viewParams = new URLSearchParams();
viewParams.set("song-link", data.song ?? "");
viewParams.set("from-name", data.from ?? "");
viewParams.set("to-name", data.to ?? "");
viewParams.set("your-note", data.note ?? "");
return `${window.location.origin}/view.html?${viewParams.toString()}`;
}
// Copy the view page URL to the clipboard
function copyLink() {
const shareButton = document.getElementById("share-button");
const viewPageUrl = buildViewUrl();
if (!navigator.clipboard?.writeText) {
alert("Clipboard not available. Please copy the URL manually.");
return;
}
navigator.clipboard
.writeText(viewPageUrl)
.then(() => {
if (shareButton) {
shareButton.innerHTML = '<i class="fa-solid fa-check"></i>Copied!';
setTimeout(() => {
shareButton.innerHTML =
'<i class="fa-regular fa-copy"></i>Copy Link';
}, 3000);
}
})
.catch((err) => {
console.error("Failed to copy: ", err);
alert("Failed to copy link. Please try again.");
});
}
// Redirect to the create page to edit the note
function editNote() {
window.location.href = "create.html";
}
init();