Advertisement
borncorp

CRE GOB Tabla.user.js

Feb 5th, 2017
1,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         CRE GOB Tabla
  3. // @namespace    https://ope.cre.gob.mx/OPElectronica/PromocionesBorradores/
  4. // @version      0.8
  5. // @description  try to take over the world!
  6. // @author       You
  7. // @match        https://ope.cre.gob.mx/OPElectronica/PromocionesBorradores
  8. // @grant        window.open
  9. // @downloadURL  http://pastebin.com/raw/TQ81yFX5
  10. // ==/UserScript==
  11.  
  12. function getCleanRecords(){
  13.     var results = getResults();
  14.     var myRecords = getFilteredRecords(results);
  15.     return myRecords;
  16.    
  17.     //##### HELPER FUNCTIONS #####
  18.     function getResults(){
  19.         var resultados;
  20.         $.ajax({
  21.             type: 'POST',
  22.             url: '/OPElectronica/ObtenerPromocionesBorradores',
  23.             data: { tipo: 1 },
  24.             async: false,
  25.             dataType: 'json',
  26.             success: function (_resultado, textStatus, jqXHR) {
  27.  
  28.                 if (_resultado.Errores == null) {
  29.                     resultados = _resultado.Valor;
  30.                     return resultados;
  31.                     ////console.log("Resultado de obtener = " + JSON.stringify(_resultado.Valor));
  32.                     console.log("Done");
  33.                    
  34.                 } else {
  35.                 }
  36.             },
  37.             error: function (jqXHR, textStatus, error) {
  38.                 // Error del servicio
  39.                 $.tMensaje({ sTipo: 'Error', sOrigen: jqXHR.status, sMensaje: error });
  40.             }
  41.         });
  42.         return resultados;
  43.     }
  44.  
  45.     function getFilteredRecords(resultsObj){
  46.         var myTable = [];
  47.         for(var i in resultsObj){
  48.             var result = resultsObj[i];
  49.             var row = {};
  50.             row.tipo = getTypeString(result.TipoPromocionId);
  51.             row.link = getLink(result);
  52.             row.asuntoText = result.Asunto;
  53.             row.JSONInstanciaId = result.JSONInstanciaId;
  54.             row.asunto = '<a target="_blank" href="' + row.link + '" title="' + result.Asunto + '">' + result.Asunto + '</a>';
  55.             row.auto = '<a target="_blank" href="' + row.link + '&auto=true ' +'" title="Auto">' + 'Auto' + '</a>';
  56.             row.empresa = result.RazonSocial;
  57.             if(!row.empresa){ continue; }
  58.             myTable.push(row);
  59.         }
  60.         //console.log(myTable);
  61.         return myTable;
  62.        
  63.         //#### HELPER FUNCTIONS ####
  64.         function getTypeString(type){
  65.             if(type == 1){ return 'Promoción'}
  66.             if(type == 2){ return 'Reporte Trimestral'}
  67.             if(type == 3){ return 'Solicitud de Permiso'}
  68.             if(type == 4){ return 'Registro'}
  69.             if(type == 5){ return 'Obligación'}
  70.             if(type == 6){ return 'Obligaciones por empresa'}
  71.         }
  72.        
  73.         function getLink(oDocumento){
  74.             if (typeof oDocumento != "undefined") {
  75.                 switch (oDocumento.TipoPromocionId) {
  76.                     case 1: // Promoción
  77.                         return 'NuevaPromocion?id=' + oDocumento.Id;
  78.                         break;
  79.                     case 2: // Reporte Trimestral
  80.                         return '/Electricidad/Reporte?id=' + oDocumento.Id;
  81.                         break;     
  82.                     case 3: // Solicitud de Permiso
  83.                         if (oDocumento.JSONInstanciaId !== '')
  84.                             return 'PromocionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
  85.                         else
  86.                             return 'SolicitudPermiso?id=' + oDocumento.Id;
  87.                         break;
  88.                     case 4: // Registro
  89.                         return 'PromocionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
  90.                         break;
  91.                     case 5: // Obligación
  92.                         return 'ObligacionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
  93.                         break;
  94.                     case 6: // Obligaciones por empresa
  95.                         return 'ObligacionesEmpresaFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
  96.                         break;
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. function checkLinks(myRecords){
  104.    
  105.     //Removing unreachable links
  106.     var done = [];
  107.    
  108.     for(var i in myRecords){
  109.         checkLink(myRecords[i]);           
  110.     }
  111.    
  112.     (function checkIfAllDone() {
  113.         // your code
  114.         if(done.length == myRecords.length){
  115.             confirmedGlobal = true;
  116.             return;
  117.         } else{
  118.             setTimeout( checkIfAllDone, 5000 );
  119.         }
  120.     })();
  121.  
  122.     function checkLink(record){
  123.         if (!record.asuntoText){
  124.             done.push(record);
  125.             return;
  126.         }
  127.         var link = record.link;
  128.  
  129.         $.ajax({
  130.             type: 'HEAD',
  131.             url: link,
  132.             success: function() {
  133.                 confirmedRecords.push(record);
  134.                 done.push(record);
  135.                 //console.log('Page exists');
  136.                     // page exists
  137.             },
  138.             error: function() {
  139.                 done.push(record);
  140.                     // page does not exist
  141.             }
  142.         });
  143.     }
  144. }
  145.  
  146. function openFilteredWindow(myRecords){
  147.     myWin = openWindowPart2(confirmedRecords);
  148.     return myWin;
  149.     //openWindow(myRecords);
  150.     //return myRecords;
  151.    
  152.     //#### HELPER FUNCTIONS ####
  153.     function runListenerForConfirmed(){
  154.         (function checkIfAllDone() {
  155.             // your code
  156.             if(confirmedGlobal){
  157.                 confirmedGlobal = false;
  158.                 openWindowPart2(confirmedRecords);
  159.                 return;
  160.             } else{
  161.                 setTimeout( checkIfAllDone, 1000 );
  162.             }
  163.         })();
  164.     }
  165.  
  166.            
  167.     function openWindowPart2(){
  168.         var myForm  ='<div class="content"> <form action="#" role="form" onsubmit="return false"> <div class="modal-body with-padding"> <div class="form-group"> <div class="row"> <div class="col-sm-12"> <label>Certificado (*.cer): </label> <input id="certificado" name="certificado" type="file" class="styled" accept=".cer,.cert"> </div> </div> </div>  <div class="form-group"> <div class="row"> <div class="col-sm-12"> <label>Clave privada (*.key): </label> <input id="clave" name="clave" type="file" class="styled" accept=".key"> </div> </div> </div>  <div class="form-group"> <div class="row"> <div class="col-sm-12"> <label>Contraseña:</label> <input id="contrasena" name="contrasena" type="password" class="form-control"> </div> </div> </div> </div>  </form> </div>';
  169.        
  170.         var myHTML = '<html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/Dynatable/0.3.1/jquery.dynatable.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/Dynatable/0.3.1/jquery.dynatable.min.css"></head><body>' + myForm + '<table id="my-final-table" class="table table-bordered"><button class="btn btn-success navbar-btn" id="reloadList">Reload</button><thead> <th>Tipo</th> <th>Empresa</th>     <th>Asunto</th> <th>FechaReportar</th><th>Auto</th>  </thead>   <tbody>   </tbody> </table><script>function updateDynTable(someRecords){ myRecords = someRecords || window.opener.confirmedRecords; var dynatable = $("#my-final-table").dynatable({ dataset: { records: myRecords, perPageDefault : 100 } }, { features: { pushState: false }}).data("dynatable"); dynatable.settings.dataset.originalRecords =  myRecords; dynatable.process(); } function updateDynTableFromOriginalWindow(){ window.opener.myConfig.updateOpenedTable()}; $("#reloadList").click(function() { updateDynTableFromOriginalWindow(); }); updateDynTableFromOriginalWindow(); </script></body></html>';
  171.        
  172.         myWin = window.open('', 'CRE GOB Tabla');
  173.         //myWin.myRecords = myRecords;
  174.         myWin.document.write(myHTML);
  175.         return myWin;
  176.     }
  177. }
  178.  
  179. function appendDate(record, index){
  180.     $.ajax({
  181.         type: "GET",
  182.         url: "https://formulario.cre.gob.mx/api/Instancia/" + record.JSONInstanciaId,
  183.         dataType: "json",
  184.         success: function(data) {
  185.             try{
  186.                 var fechaReportar = JSON.parse(data.Objeto).Persistencia.Encabezado.FechaReportar;
  187.                 confirmedRecords[index].fechaReportar = fechaReportar;
  188.             } catch(e){
  189.                 confirmedRecords[index].fechaReportar = 'Error';
  190.             }
  191.         },
  192.         error: function(XMLHttpRequest, textStatus, errorThrown){
  193.             confirmedRecords[index].fechaReportar = 'Error';
  194.         },
  195.         complete: function(data){
  196.             linksProcessed++;          
  197.         }
  198.     });
  199. }
  200.  
  201. function updateOpenedTable(){
  202.     confirmedGlobal = false;
  203.     confirmedRecords = [];
  204.    
  205.     var myRecords = getCleanRecords();
  206.    
  207.     checkLinks(myRecords);
  208.     runListenerForConfirmed();
  209.    
  210.     //openWindow(myRecords);
  211.     return myRecords;
  212.    
  213.     //#### HELPER FUNCTIONS ####
  214.     function runListenerForConfirmed(){
  215.         (function checkIfAllDone() {
  216.             // your code
  217.             if(confirmedGlobal){
  218.                 confirmedGlobal = false;
  219.                 listenForDates();
  220.                 return;
  221.             } else{
  222.                 setTimeout( checkIfAllDone, 1000 );
  223.             }
  224.         })();
  225.     }
  226.    
  227.     function listenForDates(){
  228.         linksProcessed = 0;
  229.         for(var i in confirmedRecords){
  230.             myConfig.appendDate(confirmedRecords[i], i);
  231.         }
  232.        
  233.         (function checkIfAllDone() {
  234.             // your code
  235.             console.log('confirmedRecords.length' + confirmedRecords.length + ' linksProcessed ' + linksProcessed);
  236.             if(confirmedRecords.length == linksProcessed){
  237.                 myWin.updateDynTable(confirmedRecords);
  238.                 return;
  239.             } else{
  240.                 setTimeout( checkIfAllDone, 1000 );
  241.             }
  242.         })();
  243.     }
  244. }
  245.  
  246. function insertButton(){
  247.     $("body > div.container.top-buffer-submenu > div.row > div.col-md-4 > div > ul").append('<li><button class="btn btn-success navbar-btn" id="openFilteredList">Open Filtered List</button></li>');
  248.    
  249.     $('#openFilteredList').click(function() {
  250.         //alert('Loading');
  251.         myWin = openFilteredWindow();
  252.     });
  253. }
  254.  
  255. linksProcessed = 0;
  256. confirmedGlobal = false;
  257. confirmedRecords = [];
  258. myConfig = {};
  259. myConfig.updateOpenedTable = updateOpenedTable;
  260. myConfig.openFilteredWindow = openFilteredWindow;
  261. myConfig.getCleanRecords = getCleanRecords;
  262. myConfig.checkLinks = checkLinks;
  263. myConfig.appendDate = appendDate;
  264. myWin = false;
  265. insertButton();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement