Kleinanzeigen WASD Navigation Upgrade

Navigate Kleinanzeigen with arrow keys and click "Show More" button on main page, along with CSS modifications

// ==UserScript==
// @name         Kleinanzeigen WASD Navigation Upgrade
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Navigate Kleinanzeigen with arrow keys and click "Show More" button on main page, along with CSS modifications
// @author       moritz
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kleinanzeigen.de
// @match        https://www.kleinanzeigen.de/*
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


    // https://greasyfork.org/scripts/448672-ebay-kleinanzeigen-ad-blocker/code/Ebay-Kleinanzeigen%20Ad%20Blocker.user.js
    // j-liberty-wrapper are the two ads at the top,
    // ad-listitem without any other suffix are the ads mixed within the results
    var ads = document.querySelectorAll('[class="ad-listitem"],[class="j-liberty-wrapper "]')
    ads.forEach((ad) => {
        ad.style.display = "none"
    })


    document.addEventListener('keydown', function(e) {
        if (e.key === 'ArrowLeft' || e.key === 'a') {
            navigatePage('prev');
        } else if (e.key === 'ArrowRight' || e.key === 'd') {
            navigatePage('next');
        }
    });

    function navigatePage(direction) {
        var currentPage = getCurrentPageNumber();
        var nextPage;

        if (direction === 'prev' && currentPage > 1) {
            nextPage = currentPage - 1;
        } else if (direction === 'next') {
            nextPage = currentPage + 1;
        } else {
            return; // Do nothing for other keys
        }

        var currentURL = window.location.href;
        var newURL;

        if (currentPage === 1) {
            newURL = currentURL.replace(/(\/[^\/]+)(\/[^\/]+)$/, '$1/seite:' + nextPage + '$2');
        } else {
            newURL = currentURL.replace(/\/seite:\d+/, '/seite:' + nextPage);
        }

        window.location.href = newURL;
    }

    // Funktion für das Klicken auf den "Show More" Button auf der Hauptseite
    window.addEventListener('load', function() {
        var button = document.querySelector('.button-secondary.j-feed-show-more');

        if (button && window.location.href === 'https://www.kleinanzeigen.de/') {
            // Klicken Sie auf den Button mehrmals
            button.click();
            button.click();
            button.click();
            button.click();
            button.click();
            button.click();
        }
    });

// CSS-Anpassungen
var cssStyles = `
    #brws_banner-supersize { display: none !important; }
    #btf-billboard { display: none !important; }
    #home-billboard { display: none !important; }
    #home-gallery { display: none !important; }
    #srp-skyscraper-btf { display: none !important; }
    #store-gallery { display: none !important; }
    #viewad-sidebar-banner { display: none !important; }
    .ad-listitem.badge-topad.is-topad { display: none !important; }
    .site-base--left-banner { display: none !important; }
    .site-base--right-banner { display: none !important; }
    [id^="#home-"] { display: none !important; }
    [id^="srpb"] { display: none !important; }
    [id^="vip-"] { display: none !important; }
    #home-ads { display: none !important; }
    #my-msgbox-atf { display: none !important; }
    #my-watchlist-atf { display: none !important; }
    #my_ads-top-banner { display: none !important; }
    #site-content { padding-left: 30px; width: 2430px; }
    .l-splitpage-navigation { margin-right: 50px; width: 330px; }
    .l-splitpage-flex > .l-splitpage-content { width: 2200px; }
    .l-homepage-content-col { width: 2100px; float: left; }
`;

var styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.appendChild(document.createTextNode(cssStyles));
document.head.appendChild(styleElement);

    function getCurrentPageNumber() {
        var currentPageElement = document.querySelector('.pagination-current');
        if (currentPageElement) {
            return parseInt(currentPageElement.textContent);
        } else {
            // If there is no pagination element, assume it's page 1
            return 1;
        }
    }
})();