chako / SupervisorSSCN

// ==UserScript==
// @author chako@PNScoder
// @name SupervisorSSCN
// @version  2.4.5
// @description Untuk SUPERVISOR SSCN : Helper supervisi persyaratan yang sudah diverifikasi oleh verifikator
// @copyright perdana1cell@gmail.com 2019
// @license MIT
// @updateURL https://openuserjs.org/meta/chako/SupervisorSSCN.meta.js
// @downloadURL https://openuserjs.org/install/chako/SupervisorSSCN.user.js
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_log
// @grant GM_deleteValue
// @grant GM_notification
// @grant GM_openInTab
// @grant GM_setClipboard
// @grant GM_info
// @grant GM_addStyle
// @namespace https://sscnverifikasi.bkn.go.id/supervisorverifikasi.do
// @match https://sscnverifikasi.bkn.go.id/supervisorverifikasi.do*
// @require http://userscripts-mirror.org/scripts/source/107941.user.js
// ==/UserScript==

var isTabMode = GM_SuperValue.get ('isTabMode', 'tab') == 'tab';
var idRegs = GM_SuperValue.get ('idRegsQueue', []);
var idAcc = GM_SuperValue.get ('idRegsAcc', []);
var idTms = GM_SuperValue.get ('idRegsTms', []);

let e = document.querySelector("#defaultSearchField");
e.focus();
e.select();

var isLulus = $("#myTable > tbody > tr:nth-child(1) > td:nth-child(5)").html() == 'Lulus';
var openDoc = true;//!isLulus;

var noreg = $("#defaultSearchField").val();
var firstreg = $("#myTable > tbody > tr:nth-child(1) > td:nth-child(2)").html();
if (noreg != '' && noreg == firstreg) {
    $("#myTable > tbody > tr > td:nth-child(7) > a").trigger('click');
} else {
    if(noreg != '' && firstreg != null){
        //handle back browser
        $("#searchBar > div:nth-child(7) > div > a").trigger('click');
    } else if (idRegs.length > 0){
        if(noreg !== $.trim(idRegs[0])){
            //load antrian selanjutnya
            noreg = $.trim(idRegs[0]);
            $("#defaultSearchField").val(noreg);

            if(checkRegister(noreg)){
                $("#searchBar > div:nth-child(7) > div > a").trigger('click');
            } else {
                //remove bila nomor register tidak sesuai pattern
                if(idRegs.splice( $.inArray(noreg, idRegs), 1 ).length > 0){
                    GM_SuperValue.set ('idRegsQueue', idRegs);

                    GM_notification ({
                        title: noreg,
                        text: 'Cek kembali nomor register',
                        ondone: () => {
                            location.reload();
                        }
                    });
                }
            }
        } else {
            //remove bila no register sudah diproses
            if(idRegs.splice( $.inArray(noreg, idRegs), 1 ).length > 0){
                GM_SuperValue.set ('idRegsQueue', idRegs);

                GM_notification ( {
                    title: noreg,
                    text: 'Nomor register '+noreg+' sudah terverifikasi. Refresh halaman untuk membuka antrian berikutnya',
                    ondone: () => {
                        location.reload();
                    }
                } );
            }
        }

    }
}

