Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name CRE GOB Autofirmar
- // @namespace https://ope.cre.gob.mx/OPElectronica/ObligacionFormularioElectronico/
- // @version 0.6
- // @description try to take over the world!
- // @author You
- // @match https://ope.cre.gob.mx/OPElectronica/ObligacionFormularioElectronico*
- // @grant window.open
- // @downloadURL http://pastebin.com/raw/bgjsiq4U
- // ==/UserScript==
- function autoFirmar(){
- (function checkIfAllFilledAndSend() {
- var contrasenaMain = window.opener.$('#contrasena').val();
- var certificadoMain = window.opener.$('#certificado')[0].files[0];
- var claveMain = window.opener.$('#clave')[0].files[0];
- $('#certificado')[0].files[0] = certificadoMain;
- $('#clave')[0].files[0] = claveMain;
- $('#contrasena').val(contrasenaMain);
- // your code
- console.log('Checking if has values');
- console.log('Certificado:' + !!$('#certificado')[0].files[0]);
- console.log('Clave:' + !!$('#clave')[0].files[0]);
- console.log('txtContenido' + !!$('#txtContenido').val());
- if($('#certificado')[0].files[0] && $('#clave')[0].files[0] && $('#contrasena').val() && $('#txtContenido').val()){
- $('#dlgFirma').uFirma().firmar($('#txtContenido').val(), $('#dlgFirma').attr('data-rfc'));
- //mods.triggerDownloadLink();
- return;
- } else{
- setTimeout( checkIfAllFilledAndSend, 1000 );
- }
- })();
- return;
- }
- function replaceiFrame(filename){
- var pdfURL;
- if($('#acuseIframe').attr('src')){
- pdfURL = $('#acuseIframe').attr('src') || '';
- }
- if($('#dlgFirma').attr('acuseiframe')){
- pdfURL = $('#dlgFirma').attr('acuseiframe') || '';
- }
- if (pdfURL == "undefined"){
- pdfURL = '';
- }
- var oldURL = $('#acuseIframe').attr('href') || '';
- if((pdfURL == oldURL) || !pdfURL){return;}
- $('#acuseIframe').replaceWith($('<a src = "' + pdfURL + '" href="' + pdfURL + '" download="' + filename + '" id="acuseIframe"></a>'));
- }
- function triggerDownloadLink(){
- var razonSocial = $('#txtRazonSocial')[0].value;
- var description = $('#formulario > div.col-sm-12 > div:nth-child(1) > h2')[0].innerText;
- var pdfURL;
- var promoID;
- if($('#acuseIframe').attr('src')){
- pdfURL = $('#acuseIframe').attr('src');
- promoID = pdfURL.split('\/').pop();
- }
- if($('#dlgFirma').attr('acuseiframe')){
- pdfURL = $('#dlgFirma').attr('acuseiframe');
- promoID = pdfURL.split('\\').pop().split('.').shift();
- }
- var fechaReportar = $('#txtFechaReportar').val();
- var filename = promoID + ' - ' + fechaReportar + ' - ' + razonSocial;
- filename = filename.replace(/\./g, ' ');
- filename = filename.substring(0,252);
- filename = filename + '.pdf';
- mods.replaceiFrame(filename);
- var downloadLink = $('#acuseIframe')[0];
- downloadLink.click();
- }
- function runListenerForPDF(){
- (function checkIfAllDone() {
- // your code
- if($('#acuseIframe').attr('src') || $('#dlgFirma').attr('acuseiframe')){
- mods.triggerDownloadLink();
- return;
- } else{
- setTimeout( checkIfAllDone, 1000 );
- }
- })();
- }
- function insertButtons(){
- if(!window.opener || !window.opener.$('#contrasena').val()){ return; }
- $("#divMainContainer > div.pull-right").append('<button class="btn btn-success navbar-btn" id="autofirmar">Autofirmar</button>');
- $("#divMainContainer > div.pull-right").append('<button class="btn btn-success navbar-btn" id="downloadpdf">Obtener PDF</button>');
- $('#autofirmar').click(function() {
- //alert('Loading');
- mods.autoFirmar();
- });
- $('#downloadpdf').click(function() {
- //alert('Loading');
- mods.triggerDownloadLink();
- });
- }
- function getTxtContenido(){
- $.ajax({
- type: 'POST',
- url: '/OPElectronica/ObtenerDatosFirmaPromocionFormularioElectronico',
- data: { id: $('#ContenedorFormulario').attr('data-promocion-id') },
- dataType: 'json',
- success: function (_resultado, textStatus, jqXHR) {
- if (_resultado.Errores == null) {
- var find = formularioElectronico.obtenerJSON();
- var str = JSON.stringify(find).replace(/"/g, "|");
- // Se asigna la cadena original al div para que sea utilizado pro el plug-in de firmado
- $('#txtContenido').val(_resultado.Valor + str + '||');
- //alert('txtcontenido loaded');
- // Se muestra la ventana modal para firmar la promoción
- //$('#dlgFirma').modal({ keyboard: false, backdrop: 'static' });
- } else {
- // Error al guardar
- for (var i = 0; i < _resultado.Errores.length; i++) {
- $.uMensaje({ sTipo: 'Error', sOrigen: _resultado.Errores[i].Origen, sMensaje: _resultado.Errores[i].Mensaje });
- }
- }
- },
- error: function (jqXHR, textStatus, error) {
- // Error del servicio
- $.uMensaje({ sTipo: 'Error', sOrigen: jqXHR.status, sMensaje: error });
- }
- });
- }
- function runAutoIfDetected(){
- if(location.search.split('auto=')[1]){
- mods.autoFirmar();
- }
- }
- mods = {};
- mods.replaceiFrame = replaceiFrame;
- mods.autoFirmar = autoFirmar;
- mods.triggerDownloadLink = triggerDownloadLink;
- replaceiFrame();
- insertButtons();
- getTxtContenido();
- runListenerForPDF();
- runAutoIfDetected();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement