-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexternal-links.js
More file actions
100 lines (83 loc) · 3.52 KB
/
Copy pathexternal-links.js
File metadata and controls
100 lines (83 loc) · 3.52 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
// This script adds ?ref=ssp.sh to all external links on your website
// Wait for window load to ensure all other scripts have had a chance to run
window.addEventListener('load', function() {
// Get your website domain
const siteDomain = window.location.hostname;
const ownDomain = 'ssp.sh';
// Find all links on the page
const links = document.querySelectorAll('a');
// Function to check if a hostname belongs to our domain
// This checks only the domain part (hostname), not the path
// For example, this will match ssp.sh, brain.ssp.sh, vault.ssp.sh, etc.
// regardless of what path follows (like ssp.sh/brain/article)
function isOwnDomain(hostname) {
return hostname === ownDomain ||
hostname.endsWith('.' + ownDomain) ||
hostname === 'localhost' ||
hostname.startsWith('localhost:');
}
// Process each link
links.forEach(function(link) {
// Check if the link has an href attribute
if (link.href) {
try {
// Get the URL of the link
const linkUrl = new URL(link.href);
// Only process http(s) URLs — skip javascript:, mailto:, tel:, etc.
if (linkUrl.protocol !== 'http:' && linkUrl.protocol !== 'https:') {
return;
}
// Check if the link is external (different hostname) and not to our own domains
if (linkUrl.hostname !== siteDomain && !isOwnDomain(linkUrl.hostname)) {
// Only modify if the URL doesn't already have the ref parameter
if (!linkUrl.searchParams.has('ref')) {
// Store the hash part (including text fragments)
const hashPart = linkUrl.hash;
// Remove the hash part temporarily
linkUrl.hash = '';
// Add the ref parameter
linkUrl.searchParams.set('ref', 'ssp.sh');
// Re-add the hash part
const urlWithRef = linkUrl.toString() + hashPart;
// Update the link's href attribute
link.href = urlWithRef;
}
}
} catch (e) {
// Ignore invalid URLs
console.warn('Invalid URL found:', link.href);
}
}
});
// Add click event listener for dynamically added links
document.body.addEventListener('click', function(event) {
// Check if the clicked element is an anchor tag or contains one
const link = event.target.closest('a');
if (link && link.href) {
try {
const linkUrl = new URL(link.href);
// Only process http(s) URLs — skip javascript:, mailto:, tel:, etc.
if (linkUrl.protocol !== 'http:' && linkUrl.protocol !== 'https:') {
return;
}
// Only process external links that aren't to our own domains
if (linkUrl.hostname !== siteDomain && !isOwnDomain(linkUrl.hostname) && !linkUrl.searchParams.has('ref')) {
// Prevent the default action
event.preventDefault();
// Store the hash part (including text fragments)
const hashPart = linkUrl.hash;
// Remove the hash part temporarily
linkUrl.hash = '';
// Add the ref parameter
linkUrl.searchParams.set('ref', 'ssp.sh');
// Re-add the hash part
const urlWithRef = linkUrl.toString() + hashPart;
// Navigate to the modified URL
window.open(urlWithRef, link.target || '_self');
}
} catch (e) {
// Let the browser handle invalid URLs
}
}
}, false);
});