//header buttons
let btnMode = ' <button type="button" id="btnMode" class="btn btn-sm btn-warning">'+ (isTabMode? 'Mode Popup' : 'Mode Tab') +'</button> ';
let btnAddQueue = ' <button type="button" id="btnAddQueue" class="btn btn-sm btn-success">Tambah Antrian</button> ';
let btnViewQueue = ' <button type="button" id="btnViewQueue" class="btn btn-sm btn-info">Lihat Antrian</button> ';
let btnViewAcc = ' <button type="button" id="btnViewAcc" class="btn btn-sm btn-info">Lihat SESUAI</button> ';
let btnViewTms = ' <button type="button" id="btnViewTms" class="btn btn-sm btn-info">Lihat VERIFIKASI ULANG</button> ';
let btnResetQueue = ' <button type="button" id="btnResetQueue" class="btn btn-sm btn-danger">Reset Antrian</button> ';
let btnViewInfo = ' <button type="button" id="btnViewInfo" class="btn btn-sm btn-primary">Informasi</button> ';
let info = ' ' + GM_info.script.name + ' v.' + GM_info.script.version + '. copyright ' + GM_info.script.author + ' ('+GM_info.script.copyright+')';
$('body').prepend('<div class="alert alert-success text-center" role="alert">' + btnAddQueue + btnViewQueue + btnViewAcc + btnViewTms + btnResetQueue + btnViewInfo + btnMode + '</div>');
$('body').append('<footer class="footer-copyright"><strong>'+info+'</strong></footer>');
$('body').append('<div id="myCustomDialog" style="overflow-x: hidden; width: auto; min-height: 0px; height: 853.249px;" class="ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0"><div class="row"><div class="col-md-12"><textarea id="taRegister" rows="20" cols="20" style="width: 100%; margin: 10px;" required></textarea></div></div><div class="row"><div class="col-md-12"><button type="button" id="btnSaveReg" class="btn btn-success pull-right">Simpan</button> <button type="button" id="btnClose" class="btn btn-warning pull-right">Batal</button> </div></div></div>');

GM_addStyle('.footer-copyright {position: fixed; left: 0px; bottom: 0px; vertical-align: center; width: 100%; padding: .75rem 1.25rem; text-align: center; color: #721c24; background-color: #f8d7da; border-color: #f5c6cb;}');

/LOGIC MENU COMMAND/
GM_registerMenuCommand ("Tambah Antrian", addRegisterNumbers, "A");
GM_registerMenuCommand ("Lihat Antrian", viewQueue, "Q");
GM_registerMenuCommand ("Lihat SESUAI", viewAcc, "Y");
GM_registerMenuCommand ("Lihat VERIFIKASI ULANG", viewTms, "N");
GM_registerMenuCommand ("Reset Antrian", resetQueue, "X");
GM_registerMenuCommand ("Informasi", viewInfo, "I");

var IdCommandMode = null;
if(isTabMode){
    IdCommandMode = GM_registerMenuCommand ("Mode Popup", modePopup, "P");
    document.getElementById('btnMode').addEventListener('click', modePopup);
} else {
    IdCommandMode = GM_registerMenuCommand ("Mode Tab", modeTab, "P");
    document.getElementById('btnMode').addEventListener('click', modeTab);
}

function modeTab () {
    isTabMode = true;

    $('#btnMode').html('Mode Popup');
    document.getElementById('btnMode').removeEventListener('click', modeTab);
    document.getElementById('btnMode').addEventListener('click', modePopup);

    GM_unregisterMenuCommand(IdCommandMode);
    IdCommandMode = GM_registerMenuCommand ("Mode Popup", modePopup, "T");
    GM_SuperValue.set ('isTabMode', 'tab');
    GM_notification ( {title: 'Mode Tab', text: 'Mode buka dokumen di new tab diaktifkan'} );
}

function modePopup () {
    isTabMode = false;

    $('#btnMode').html('Mode Tab');
    document.getElementById('btnMode').removeEventListener('click', modePopup);
    document.getElementById('btnMode').addEventListener('click', modeTab);

    GM_unregisterMenuCommand(IdCommandMode);
    IdCommandMode = GM_registerMenuCommand ("Mode Tab", modeTab, "T");
    GM_SuperValue.set ('isTabMode', 'popup');
    GM_notification ( {title: 'Mode Popup', text: 'Mode buka dokumen di form verifikasi diaktifkan'} );
}

