Hill / Mailbox_extension_Niedersachsen

// ==UserScript==
// @name         Mailbox_extension_Niedersachsen
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Hill
// @include      https://www.grundbuch.niedersachsen.de/*
// @include      https://dataroom.gsk.de/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @run-at document-end
// @license      LGPL-2.0-only
// @connect *
// @updateUrl    https://openuserjs.org/meta/Hill/Mailbox_extension_Niedersachsen.meta.js
// ==/UserScript==


(function($) {
debugger;
    'use strict';
    var config = {
        processingSystems:{
            "Niedersachsen":'https://www.grundbuch.niedersachsen.de',
            "Bayern":'https://grundbuch.justizregister.bayern.de',
            "Berlin":'https://grundbuch.berlin.de',
            "Bremen":'https://grundbuch.bremen.de',
            "Rheinland-Pfalz":'https://solumweb.service24.rlp.de/'
        },
        credentials:{
            "www.grundbuch.niedersachsen.de":{
                login:"xnnw1165",
                password:"Luzi2021",
                reviser:"n1165005",
                revNumber:"No2SA0005-21",
                court: "Geestland"
            },

        },
        landMap:{
            'dummyland':['altName1','altName2','altName3']
        },
        districtMap:{
            'district':['altName1','altName2','altName3']
        }
    };
    $(document).ready(function(){

        if(location.hostname=='www.grundbuch.niedersachsen.de'){
            var conf = config.credentials[window.location.hostname];
            if(!window.opener)
            {
                return;
            }
            if($("form[name='loginform']").length>0)
            {
                $(window).on('message',function(e)
                             {
                    var message = e.originalEvent;
                    //login form
                    var form = $("*[name='loginform']");
                    $("input[name='username']",form).val(conf.login);
                    $("input[name='password']",form).val(conf.password);
                    $("input[name='bearbeiter']",form).val(conf.reviser);
                    form.submit();
                });
                window.opener.postMessage('grundbuch.domloaded','*');



            }
            else if($("form[name='AnmeldungErfolgreichForm']").length>0)
            {

                $(window).on('message',function(e,uri)
                             {
                    var message = e.originalEvent;
                    var lOption = $("select[name='land'] option[value^='"+message.data+"']")
                    if(lOption.length==0 && config.districtMap[message.data])
                    {
                        for(var d in config.districtMap[message.data])
                        {
                             lOption = $("select[name='bezirk'] option[value^='"+d+"']",form);
                             if(lOption.length>0)
                                break;
                        }
                    }
                    if(lOption.length==0)

                    {
                        alert('Land '+message.data+' not found');
                        window.close();
                    }
                    lOption.attr("selected","selected");
                    $("select[name='land']").trigger('change');
                    $("form[name='AnmeldungErfolgreichForm'] input[name='pressedButton'][type='submit']" ).click();
                });
                window.opener.postMessage('grundbuch.landRequired','*');
            }
            else if($("form[name='FlurstucksForm']").length>0)
            {
                debugger;
                var form=$("form[name='FlurstucksForm']");
                $(window).on('message',function(e)
                             {
                    var message = e.originalEvent;
                    message=message.data;
                    if(!message.birthDate)
                        return;
                    var name = message.name.split(' ');
                    var dOption = $("select[name='bezirk'] option[value^='"+message.GBDistrict+"']",form);
                    if(dOption.length==0 && config.districtMap[message.GBDistrict])
                    {
                        for(var d in config.districtMap[message.GBDistrict])
                        {
                             dOption = $("select[name='bezirk'] option[value^='"+d+"']",form);
                             if(dOption.length>0)
                                break;
                        }
                    }
                    if(dOption.length==0)

                    {
                        alert('District '+message.GBDistrict+' not found');
                        window.close();
                    }
                    dOption.attr("selected","selected");
                    //$("select[name='gemarkung'] option[value^='"+message.district+"']",form).attr("selected","selected");
                    //$("input[name='name']",form).val(name[0]);
                    //$("input[name='vorname']",form).val(name[1]);
                    $("input[name='geburtsDatum']",form).val(message.birthDate);
                    $("input[name='flur']",form).val(message.floor);
                    var parcelSplitted = message.parcel.split('/');
                    $("input[name='flurstucksZahler']",form).val(parcelSplitted[0]);
                    $("input[name='flurstucksNenner']",form).val(parcelSplitted[1]);
                    $("input[name='aktenz']",form).val(conf.revNumber);
                    $("input[name='pressedButton'][type='submit']",form ).click();
                });
                window.opener.postMessage('grundbuch.dataRequired',"*");
            }
            else if($("form[name='AktualitatsnachweisListeForm']").length>0)
            {
                var docLink = $("form[name='AktualitatsnachweisListeForm'] table tr:eq(1) td:eq(0) a").attr('href');
                window.opener.postMessage({link:new URL(docLink,location).href},"*");
            }
            else if($("form[name='TrefferFlurstucksForm']").length>0)
            {
                $("form[name='TrefferFlurstucksForm'] [name='pressedButton']:eq(1)").click();
            }
            else
            {
                debugger;
                $("table.left-panel td.leftPanel a")[2].click();
            }
        }
        if(location.hostname=='dataroom.gsk.de'){
            
            const callback = function(mutationsList, observer) {
                // Use traditional 'for loops' for IE 11
                for(const mutation of mutationsList) {
                    if (mutation.type === 'childList') {

                        if(mutation.addedNodes.length>0 && $('.processButton',mutation.addedNodes).length==0 && $("td:contains('Bundesland')",mutation.addedNodes[0]).length>0)
                        {
                            var system = $("td:contains('Bundesland')",mutation.addedNodes[0]).next().text();
                            debugger;
                            if(!config.processingSystems[system])
                                return;
                            if( $(".ms-bgc-tlr.ms-bgc-tlra",mutation.addedNodes[0]).length>0){
                                var buttonContainer = $(".ms-bgc-tlr.ms-bgc-tlra");
                                var processButton = $("<button></button>").addClass("_rp_s1 ms-font-m ms-fwt-r o365button processButton").text("Process").hover(function(){
                                    $(this).addClass('ms-fcl-nd ms-bgc-tl');},
                                                                                                                                                  function(){
                                    $(this).removeClass('ms-fcl-nd ms-bgc-tl');
                                }).click(function(){
                                    //parse table
                                    var table = $("td:contains('Bundesland')",mutation.addedNodes[0]).parents('table');
                                    var court =table[0].rows[11].cells[1].innerHTML;
                                    var district = table[0].rows[8].cells[1].innerHTML.replace(/[\d]{1,}\s?/,'').split('/');
                                    var data = {
                                        birthDate:table[0].rows[4].cells[1].innerHTML,
                                        name:table[0].rows[3].cells[1].innerHTML,
                                        floor:table[0].rows[9].cells[1].innerHTML,
                                        parcel:table[0].rows[10].cells[1].innerHTML,
                                        district:district.length>1?district[1]:'',
                                        GBDistrict:district[0]
                                    }
                                    var loaderCount = 0;
                                    var swindow =window.open(config.processingSystems[system]);

                                    $(window).on('message',function(e){
                                        var message = e.originalEvent;
                                        if(message.data=='grundbuch.domloaded')
                                        {
                                            swindow.postMessage("processLogin","*");
                                        }
                                        else if(message.data=='grundbuch.landRequired')
                                        {
                                            swindow.postMessage(court,"*");
                                        }
                                        else if(message.data=='grundbuch.dataRequired')
                                        {
                                            swindow.postMessage(data,"*");
                                        }
                                        else if(message.data.link)
                                        {
                                            swindow.close();
                                            $('tbody',table).append(
                                                $('<tr></tr>').append(
                                                    $('<td></td>').text('Link to document')
                                                ).append(
                                                    $('<td></td>').append(
                                                        $('<a></a>').attr('href',message.data.link).text('Download document')
                                                    )
                                                )
                                            );
                                            processButton.remove();
                                        }
                                    });
                                    return false;
                                });
                                buttonContainer.prepend(processButton);
                            }
                        }
                    }
                }
            };
            const observer = new MutationObserver(callback);

            // Start observing the target node for configured mutations
            observer.observe($("div[role='document'][regionbehavior='1']")[0],  { attributes: false, childList: true, subtree: true });

        }
    });



})(jQuery);