I saw a feature on thewpclub. net, I also want to implement it on my Wordpress blog. when you click on any download link, it will first redirect you to thewpclub. net/download and a 15 seconds countdown will happen there before the actual download link appear.
What I have tried:
I saw a code on help.gulshankumar. net/forum, I paste the code on my blog footer, but it ain't working. Please help me correct any error in the code and guide me on how to place and implement it on my blog.
See the code below.
The first script will select any links with class download-wait and change its link from “https://example.com/file.zip” to “https://yoursite.com/download?https%example.com%file.zip
const downloadLinks = document.querySelectorAll('.download-wait');
downloadLinks.forEach(link => {
const linkToFile = link.getAttribute('hrefs');
const linkToFileEdited = linkToFile
.replace('https://', 'https/')
.replace(new RegExp('%', 'g'), '/');
link.setAttribute('href', `${window.location.origin}/download/?${linkToFileEdited}`);
});
after the user is redirected to the download page the second script will keep the user wait for 15 sec then decode the link and redirect the user to the external link.
<p style="font-size:16px;">Please wait, starting download in <strong><span id="countdown">15</span> seconds</strong></p>
<pre lang="Javascript">const query = window.location.search;
const link = query.slice(1);
const newLink = link
.replace(new RegExp('%', 'g'), '/')
.replace('https/', 'https://');
// Total seconds to wait
var seconds = 15;
function countdown() {
seconds = seconds - 1;
if (seconds < 0) {
// Download link
window.location = newLink;
} else {
// Update remaining seconds
document.getElementById("countdown").innerHTML = seconds;
// Countdown wait time is 1 second
setTimeout("countdown()", 1000);
}
}
// Run countdown function
countdown();