function addRegisterNumbers () {
    $( "#myCustomDialog" ).dialog({ title: "Tambah Nomor Register" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveReg').show();

    $('textarea#taRegister').val('');
    $('textarea#taRegister').prop('disabled',false);
}

function viewQueue () {
    $( "#myCustomDialog" ).dialog({ title: "Daftar Antrian" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveReg').hide();

    $('textarea#taRegister').val('');

    let idRegs = GM_SuperValue.get ('idRegsQueue', []);
    if(idRegs !== null){
        $('textarea#taRegister').val(idRegs.join('\n'));
    }
    $('textarea#taRegister').prop('disabled',true);
}

function viewAcc () {
    $( "#myCustomDialog" ).dialog({ title: "Daftar SESUAI" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveReg').hide();

    $('textarea#taRegister').val('');

    let idRegs = GM_SuperValue.get ('idRegsAcc', []);
    if(idRegs !== null){
        $('textarea#taRegister').val(idRegs.reverse().join('\n'));
    }
    $('textarea#taRegister').prop('disabled',true);
}

function viewTms () {
    $( "#myCustomDialog" ).dialog({ title: "Daftar VERIFIKASI ULANG" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveReg').hide();

    $('textarea#taRegister').val('');

    let idRegs = GM_SuperValue.get ('idRegsTms', []);
    if(idRegs !== null){
        $('textarea#taRegister').val(idRegs.reverse().join('\n'));
    }
    $('textarea#taRegister').prop('disabled',true);
}

function resetQueue () {
    GM_deleteValue('idRegsQueue');
    GM_notification ( {title: 'Berhasil', text: 'Antrian berhasil direset'} );
}

function viewInfo () {
    GM_notification ({title: $('#header > div > div > div.col-sm-9.new-btn-header > div:nth-child(1) > div > div > button.btn.btn-sm.btn-primary > b').html(), text: '\
Program hanyalah sebuah tools pembantu, ketelitian dalam memverifikasi berkas tetap harus dijaga. Ganbatte!'});
}

function getElementAccredited(){
    if($('#lembaga > label > i').length > 0){
        let cloneLembaga = $("#lembaga").clone();
        cloneLembaga.addClass("alert alert-info text-center h5 divDocument");
        return cloneLembaga;
    }
    return null;
}

function getElementFormasi(){
    var elm = '';

    var formasi = $('#jenisFormasiPendaftar > label').html();
    if(formasi.indexOf('LULUSAN TERBAIK') != -1){
        elm += "<div class='alert alert-danger text-center h5 divDocument'>" + $("#jenisFormasiPendaftar").clone().html() + "</div>";
    }
    if(formasi.indexOf('PUTRA/PUTRI PAPUA DAN PAPUA BARAT') != -1){
        elm += "<div class='alert alert-danger text-center h5 divDocument'>" + $("#jenisFormasiPendaftar").clone().html() + "</div>";
    }
    if($('#disables').length > 0 && $('#disables').html() != ''){
        elm += "<div class='alert alert-danger text-center h5 divDocument'>" + $("#disables").clone().html() + "</div>";
    }
    if($("#isP1tl").length > 0){
        elm += "<div class='alert alert-warning text-center h5 divDocument'>" +  $("#pendidikanP1tl > div > div.col-md-10 > div").clone().html() + "</div>";
    }
    if($("#alasanSupervisorDiv > div > div > div > div").length > 0){
        elm += "<div class='alert alert-warning text-center h5 divDocument'>" +  $("#alasanSupervisorDiv > div > div > div > div").clone().html() + "</div>";
    }
    if($("#alasanSupervisorDiv > div > div > div > div").length > 0){
        elm += "<div class='alert alert-warning text-center h5 divDocument'>" +  $("#alasanSupervisorDiv > div > div > div > div").clone().html() + "</div>";
    }
    if($("#kelulusanDiv > div > div > div.col-md-10 > div").length > 0){
        elm += "<div class='alert alert-danger text-center h5 divDocument'>" +  $("#kelulusanDiv > div > div > div.col-md-10 > div").clone().html() + "</div>";
    }

    return elm != '' ? $(elm) : null;
}

function checkRegister(noreg){
    var pattern = /^\d{17}$/;
    return pattern.test(noreg);
}

let w = $(window).width();
let h = $(window).height();

$("#myCustomDialog").dialog({
    autoOpen: false,
    height: h,
    width: 1120,
    position: {
        my: "center",
        at: "top",
        of: window
    },
    modal: true,
    resizable: true,
    open: function(){
        $('.ui-widget-overlay').bind('click',function(){
            $('#myCustomDialog').dialog('close');
        });

        //action close button
        document.getElementById('btnClose').addEventListener('click', function() {
            $('#myCustomDialog').dialog('close');
        });
    }
});

//form verifikasi
$("#myModal2").dialog({
    autoOpen: false,
    height: h,
    width: w,
    position: {
        my: "center",
        at: "top",
        of: window
    },
    modal: true,
    open: function(event, ui) {
        if(openDoc){
            //open all doc
            var arrDoc = [
                "#persyaratanDiv > div:nth-child(1) > div > div:nth-child(2) > label > a",//pas foto
                "#persyaratanDiv > div:nth-child(2) > div:nth-child(2) > label > a",//ktp
                "#persyaratanDiv > div:nth-child(3) > div > div:nth-child(2) > label > a",
                "#persyaratanDiv > div:nth-child(4) > div:nth-child(2) > label > a",
                "#persyaratanDiv > div:nth-child(5) > div > div:nth-child(2) > label > a",
                "#persyaratanDiv > div:nth-child(6) > div:nth-child(2) > label > a",
            ];

            if(!isTabMode){
                let cloneDiv = $("#formSupervisi > div:nth-child(5)").clone();
                cloneDiv.addClass("divDocument");
                cloneDiv.insertAfter("#formSupervisi > div:nth-child(5)");
            }

            //info pelamar lulusan terbaik / papua
            let formasi = getElementFormasi();
            if(formasi != null){
                formasi.insertAfter("body > div:nth-child(11) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix");
                formasi.clone().insertAfter("#myModal2");
            }

            //info lembaga terakreditasi
            let lembaga = getElementAccredited();
            if(lembaga != null){
                lembaga.insertAfter("body > div:nth-child(11) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix");
                lembaga.clone().insertAfter("#myModal2");
            }

            $.each(arrDoc.reverse(), function( index, doc ) {
                var isFoto = index == 5;
                var doc_id = document.querySelector(doc).getAttribute("onclick").substring(22, 54);
                if(isTabMode){
                    //README : Block or allow pop-ups from a specific site
                    //https://support.google.com/chrome/answer/95472?co=GENIE.Platform%3DDesktop&hl=en-GB
                    GM_openInTab('https://sscnverifikasi.bkn.go.id/showdokumen.do?dokumenpindai='+doc_id/*, {active:isFoto}*/);
                } else {
                    //load dokumen di form verifikasi
                    let height = isFoto ? 614 : 740;
                    let e = document.createElement('div');
                    e.setAttribute('class', (isFoto ? 'col-md-6' : 'col-md-12')+' divDocument');
                    e.setAttribute('style', 'padding: 0 150px;');
                    e.innerHTML = "<div id='docModal_"+index+"' title='' style='overflow-x:hidden'><object type='application/pdf' data='https://sscnverifikasi.bkn.go.id/showdokumen.do?dokumenpindai="+doc_id+"' width='100%' height='"+height+"'>No Support</object></div><br>";
                    $(e).insertAfter( "#formSupervisi > div:nth-child(5)" );
                }
            });
        }

        //logic button final
       /* if($("#pendidikanP1tl").length > 0){
            //kalau PLT1 kasih warning
            //alert($('#pendidikanP1tl > div > div.col-md-10 > div > label').text());
        } else {
            if(isLulus){
                //kalau bukan, click button final
                document.querySelector("#formSupervisi > div:nth-child(6) > div:nth-child(11) > div > div > button.btn.btn-success.btn-large.supervisiBtn").click();
            }
        }*/
    },
    close: function(event, ui) {
        $( ".divDocument" ).remove();
    }
});

document.getElementById('btnAddQueue').addEventListener('click', addRegisterNumbers);
document.getElementById('btnViewQueue').addEventListener('click', viewQueue);
document.getElementById('btnViewAcc').addEventListener('click', viewAcc);
document.getElementById('btnViewTms').addEventListener('click', viewTms);
document.getElementById('btnResetQueue').addEventListener('click', resetQueue);
document.getElementById('btnViewInfo').addEventListener('click', viewInfo);

//action tambah no register
document.getElementById('btnSaveReg').addEventListener('click', function() {
    var val = $('textarea#taRegister').val();
    if(val.length > 0){
        var arr = val.split(/\s+/g);

        var initArr = GM_SuperValue.get ('idRegsQueue', []);
        $.each(arr, function( index, reg ) {
            initArr.push(reg);
        });
        GM_SuperValue.set ('idRegsQueue', initArr);

        $('#myCustomDialog').dialog('close');

        $("#defaultSearchField").val($.trim(initArr[0]));

        let reg = $.trim(initArr[0]);
        if(checkRegister(reg)){
            $("#searchBar > div:nth-child(7) > div > a").trigger('click');
            GM_notification ( {title: 'Berhasil', text: 'Antrian berhasil ditambah'} );
        } else {
            //GM_notification ( {title: $.trim(initArr[0]), text: 'Cek kembali nomor register'} );
            if(initArr.splice( $.inArray(reg, initArr), 1 ).length > 0){
                GM_SuperValue.set ('idRegsQueue', initArr);

                GM_notification ({
                    title: reg,
                    text: 'Cek kembali nomor register',
                    ondone: () => {
                        location.reload();
                    }
                });
            }
        }
    } else {
        GM_notification ( {title: 'Tidak Ada Data', text: 'Silakan isi nomor register yang akan dimasukkan ke dalam antrian'} );
    }
});

//logic button VERIFIKASI ULANG
$('body').on('click', '#formSupervisi > div:nth-child(6) > div:nth-child(11) > div > div > button.btn.btn-warning.btn-large.supervisiUlangBtn', function (){
    //remove no register yang sudah diproses
    idRegs.splice( $.inArray(noreg, idRegs), 1 );
    GM_SuperValue.set ('idRegsQueue', idRegs);

    //push no register tms
    if($('#keteranganSupervisor').length > 0){
        let ket = $('#keteranganSupervisor').val();
        noreg = noreg+' - '+ket;

        //copy keterangan di clipboard untuk paste di spreadsheet
        GM_setClipboard (noreg);
    }
    idTms.push(noreg);
    GM_SuperValue.set ('idRegsTms', idTms);
});

//logic button SESUAI
$('body').on('click', '#formSupervisi > div:nth-child(6) > div:nth-child(11) > div > div > button.btn.btn-success.btn-large.supervisiBtn', function (){
    //remove no register yang sudah diproses
    idRegs.splice( $.inArray(noreg, idRegs), 1 );
    GM_SuperValue.set ('idRegsQueue', idRegs);

    //push no register acc
    idAcc.push(noreg);
    GM_SuperValue.set ('idRegsAcc', idAcc);

    //copy status di clipboard untuk paste di spreadsheet
    GM_setClipboard (1);
});

//paste no register
$('#defaultSearchField').on('paste', function(elm){
    var e = $(this);
    setTimeout(function(){
        if(checkRegister($.trim(e.val()))){
            $("#searchBar > div:nth-child(7) > div > a").trigger('click');
        } else {
            GM_notification ( {title: e.val(), text: 'Cek kembali nomor register'} );
        }
    }, 0);
});

$('#defaultSearchField').on('change', function(elm){
    if(checkRegister($.trim($(this).val()))){
        $("#searchBar > div:nth-child(7) > div > a").trigger('click');
    } else {
        GM_notification ( {title: $(this).val(), text: 'Cek kembali nomor register'} );
    }
});