Camamba Forums Search Library

fetches forums, threads and posts

Version au 06/06/2022. Voir la dernière version.

Ce script ne doit pas être installé directement. C'est une librairie destinée à être incluse dans d'autres scripts avec la méta-directive // @require https://update.greasyfork.org/scripts/446112/1058380/Camamba%20Forums%20Search%20Library.js

Vous devrez installer une extension telle que Tampermonkey, Greasemonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Userscripts pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension de gestionnaire de script utilisateur pour installer ce script.

(J'ai déjà un gestionnaire de scripts utilisateur, laissez-moi l'installer !)

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

(J'ai déjà un gestionnaire de style utilisateur, laissez-moi l'installer!)

// ==UserScript==
// @name         Camamba Forums Search Library
// @namespace    hoehleg.userscripts.private
// @version      0.0.1
// @description  fetches forums, threads and posts
// @author       Gerrit Höhle
// @license MIT
//
// @require      https://greasyfork.org/scripts/405144-httprequest/code/HttpRequest.js?version=1058339
//
// @grant        GM_xmlhttpRequest
//
// ==/UserScript==

/* jslint esversion: 9 */

/**
 * @typedef ForumDef
 * @property {number} id
 * @property {string} lng
 * @property {string} title
 */

/** 
 * @typedef ThreadDef
 * @property {string} title
 * @property {number} id 
 * @property {number} forumId
 * @property {number} postCount
 */

/** 
 * @typedef PostDef
 * @property {number} forumId
 * @property {number} threadId
 * @property {number} page
 * @property {number} id 
 * @property {Date} postDate
 * @property {string} text 
 * @property {string} uname 
 * @property {number} uid 
 */

/**
 * @typedef ThreadIdentifier
 * @property {number} forumId
 * @property {number} threadId
 */

class Post {
    /**
     * @param {PostDef} param0 
     */
    constructor({ forumId, threadId, page, id, postDate, text, uname, uid }) {
        /** @type {number} */
        this.forumId = forumId;
        /** @type {number} */
        this.threadId = threadId;
        /** @type {number} */
        this.page = page;
        /** @type {number} */
        this.id = id;
        /** @type {Date} */
        this.postDate = postDate;
        /** @type {string} */
        this.text = text;
        /** @type {string} */
        this.uname = uname;
        /** @type {number} */
        this.uid = uid;
    }

    async delete() {
        return await HttpRequest.send({
            method: 'GET', url: "https://www.camamba.com/forum_view.php", params: {
                thread: this.threadId,
                forum: this.forumId,
                delete: this.id,
                page: this.page,
            }
        });
    }
}

class Thread {

    /**
     * @param {ThreadDef} param0 
     */
    constructor({ title, id, forumId, postCount }) {
        /** @type {string} */
        this.title = title;
        /** @type {number} */
        this.id = id;
        /** @type {number} */
        this.forumId = forumId;
        /** @type {number} */
        this.postCount = postCount;
    }

    /**
     * @returns {Promise<Array<Post>>}
     */
    async getPosts() {
        return Thread.getPosts({ threadId: this.id, forumId: this.forumId });
    }

    async delete() {
        for (const post of await this.getPosts()) {
            post.delete();
        }
    }

