Advertisement
borncorp

CRE GOB Autofirmar.user.js

Feb 5th, 2017
1,897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         CRE GOB Autofirmar
  3. // @namespace    https://ope.cre.gob.mx/OPElectronica/ObligacionFormularioElectronico/
  4. // @version      0.6
  5. // @description  try to take over the world!
  6. // @author       You
  7. // @match        https://ope.cre.gob.mx/OPElectronica/ObligacionFormularioElectronico*
  8. // @grant        window.open
  9. // @downloadURL  http://pastebin.com/raw/bgjsiq4U
  10. // ==/UserScript==
  11.  
  12. function autoFirmar(){ 
  13.     (function checkIfAllFilledAndSend() {
  14.         var contrasenaMain = window.opener.$('#contrasena').val();
  15.         var certificadoMain = window.opener.$('#certificado')[0].files[0];
  16.         var claveMain = window.opener.$('#clave')[0].files[0];
  17.         $('#certificado')[0].files[0] = certificadoMain;
  18.         $('#clave')[0].files[0] = claveMain;
  19.         $('#contrasena').val(contrasenaMain);
  20.        
  21.         // your code
  22.         console.log('Checking if has values');
  23.         console.log('Certificado:' + !!$('#certificado')[0].files[0]);
  24.         console.log('Clave:' + !!$('#clave')[0].files[0]);
  25.         console.log('txtContenido' + !!$('#txtContenido').val());
  26.         if($('#certificado')[0].files[0] && $('#clave')[0].files[0] && $('#contrasena').val() && $('#txtContenido').val()){
  27.             $('#dlgFirma').uFirma().firmar($('#txtContenido').val(), $('#dlgFirma').attr('data-rfc'));
  28.             //mods.triggerDownloadLink();
  29.             return;
  30.         } else{
  31.             setTimeout( checkIfAllFilledAndSend, 1000 );
  32.         }
  33.     })();
  34.     return;
  35. }
  36.  
  37. function replaceiFrame(filename){
  38.     var pdfURL;
  39.    
  40.     if($('#acuseIframe').attr('src')){
  41.         pdfURL = $('#acuseIframe').attr('src')  || '';
  42.     }
  43.  
  44.     if($('#dlgFirma').attr('acuseiframe')){
  45.         pdfURL = $('#dlgFirma').attr('acuseiframe')  || '';
  46.     }
  47.    
  48.     if (pdfURL == "undefined"){
  49.         pdfURL = '';
  50.     }
  51.    
  52.     var oldURL = $('#acuseIframe').attr('href') || '';
  53.     if((pdfURL == oldURL) || !pdfURL){return;}
  54.     $('#acuseIframe').replaceWith($('<a src = "' + pdfURL + '" href="' + pdfURL + '" download="' + filename + '" id="acuseIframe"></a>'));
  55. }
  56.  
  57. function triggerDownloadLink(){
  58.     var razonSocial = $('#txtRazonSocial')[0].value;
  59.     var description = $('#formulario > div.col-sm-12 > div:nth-child(1) > h2')[0].innerText;
  60.     var pdfURL;
  61.     var promoID;
  62.     if($('#acuseIframe').attr('src')){
  63.         pdfURL = $('#acuseIframe').attr('src');
  64.         promoID = pdfURL.split('\/').pop();
  65.     }
  66.  
  67.     if($('#dlgFirma').attr('acuseiframe')){
  68.         pdfURL = $('#dlgFirma').attr('acuseiframe');
  69.         promoID = pdfURL.split('\\').pop().split('.').shift();
  70.     }
  71.    
  72.     var fechaReportar = $('#txtFechaReportar').val();
  73.    
  74.     var filename = promoID + ' - ' + fechaReportar + ' - ' + razonSocial;
  75.     filename = filename.replace(/\./g, ' ');
  76.     filename = filename.substring(0,252);
  77.     filename = filename + '.pdf';
  78.  
  79.     mods.replaceiFrame(filename);
  80.    
  81.     var downloadLink = $('#acuseIframe')[0];
  82.     downloadLink.click();
  83. }
  84.  
  85. function runListenerForPDF(){
  86.     (function checkIfAllDone() {
  87.         // your code
  88.         if($('#acuseIframe').attr('src') || $('#dlgFirma').attr('acuseiframe')){
  89.             mods.triggerDownloadLink();
  90.             return;
  91.         } else{
  92.             setTimeout( checkIfAllDone, 1000 );
  93.         }
  94.     })();
  95. }
  96.  
  97. function insertButtons(){
  98.     if(!window.opener || !window.opener.$('#contrasena').val()){ return; }
  99.     $("#divMainContainer > div.pull-right").append('<button class="btn btn-success navbar-btn" id="autofirmar">Autofirmar</button>');
  100.     $("#divMainContainer > div.pull-right").append('<button class="btn btn-success navbar-btn" id="downloadpdf">Obtener PDF</button>');
  101.  
  102.     $('#autofirmar').click(function() {
  103.         //alert('Loading');
  104.         mods.autoFirmar();
  105.     });
  106.    
  107.     $('#downloadpdf').click(function() {
  108.         //alert('Loading');
  109.         mods.triggerDownloadLink();
  110.     });
  111. }
  112.  
  113. function getTxtContenido(){
  114.     $.ajax({
  115.         type: 'POST',
  116.         url: '/OPElectronica/ObtenerDatosFirmaPromocionFormularioElectronico',
  117.         data: { id: $('#ContenedorFormulario').attr('data-promocion-id') },
  118.         dataType: 'json',
  119.         success: function (_resultado, textStatus, jqXHR) {
  120.             if (_resultado.Errores == null) {
  121.                 var find = formularioElectronico.obtenerJSON();
  122.                 var str = JSON.stringify(find).replace(/"/g, "|");
  123.  
  124.                 // Se asigna la cadena original al div para que sea utilizado pro el plug-in de firmado
  125.                 $('#txtContenido').val(_resultado.Valor + str + '||');
  126.                 //alert('txtcontenido loaded');
  127.                 // Se muestra la ventana modal para firmar la promoción
  128.                 //$('#dlgFirma').modal({ keyboard: false, backdrop: 'static' });
  129.  
  130.             } else {
  131.                 // Error al guardar
  132.                 for (var i = 0; i < _resultado.Errores.length; i++) {
  133.                     $.uMensaje({ sTipo: 'Error', sOrigen: _resultado.Errores[i].Origen, sMensaje: _resultado.Errores[i].Mensaje });
  134.                 }
  135.             }
  136.         },
  137.         error: function (jqXHR, textStatus, error) {
  138.             // Error del servicio
  139.             $.uMensaje({ sTipo: 'Error', sOrigen: jqXHR.status, sMensaje: error });
  140.         }
  141.     });
  142. }
  143.  
  144. function runAutoIfDetected(){
  145.     if(location.search.split('auto=')[1]){
  146.         mods.autoFirmar();
  147.     }
  148. }
  149.  
  150. mods = {};
  151. mods.replaceiFrame = replaceiFrame;
  152. mods.autoFirmar = autoFirmar;
  153. mods.triggerDownloadLink = triggerDownloadLink;
  154. replaceiFrame();
  155. insertButtons();
  156. getTxtContenido();
  157. runListenerForPDF();
  158. runAutoIfDetected();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement