Auto load in kleinanzeigen

7.12.2022, 18:58:27

当前为 2023-09-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Auto load in kleinanzeigen
// @namespace   Violentmonkey Scripts
// @match       https://www.kleinanzeigen.de/*
// @grant       none
// @version     1.1
// @author      Janusk22
// @description 7.12.2022, 18:58:27
// @license MIT
// ==/UserScript==


function auto_load() {
    var currentURL = window.location.href;
    var isLoading = false;
    var page1Results = document.querySelector("#srchrslt-adtable");
    //increment the page for the next results
    function incrementPageNumber(url) {
        // Define a regular expression to match "seite:X" where X is a number
        var regex = /seite:(\d+)/;

        // Check if the URL contains the pattern
        if (!regex.test(url)) {
            // If the pattern is not found, add "seite:1/" to the beginning of the URL
            url = url.replace(/\/([^/]+)\/?$/, '/seite:1/$1');
        } else {
            url = url.replace(regex, function(match, pageNumber) {
                // Parse the matched pageNumber as an integer, increment it by 1, and convert it back to a string
                var incrementedPage = (parseInt(pageNumber, 10) + 1).toString();
                // Replace the matched part in the URL with the incremented value
                return "seite:" + incrementedPage;
            });
        }
        return url;
    }

    function isAtBottomOfPage() {
        var scrollPosition = window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
        var pageHeight = document.documentElement.scrollHeight;
        var windowHeight = window.innerHeight || document.documentElement.clientHeight;
        return scrollPosition + windowHeight >= pageHeight;
    }

    // Make an AJAX request to get the results for page 2
    function change(){
        if (isLoading) {
            return;
        }

        // Mark loading as in progress
        isLoading = true;
        var xhr = new XMLHttpRequest();
        currentURL = incrementPageNumber(currentURL)
        xhr.open("GET", currentURL);
        xhr.onload = function() {
            if (xhr.status === 200) {
                // Parse the HTML response
                var parser = new DOMParser();
                var doc = parser.parseFromString(xhr.responseText, "text/html");

                // Get the results for page 2
                var page2ResultsFromResponse = doc.querySelector("#srchrslt-adtable");

                // Copy the results for page 2 to the new element we created earlier
                page1Results.innerHTML = page1Results.innerHTML + page2ResultsFromResponse.innerHTML;
                isLoading = false;
            }
        };
        xhr.send();}
    window.addEventListener('scroll',function (){
        if (isAtBottomOfPage()){change()}
    })
};

auto_load();