59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
const onhover = false // when to load link, false will load immediately
|
|
|
|
async function getDownload(href: string): Promise<string | null> {
|
|
let resp = await fetch(href)
|
|
let text = await resp.text()
|
|
let match = text.match(/<a href=['"](downloadx.*?)['"]/i)
|
|
if (match) {
|
|
return match[1]
|
|
}
|
|
return null
|
|
}
|
|
|
|
function attachFetchDownloadLink(a: HTMLLinkElement, depth = 0) {
|
|
function run() {
|
|
a.innerText = 'Loading'
|
|
let url = new URL(a.href)
|
|
if (url.pathname === '/adloadx') {
|
|
getDownload(a.href).then(downloadUrl => {
|
|
if (downloadUrl) {
|
|
a.href = downloadUrl
|
|
a.innerText = 'Download'
|
|
} else {
|
|
a.innerText = 'Failed'
|
|
if (onhover) attachFetchDownloadLink(a, ++depth)
|
|
else
|
|
setTimeout(
|
|
() => attachFetchDownloadLink(a, ++depth),
|
|
1000
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
if (depth > 5) return
|
|
|
|
if (onhover) {
|
|
a.addEventListener('mouseover', run, { once: true })
|
|
} else {
|
|
run()
|
|
}
|
|
}
|
|
|
|
addEventListener('load', () => {
|
|
document.querySelectorAll<HTMLLinkElement>('a').forEach(a => {
|
|
let href = new URL(a.href)
|
|
if (href.hostname === 'adfoc.us') {
|
|
let params = href.searchParams
|
|
if (params.has('url')) {
|
|
let url = params.get('url')?.replace('http://', 'https://')
|
|
if (url) {
|
|
a.href = url
|
|
attachFetchDownloadLink(a)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|