Mirrored from GitHub

git clone https://github.com/christc4/js1.git

Jump to: date-time random-page-from-json1 random-page-from-json2 searchbar-from-json


date-time

1function startTime() {
2  const today = new Date();
3	let h = today.getHours();
4	let m = today.getMinutes();
5	let s = today.getSeconds();
6	m = checkTime(m);
7	s = checkTime(s);
8	document.getElementById('txt').innerHTML =  h + ":" + m + ":" + s;
9	setTimeout(startTime, 1000);
10}
11
12function checkTime(i) {
13  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
14	return i;
15}

random-page-from-json1

1async function fetchPaths() {
2    const response = await fetch('s.json');
3    const paths = await response.json();
4    return paths;
5}
6
7async function goToRandomPage() {
8    const paths = await fetchPaths();
9    const randomPath = paths[Math.floor(Math.random() * paths.length)];
10    window.location.href = randomPath;
11}

random-page-from-json2

1async function fetchPaths() {
2    const response = await fetch('http://avsbq.org/s.json');
3    const paths = await response.json();
4    return paths;
5}
6
7async function goToRandomPage() {
8    const paths = await fetchPaths();
9    
10    // Filter out paths containing a dot
11    const filteredPaths = paths.filter(path => !path.includes('.'));
12    
13    // Select a random path from the filtered paths
14    const randomPath = filteredPaths[Math.floor(Math.random() * filteredPaths.length)];
15    
16    // Redirect to the new URL
17    window.location.assign(`http://avsbq.org/${randomPath}`);
18}

searchbar-from-json

1// Function to fetch the site index from an external file
2async function fetchSiteIndex() {
3    const response = await fetch('https://avsbq.org/s.json'); 
4    // Replace with the correct path to your JSON file
5    const data = await response.json();
6    return data;
7}
8
9// Function to filter search results
10function filterResults(query, siteIndex) {
11    return siteIndex.filter(item => item.toLowerCase().includes(query.toLowerCase()));
12}
13
14// Function to display search results
15function displayResults(results) {
16    const searchResults = document.getElementById('searchResults');
17    searchResults.innerHTML = '';
18
19    // Slice the results to show a maximum of 5 items
20    results.slice(0, 4).forEach(result => {
21	const resultItem = document.createElement('div');
22	resultItem.className = 'resultItem';
23	resultItem.innerHTML = `<a href="/${result}">${result}</a>`;
24	searchResults.appendChild(resultItem);
25    });
26}
27
28// Event listener for search input
29document.getElementById('searchBar').addEventListener('input', async function() {
30    const query = this.value;
31    const searchResults = document.getElementById('searchResults');
32    
33    // Clear search results if input is empty
34    if (query === '') {
35	searchResults.innerHTML = '';
36	return;
37    }
38
39    const siteIndex = await fetchSiteIndex();
40    const results = filterResults(query, siteIndex);
41    displayResults(results);
42});