-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (143 loc) · 6.25 KB
/
Copy pathscript.js
File metadata and controls
171 lines (143 loc) · 6.25 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
// Reddox Core Logic
document.addEventListener('DOMContentLoaded', () => {
const usernameInput = document.getElementById('redditUsername');
const subredditInput = document.getElementById('subredditInput');
const searchBtn = document.getElementById('searchBtn');
const copyBtn = document.getElementById('copyBtn');
const optionChips = document.querySelectorAll('.search-options:not(.time-filters) .option-chip');
const timeChips = document.querySelectorAll('.time-filters .option-chip');
const resultArea = document.getElementById('result-area');
let currentMode = 'all';
let currentTime = 'any';
// Toggle time modes
timeChips.forEach(chip => {
chip.addEventListener('click', () => {
timeChips.forEach(c => c.classList.remove('active'));
chip.classList.add('active');
currentTime = chip.dataset.time;
});
});
// Toggle search modes
optionChips.forEach(chip => {
chip.addEventListener('click', () => {
optionChips.forEach(c => c.classList.remove('active'));
chip.classList.add('active');
currentMode = chip.id.replace('opt-', '');
});
});
const getFinalQuery = (username) => {
// Clean username (remove u/ if present)
const cleanUser = username.trim().replace(/^u\//, '');
if (!cleanUser) return null;
// Clean subreddit
let sub = subredditInput.value.trim().replace(/^r\//, '');
let siteModifier = sub ? `site:reddit.com/r/${sub}` : `site:reddit.com`;
if (!sub && (currentMode === 'posts' || currentMode === 'comments')) {
siteModifier = `site:reddit.com/r/*`;
}
let query = '';
switch(currentMode) {
case 'posts':
query = `${siteModifier} "submitted by ${cleanUser}"`;
break;
case 'comments':
query = `${siteModifier} "${cleanUser}"`;
break;
default:
query = `${siteModifier} "${cleanUser}"`;
}
return query;
};
const loadHistory = () => {
const history = JSON.parse(localStorage.getItem('reddoxHistory') || '[]');
const historyContainer = document.getElementById('history-list');
if (!historyContainer) return;
historyContainer.innerHTML = history.map(user => `
<span class="history-item" onclick="document.getElementById('redditUsername').value='${user}'; document.getElementById('searchBtn').click();">${user}</span>
`).join('');
};
const addToHistory = (user) => {
let history = JSON.parse(localStorage.getItem('reddoxHistory') || '[]');
history = [user, ...history.filter(h => h !== user)].slice(0, 5);
localStorage.setItem('reddoxHistory', JSON.stringify(history));
loadHistory();
};
const performSearch = (redirectCurrentTab = false) => {
const username = usernameInput.value;
const query = getFinalQuery(username);
if (!query) {
showFeedback('Please enter a username', 'error');
return;
}
addToHistory(username.trim().replace(/^u\//, ''));
let url = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
if (currentTime !== 'any') {
url += `&tbs=${currentTime}`;
}
if (redirectCurrentTab === true) {
window.location.href = url;
} else {
window.open(url, '_blank');
}
};
const copyToClipboard = () => {
const username = usernameInput.value;
const query = getFinalQuery(username);
if (!query) {
showFeedback('Enter a username first', 'error');
return;
}
// Build a shareable Reddox deep-link using the current base path
// so it works on both GitHub Pages (/redDoz/) and custom domains (/).
const basePath = window.location.pathname.replace(/[^/]*$/, '');
const cleanUser = encodeURIComponent(username.trim().replace(/^u\//, ''));
const shareableUrl = `${window.location.origin}${basePath}${cleanUser}`;
navigator.clipboard.writeText(shareableUrl).then(() => {
showFeedback('Reddox link copied to clipboard!', 'success');
}).catch(() => {
// Fallback for older browsers
showFeedback('Failed to copy link', 'error');
});
};
const showFeedback = (message, type) => {
resultArea.innerHTML = `<p style="color: ${type === 'error' ? '#ff4b2b' : '#4bb543'}; font-size: 0.9rem;">${message}</p>`;
resultArea.classList.add('visible');
setTimeout(() => {
resultArea.classList.remove('visible');
}, 3000);
};
// Event Listeners
searchBtn.addEventListener('click', performSearch);
copyBtn.addEventListener('click', copyToClipboard);
usernameInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') performSearch();
});
// Initial load
loadHistory();
// Auto-search logic from URL (e.g. reddox.in/0xCynic or prajwal-56.github.io/redDoz/0xCynic)
// Derive the base path (the directory serving this page) so the repo name on
// GitHub Pages (e.g. /redDoz/) is never mistaken for a username.
const basePath = window.location.pathname.replace(/[^/]*$/, ''); // e.g. '/redDoz/' or '/'
const relativePath = window.location.pathname
.slice(basePath.length) // strip the base directory prefix
.replace(/^\/+|\/+$/g, ''); // strip any remaining leading/trailing slashes
const searchParams = new URLSearchParams(window.location.search);
const hash = window.location.hash.replace(/^#/, '');
let autoSearchUser = '';
// Priority: Pathname > Query Param > Hash
// Ignore common paths like index.html
if (relativePath && !relativePath.includes('.html')) {
autoSearchUser = relativePath;
} else if (searchParams.has('u')) {
autoSearchUser = searchParams.get('u');
} else if (hash) {
autoSearchUser = hash;
}
if (autoSearchUser) {
usernameInput.value = decodeURIComponent(autoSearchUser);
// Automatically perform search
setTimeout(() => {
performSearch(true);
}, 100);
}
});