Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name CRE GOB Tabla
- // @namespace https://ope.cre.gob.mx/OPElectronica/PromocionesBorradores/
- // @version 0.8
- // @description try to take over the world!
- // @author You
- // @match https://ope.cre.gob.mx/OPElectronica/PromocionesBorradores
- // @grant window.open
- // @downloadURL http://pastebin.com/raw/TQ81yFX5
- // ==/UserScript==
- function getCleanRecords(){
- var results = getResults();
- var myRecords = getFilteredRecords(results);
- return myRecords;
- //##### HELPER FUNCTIONS #####
- function getResults(){
- var resultados;
- $.ajax({
- type: 'POST',
- url: '/OPElectronica/ObtenerPromocionesBorradores',
- data: { tipo: 1 },
- async: false,
- dataType: 'json',
- success: function (_resultado, textStatus, jqXHR) {
- if (_resultado.Errores == null) {
- resultados = _resultado.Valor;
- return resultados;
- ////console.log("Resultado de obtener = " + JSON.stringify(_resultado.Valor));
- console.log("Done");
- } else {
- }
- },
- error: function (jqXHR, textStatus, error) {
- // Error del servicio
- $.tMensaje({ sTipo: 'Error', sOrigen: jqXHR.status, sMensaje: error });
- }
- });
- return resultados;
- }
- function getFilteredRecords(resultsObj){
- var myTable = [];
- for(var i in resultsObj){
- var result = resultsObj[i];
- var row = {};
- row.tipo = getTypeString(result.TipoPromocionId);
- row.link = getLink(result);
- row.asuntoText = result.Asunto;
- row.JSONInstanciaId = result.JSONInstanciaId;
- row.asunto = '<a target="_blank" href="' + row.link + '" title="' + result.Asunto + '">' + result.Asunto + '</a>';
- row.auto = '<a target="_blank" href="' + row.link + '&auto=true ' +'" title="Auto">' + 'Auto' + '</a>';
- row.empresa = result.RazonSocial;
- if(!row.empresa){ continue; }
- myTable.push(row);
- }
- //console.log(myTable);
- return myTable;
- //#### HELPER FUNCTIONS ####
- function getTypeString(type){
- if(type == 1){ return 'Promoción'}
- if(type == 2){ return 'Reporte Trimestral'}
- if(type == 3){ return 'Solicitud de Permiso'}
- if(type == 4){ return 'Registro'}
- if(type == 5){ return 'Obligación'}
- if(type == 6){ return 'Obligaciones por empresa'}
- }
- function getLink(oDocumento){
- if (typeof oDocumento != "undefined") {
- switch (oDocumento.TipoPromocionId) {
- case 1: // Promoción
- return 'NuevaPromocion?id=' + oDocumento.Id;
- break;
- case 2: // Reporte Trimestral
- return '/Electricidad/Reporte?id=' + oDocumento.Id;
- break;
- case 3: // Solicitud de Permiso
- if (oDocumento.JSONInstanciaId !== '')
- return 'PromocionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
- else
- return 'SolicitudPermiso?id=' + oDocumento.Id;
- break;
- case 4: // Registro
- return 'PromocionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
- break;
- case 5: // Obligación
- return 'ObligacionFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
- break;
- case 6: // Obligaciones por empresa
- return 'ObligacionesEmpresaFormularioElectronico?jsonInstanciaId=' + oDocumento.JSONInstanciaId;
- break;
- }
- }
- }
- }
- }
- function checkLinks(myRecords){
- //Removing unreachable links
- var done = [];
- for(var i in myRecords){
- checkLink(myRecords[i]);
- }
- (function checkIfAllDone() {
- // your code
- if(done.length == myRecords.length){
- confirmedGlobal = true;
- return;
- } else{
- setTimeout( checkIfAllDone, 5000 );
- }
- })();
- function checkLink(record){
- if (!record.asuntoText){
- done.push(record);
- return;
- }
- var link = record.link;
- $.ajax({
- type: 'HEAD',
- url: link,
- success: function() {
- confirmedRecords.push(record);
- done.push(record);
- //console.log('Page exists');
- // page exists
- },
- error: function() {
- done.push(record);
- // page does not exist
- }
- });
- }
- }
- function openFilteredWindow(myRecords){
- myWin = openWindowPart2(confirmedRecords);
- return myWin;
- //openWindow(myRecords);
- //return myRecords;
- //#### HELPER FUNCTIONS ####
- function runListenerForConfirmed(){
- (function checkIfAllDone() {
- // your code
- if(confirmedGlobal){
- confirmedGlobal = false;
- openWindowPart2(confirmedRecords);
- return;
- } else{
- setTimeout( checkIfAllDone, 1000 );
- }
- })();
- }
- function openWindowPart2(){
- 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>';
- 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>';
- myWin = window.open('', 'CRE GOB Tabla');
- //myWin.myRecords = myRecords;
- myWin.document.write(myHTML);
- return myWin;
- }
- }
- function appendDate(record, index){
- $.ajax({
- type: "GET",
- url: "https://formulario.cre.gob.mx/api/Instancia/" + record.JSONInstanciaId,
- dataType: "json",
- success: function(data) {
- try{
- var fechaReportar = JSON.parse(data.Objeto).Persistencia.Encabezado.FechaReportar;
- confirmedRecords[index].fechaReportar = fechaReportar;
- } catch(e){
- confirmedRecords[index].fechaReportar = 'Error';
- }
- },
- error: function(XMLHttpRequest, textStatus, errorThrown){
- confirmedRecords[index].fechaReportar = 'Error';
- },
- complete: function(data){
- linksProcessed++;
- }
- });
- }
- function updateOpenedTable(){
- confirmedGlobal = false;
- confirmedRecords = [];
- var myRecords = getCleanRecords();
- checkLinks(myRecords);
- runListenerForConfirmed();
- //openWindow(myRecords);
- return myRecords;
- //#### HELPER FUNCTIONS ####
- function runListenerForConfirmed(){
- (function checkIfAllDone() {
- // your code
- if(confirmedGlobal){
- confirmedGlobal = false;
- listenForDates();
- return;
- } else{
- setTimeout( checkIfAllDone, 1000 );
- }
- })();
- }
- function listenForDates(){
- linksProcessed = 0;
- for(var i in confirmedRecords){
- myConfig.appendDate(confirmedRecords[i], i);
- }
- (function checkIfAllDone() {
- // your code
- console.log('confirmedRecords.length' + confirmedRecords.length + ' linksProcessed ' + linksProcessed);
- if(confirmedRecords.length == linksProcessed){
- myWin.updateDynTable(confirmedRecords);
- return;
- } else{
- setTimeout( checkIfAllDone, 1000 );
- }
- })();
- }
- }
- function insertButton(){
- $("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>');
- $('#openFilteredList').click(function() {
- //alert('Loading');
- myWin = openFilteredWindow();
- });
- }
- linksProcessed = 0;
- confirmedGlobal = false;
- confirmedRecords = [];
- myConfig = {};
- myConfig.updateOpenedTable = updateOpenedTable;
- myConfig.openFilteredWindow = openFilteredWindow;
- myConfig.getCleanRecords = getCleanRecords;
- myConfig.checkLinks = checkLinks;
- myConfig.appendDate = appendDate;
- myWin = false;
- insertButton();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement