-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (160 loc) · 5.6 KB
/
Copy pathscript.js
File metadata and controls
191 lines (160 loc) · 5.6 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const cardContainer = document.getElementById('cardContainer');
async function checkOnlineStatus(url) {
try {
const response = await fetch(url, { method: 'GET', mode: 'no-cors' });
return true;
} catch (error) {
return false;
}
}
async function createCard(item) {
const card = document.createElement('div');
card.classList.add('col-md-6', 'col-lg-4', 'card-container');
const badgeElement = document.createElement('span');
badgeElement.classList.add('badge', 'badge-unknown', 'badge-secondary', 'mt-2');
badgeElement.textContent = 'Checking';
const cardContent = `
<div class="card link-card" data-url="${item.link}">
<div class="card-body d-flex">
<div class="logo-container">
<img src="${item.icon}" alt="${item.title}" class="logo-img">
</div>
<div class="title-content">
<h5 class="card-title">${item.title}</h5>
<p class="card-description">${item.description}</p>
</div>
<div class="buttons-container">
${createButtons(item)}
</div>
</div>
</div>
`;
card.innerHTML = cardContent;
const imgElement = card.querySelector('.logo-img');
imgElement.fallback = 0; // Initialize before setting src
imgElement.onerror = async function () {
const nextUrl = getNextBackupIconUrl(item, imgElement.fallback);
console.log(`Trying backup URL: ${nextUrl}`);
if (nextUrl) {
imgElement.src = nextUrl;
imgElement.fallback++;
} else {
imgElement.src = blank;
imgElement.onerror = null; // Prevent loop
}
};
card.querySelector('.title-content').appendChild(badgeElement);
card.addEventListener('click', handleCardClick);
card.addEventListener('auxclick', handleCardAuxClick);
const buttonsContainer = card.querySelector('.buttons-container');
if (buttonsContainer) {
buttonsContainer.addEventListener('click', handleButtonClicked);
}
cardContainer.appendChild(card);
updateBadgeStatus(card, item.link);
}
function createButtons(item) {
if (!item.buttons) {
return '';
}
return item.buttons.map(button => `
<button class="btn btn-primary btn-sm" data-url="${button.url}">
${button.label}
</button>
`).join('');
}
function handleCardClick(event) {
const clickedElement = event.target;
if (clickedElement.tagName !== 'BUTTON') {
const card = clickedElement.closest('.card');
if (card) {
const cardLink = card.getAttribute('data-url');
if (cardLink) {
window.location.href = cardLink;
}
}
}
}
function handleCardAuxClick(event) {
const clickedElement = event.target;
if (clickedElement.tagName !== 'BUTTON') {
const card = clickedElement.closest('.card');
if (card) {
const cardLink = card.getAttribute('data-url');
if (cardLink) {
if (event.button === 0) {
window.location = cardLink.href;
} else if (event.button === 1) {
window.open(cardLink, '_blank');
}
}
}
}
}
function handleButtonClicked(event) {
const button = event.target;
const buttonUrl = button.getAttribute('data-url');
if (buttonUrl) {
fetch(buttonUrl, { method: 'GET', mode: 'no-cors' })
.then(response => {
// Handle the response as needed
})
.catch(error => {
console.error('Error fetching URL:', error);
});
}
}
async function updateBadgeStatus(card, link) {
const badgeElement = card.querySelector('.badge-unknown');
try {
const isOnline = await checkOnlineStatus(link);
const badgeClass = isOnline ? 'badge-success' : 'badge-danger';
badgeElement.textContent = isOnline ? 'Online' : 'Offline';
badgeElement.classList.remove('badge-unknown');
badgeElement.classList.add(badgeClass);
} catch (error) {
console.error(error);
badgeElement.textContent = 'Unknown';
badgeElement.classList.remove('badge-unknown');
badgeElement.classList.add('badge-danger');
}
}
function setStyle(dark) {
const customTheme = document.getElementById('custom-theme');
customTheme.href = dark ? 'styles-dark.css' : 'styles-light.css';
}
function setupStyle() {
// TODO: Read preference, store in cookie?
// Yeah, i dont want to deal with this annoying cookie law.
const darkmode = true; // Default preference
setStyle(darkmode);
const styleSwitch = document.getElementById('styleSwitch');
styleSwitch.checked = darkmode;
styleSwitch.addEventListener('change', function () {
setStyle(styleSwitch.checked);
});
}
function getRoot(url) {
try {
const { protocol, host } = new URL(url);
return `${protocol}//${host}`;
} catch (error) {
console.error(`Invalid URL: ${url}`, error);
return '';
}
}
function getNextBackupIconUrl(item, index) {
const simpleName = (item.name || item.title || '').toLowerCase().replace(/\s+/g, '');
const root = getRoot(item.link);
const urls = [
`https://cdn.simpleicons.org/${simpleName}`,
`${root}/favicon.ico`
];
if (index >= urls.length) {
return null; // All URLs failed
}
return urls[index];
}
// Process each item
jsonData.forEach(item => createCard(item));
setupStyle();