Skip to content

Commit 390d858

Browse files
committed
Udate for links
1 parent ebcfd43 commit 390d858

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/layouts/Layout.astro

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,60 @@ if ("serviceWorker" in navigator) {
116116

117117
document.addEventListener('DOMContentLoaded', () => {
118118
if (window.matchMedia('(display-mode: standalone)').matches) {
119-
document.addEventListener('click', function (e:any) {
120-
const anchor = e.target.closest('a');
119+
document.addEventListener('click', function (e: Event) {
120+
const target = e.target as HTMLElement;
121+
const anchor = target.closest('a') as HTMLAnchorElement;
122+
121123
if (!anchor || !anchor.href) return;
122124

123125
const origin = window.location.origin;
124126
const href = anchor.getAttribute('href');
125127

128+
if (!href) return;
129+
130+
const normalizedHref = href.trim().toLowerCase();
131+
126132
if (
127-
href.startsWith('#') ||
128-
href.startsWith('/') ||
129-
href.startsWith(origin)
133+
normalizedHref.startsWith('#') ||
134+
normalizedHref.startsWith('/') ||
135+
normalizedHref.startsWith(origin.toLowerCase()) ||
136+
normalizedHref.startsWith('mailto:') ||
137+
normalizedHref.startsWith('tel:') ||
138+
normalizedHref.startsWith('sms:') ||
139+
(!normalizedHref.includes('://') && !normalizedHref.startsWith('//'))
130140
) {
131141
return;
132142
}
133143

134144
e.preventDefault();
135-
alert('External links are available only via website.');
145+
e.stopPropagation();
146+
147+
const message = 'External links are disabled in standalone mode. Please visit the website to access external content.';
148+
149+
if (typeof window !== 'undefined' && 'Notification' in window && Notification.permission === 'granted') {
150+
new Notification('External Link Blocked', { body: message });
151+
} else {
152+
alert(message);
153+
}
154+
155+
console.log('Blocked external link:', href);
156+
136157
}, { passive: false });
137158
}
138159
});
160+
161+
function addVisualFeedback() {
162+
if (window.matchMedia('(display-mode: standalone)').matches) {
163+
const style = document.createElement('style');
164+
style.textContent = `
165+
a[href^="http"]:not([href^="${window.location.origin}"]):hover::after,
166+
a[href^="//"]:hover::after {
167+
content: " 🚫";
168+
}
169+
`;
170+
document.head.appendChild(style);
171+
}
172+
}
173+
174+
document.addEventListener('DOMContentLoaded', addVisualFeedback);
139175
</script>

0 commit comments

Comments
 (0)