Last active
July 10, 2024 11:52
-
-
Save helloint/725c30bcd5c541b86ac1f325c52f2148 to your computer and use it in GitHub Desktop.
Download PDF file from smartedu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Download PDF from smartedu | |
// @namespace https://helloint.com | |
// @version 0.2 | |
// @description Download PDF file from basic.smartedu.cn by press keyboard 'ddd' | |
// @author Wayne Mao | |
// @match https://basic.smartedu.cn/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=web.app | |
// @grant none | |
// ==/UserScript== | |
const PASSWORD = 'ddd'; | |
const TIMEOUT = 5000; | |
let userInput = ''; | |
let iid = 0; | |
const startTimer = () => { | |
iid = setTimeout(() => { | |
userInput = ''; | |
}, TIMEOUT); | |
}; | |
const clearTimer = () => { | |
if (iid) { | |
clearTimeout(iid); | |
} | |
}; | |
const resetTimer = () => { | |
clearTimer(); | |
startTimer(); | |
} | |
function downloadPDF(url, fileName) { | |
console.log(`PDF url: ${url}, fileName: ${fileName}`); | |
fetch(url) | |
.then(response => response.blob()) | |
.then(blob => { | |
const url = window.URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.style.display = 'none'; | |
a.href = url; | |
a.download = fileName; | |
document.body.appendChild(a); | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
}) | |
.catch(() => alert('An error occurred while downloading the PDF.')); | |
} | |
function parsePdfInfo() { | |
const pdfTitle = document.querySelectorAll('h3')[0].innerText; | |
const urlParams = new URLSearchParams(location.search); | |
const pdfId = urlParams.get('contentId'); // c4857bdb-4166-4552-b65e-41b545d8f7b8 | |
const pdfUrl = `https://r3-ndr.ykt.cbern.com.cn/edu_product/esp/assets/${pdfId}.pkg/pdf.pdf`; | |
return { | |
pdfTitle, pdfUrl | |
}; | |
} | |
function doc_keyUp(e) { | |
resetTimer(); | |
switch (e.keyCode) { | |
case 68: | |
//d | |
userInput += 'd'; | |
break; | |
default: | |
userInput = ''; | |
break; | |
} | |
if (userInput === 'ddd') { | |
const {pdfUrl, pdfTitle} = parsePdfInfo(); | |
downloadPDF(pdfUrl, pdfTitle); | |
} | |
} | |
document.addEventListener('keyup', doc_keyUp, false); |
your request become quite customized...
I understand, and I apologize for the inconvenience. For your help, thank you very much, the code was perfect with what I was looking for.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perfect, works very well. I only have one problem, in the Safari browser it doesn't download anything, the download button doesn't work. Could you help me with that problem?
I also found an element that stands above the download button that acts as if it were deactivated, will it be possible to hide or eliminate it from the same code?