Skip to content

Instantly share code, notes, and snippets.

@helloint
Last active July 10, 2024 11:52
Show Gist options
  • Save helloint/725c30bcd5c541b86ac1f325c52f2148 to your computer and use it in GitHub Desktop.
Save helloint/725c30bcd5c541b86ac1f325c52f2148 to your computer and use it in GitHub Desktop.
Download PDF file from smartedu
// ==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);
@badkill23
Copy link

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