    /**
     * @param {ThreadIdentifier} param0 
     * @returns {Promise<Array<Post>>}
     */
    static async getPosts({ threadId, forumId }) {
        return (await HttpRequestHtml.send({
            url: "https://www.camamba.com/forum_view.php",
            params: {
                thread: threadId,
                forum: forumId,
            },
            pageNr: 1,
            pagesMaxCount: 1000,
            paramsConfiguratorForPageNr: (params, pageNr) => ({ ...params, page: pageNr - 1 }),
            hasNextPage: (res, _req) => res.html.querySelectorAll("td.psmallbox").length >= 30,
            resultTransformer: (res, req) => {

                return [...res.html.querySelectorAll("td.psmallbox")].map(td => {
                    let postId = null, text = "", postDate = null;

                    const div = td.querySelector("div");
                    if (div) {
                        const deleteLink = div.querySelector('a[href^="javascript:deletePost"]');

                        if (deleteLink) {
                            const postIdMatch = /(?<=javascript:deletePost)\d+(?=\(\))/.exec(deleteLink.href);
                            postId = postIdMatch === null ? null : Number.parseInt(postIdMatch[0]);
                        }

                        text = [...div.children].filter(el => el.tagName === 'P').map(el => el.innerText).join("").trim();
                    }

                    let uname = "", uid = null;

                    const tdLeft = td.previousElementSibling;
                    if (tdLeft && [...tdLeft.classList].includes("psmallbox2")) {
                        const linkOpenProfile = tdLeft.querySelector('a[href^="javascript:openProfile("]');
                        if (linkOpenProfile) {
                            const unameMatch = /(?<=javascript:openProfile\(['"])\w+(?=['"]\))/.exec(linkOpenProfile.href);
                            if (unameMatch) {
                                uname = unameMatch[0];
                            }
                        }

                        const imgUserpic = tdLeft.querySelector('img[src^="/userpics"]');
                        if (imgUserpic) {
                            const uidMatch = /(?<=userpics\/)\d+(?=\.s\.jpg)/.exec(imgUserpic.src);
                            if (uidMatch) {
                                uid = uidMatch[0];
                            }
                        }

                        const divDate = tdLeft.querySelector('div.smalltext');
                        if (divDate) {
                            const postDateMatch = /(\d{2}).(\d{2}).(\d{4})\s(\d{1,2}):(\d{2}):(\d{2})/.exec(divDate.innerText.trim());
                            if (postDateMatch) {
                                const day = postDateMatch[1];
                                const month = postDateMatch[2];
                                const year = postDateMatch[3];
                                const hour = postDateMatch[4];
                                const minute = postDateMatch[5];
                                const second = postDateMatch[6];
                                postDate = new Date(year, month - 1, day, hour, minute, second);
                            }
                        }
                    }

                    let page = req.params.page;
                    return { forumId, threadId, page, id: postId, postDate, text, uname, uid };
                }).filter(p => p.id && p.text).map(p => new Post(p));
            },
        })).flat();
    }
}

class Forum {
    /**
     * @param {ForumDef} param0 
     */
    constructor({ id, lng, title }) {
        /** @type {number} */
        this.id = id;
        /** @type {string} */
        this.lng = lng;
        /** @type {string} */
        this.title = title;
    }

    /**
     * @returns {Promise<Array<Thread>>}
     */
    async getThreads() {
        return await Forum.getThreads(this.id);
    }

    /**
     * @param {number} forumId 
     * @returns {Promise<Array<Thread>>}
     */
    static async getThreads(forumId) {
        return (await HttpRequestHtml.send({
            url: "https://www.camamba.com/forum_view.php",
            params: { forum: forumId },
            pageNr: 1,
            pagesMaxCount: 10000,
            paramsConfiguratorForPageNr: (params, pageNr) => ({ ...params, page: pageNr - 1 }),
            hasNextPage: (res, _req) => res.html.querySelectorAll('a[href^="?thread="]').length >= 10,
            resultTransformer: (res, req) => {
                return [...res.html.querySelectorAll('a[href^="?thread="]')].map(a => {

                    const matchThreadId = new RegExp(`(?<=\\?thread=)\\d+(?=&forum=${forumId})`).exec(a.href);
                    const threadId = matchThreadId === null ? null : Number.parseInt(matchThreadId[0]);

                    let postCount = (() => {

                        let postCountTD = a.parentElement;

                        while (postCountTD !== null && postCountTD.tagName !== 'TD') {
                            postCountTD = postCountTD.parentElement;
                        }

                        for (let i = 0; i < 2 && postCountTD !== null; i++) {
                            postCountTD = postCountTD.nextElementSibling;
                        }

                        if (postCountTD != null) {
                            let postCountMatch = /^\d+$/.exec(postCountTD.innerText.trim());
                            if (postCountMatch && postCountMatch.length) {
                                return Number.parseInt(postCountMatch[0]);
                            }
                        }

                        return 0;
                    })();

                    return { title: a.innerHTML, id: threadId, forumId, postCount };

                }).filter(t => t.id && t.forumId && t.postCount).map(t => new Thread(t));
            },
        })).flat();
    }
}

const Foren = (() => {

    /**
     * @type {Promise<Array<Forum>>}
     */
    const foren = HttpRequestHtml.send({
        url: 'https://www.camamba.com/forum.php',
        params: { mode: 'all' },
        resultTransformer: (resp, _req) => [...resp.html.querySelectorAll('a[href^="/forum_view"]')].map(a => {

            const idMatch = /(?<=forum=)\d{1,2}$/.exec(a.href);
            const id = idMatch === null ? null : Number.parseInt(idMatch[0]);

            const lngMatch = /(?<=^\[)\D\D(?=\])/g.exec(a.textContent);
            const lng = lngMatch === null ? null : lngMatch[0].toLowerCase();

            const titleMatch = /(?<=^\[\D\D\]).+$/.exec(a.textContent);
            const title = titleMatch === null ? null : titleMatch[0];

            return { id, lng, title };

        }).filter(forum => forum.id && forum.lng).map(f => new Forum(f)),
    });

    return {
        /**
        * @returns {Promise<Array<Forum>>}
        */
        getAll: async () => await foren,

        /**
         * @param {string} lng 
         * @returns {Promise<Array<Forum>>}
         */
        byLanguage: async (lng) => (await foren).filter(f => f.lng.toUpperCase() === lng.toUpperCase()),

        /**
         * @param {number} id 
         * @returns {Promise<Forum>}
         */
        byId: async (id) => (await foren).find(f => f.id == id),
    };
})();