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);
@helloint
Copy link
Author

helloint commented Jul 9, 2024

like CURP_LOAN030925MPLPNYA8.pdf?

@badkill23
Copy link

Yes, something like that, or maybe taking the first name. Something like this:
NAME_LOAN030925MPLPNYA8.pdf
Because I always download it as CURP.pdf

@helloint
Copy link
Author

helloint commented Jul 9, 2024

a bit difficult to get the name. CURP_LOAN030925MPLPNYA8.pdf is easier

@badkill23
Copy link

I understand, with that structure of the name of the pdf it would look good

@helloint
Copy link
Author

helloint commented Jul 9, 2024

try this

// ==UserScript==
// @name         Download named curp PDF
// @namespace    https://helloint.com
// @version      0.2
// @description  Download curp PDF file with specific filename
// @author       Wayne Mao
// @match        https://www.gob.mx/curp/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=web.app
// @grant        none
// ==/UserScript==

function execModule() {
    window.define('tramite-curp/services/curp-document', ['exports', 'tramite-curp/mixins/util', 'tramite-curp/config/environment'], function(e, t, a) {
        Object.defineProperty(e, '__esModule', { value: !0 }), e.default = Ember.Service.extend(t.default, {
            getDocument: function(e, t) {
                var n = this;
                Ember.$.ajax({
                    url: '' + a.default.APP.CURP_HOST + e, type: 'GET', mimeType: 'text/plain; charset=x-user-defined', success: function(e) {
                        var a = 'data:application/pdf;base64,' + e, r = e, o = document.getElementById('dwnldLnk'),
                            i = Ember.$('.wizard-steps').find('.wizardbar-i').find('.glyphicon');
                        var s = function() {
                            var e = window.navigator.userAgent;
                            if (e.indexOf('MSIE ') > 0) return !0;
                            if (e.indexOf('Trident/') > 0) return e.indexOf('rv:'), !0;
                            return e.indexOf('Edge/'), !1;
                        }();
                        n.downloader(a, 'CURP_' + t + '.pdf');
                        Ember.$(i).parent().addClass('success'), Ember.$('.loader').addClass('hidden'), Ember.$('#download').attr('disabled', !0), surveyTramiteGobMX.getEncuestaHC('CRP666');
                    },
                });
            },
        });
    });
    console.log('module executed.');
}

execModule();

@badkill23
Copy link

badkill23 commented Jul 9, 2024

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?

<div class="col-md-12 top-buffer clearfix">
                  <hr>
                </div>

@helloint
Copy link
Author

your request become quite customized...

@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