Reddit Auto Award Posts

Press the "Auto-Award" button to give every post that got loaded a "free" award

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

Vous devrez installer une extension telle que Tampermonkey 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         Reddit Auto Award Posts
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Press the "Auto-Award" button to give every post that got loaded a "free" award
// @match        https://www.reddit.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    const log = (...args) => console.log('[AwardButton]', ...args);

    function walkDOM(root, callback) {
        const iterator = document.createNodeIterator(
            root,
            NodeFilter.SHOW_ELEMENT
        );

        let node;
        while ((node = iterator.nextNode())) {
            callback(node);
            if (node.shadowRoot) {
                walkDOM(node.shadowRoot, callback);
            }
        }
    }

    function isShareButton(el) {
        if (!el || el.tagName !== 'BUTTON') return false;
        if (el.getAttribute('aria-label') === 'Share') return true;
        if (el.textContent?.trim() === 'Share') return true;
        if (el.querySelector?.('svg[icon-name="share"]')) return true;
        return false;
    }

    function createAwardButton() {
        const btn = document.createElement('button');
        btn.type = 'button';
        btn.dataset.tmAwardBtn = 'true';
        btn.className = 'button border-md button-secondary';

        btn.style.marginRight = '10px';
        btn.style.padding = '0 10px';
        btn.style.display = 'inline-flex';
        btn.style.alignItems = 'center';
        btn.style.justifyContent = 'center';
        btn.style.height = 'var(--size-button-sm-h)';

        const content = document.createElement('span');
        content.style.display = 'inline-flex';
        content.style.alignItems = 'center';
        content.style.gap = '2px';
        content.style.lineHeight = '1';

        const text = document.createElement('span');
        text.textContent = 'Auto-';
        text.style.fontSize = '13px';
        text.style.lineHeight = '1';

        const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        svg.setAttribute('viewBox', '0 0 20 20');
        svg.setAttribute('width', '16');
        svg.setAttribute('height', '18');
        svg.setAttribute('fill', 'currentColor');
        svg.style.display = 'inline-block';
        svg.style.transform = 'translateY(1px)';
        svg.innerHTML = `
          <path d="M18.75 14.536l-2.414-3.581A6.947 6.947 0 0017 8c0-3.86-3.14-7-6.999-7-3.859 0-6.999 3.14-6.999 7 0 1.057.242 2.056.664 2.955l-2.414 3.581c-.289.428-.33.962-.109 1.429.22.467.658.776 1.173.826l1.575.151.758 1.494a1.435 1.435 0 001.297.795c.482 0 .926-.234 1.198-.639l2.437-3.612c.14.008.28.021.423.021.143 0 .282-.013.423-.021l2.437 3.612c.272.405.716.639 1.198.639.031 0 .062 0 .094-.003a1.435 1.435 0 001.203-.791l.758-1.495 1.576-.151c.514-.05.952-.358 1.172-.826a1.434 1.434 0 00-.109-1.429h-.006zM10 2.8A5.205 5.205 0 0115.2 8c0 2.867-2.333 5.2-5.2 5.2A5.205 5.205 0 014.801 8c0-2.867 2.332-5.2 5.2-5.2zM5.982 17.09l-.937-1.846-1.974-.189 1.66-2.462a7.02 7.02 0 002.936 1.999L5.982 17.09zm10.947-2.035l-1.974.189-.937 1.846-1.685-2.499a7.013 7.013 0 002.936-1.999l1.66 2.462v.001z"></path>
        `;

        content.append(text, svg);
        btn.appendChild(content);

        // WICHTIG: Klick abfangen, damit Reddit ihn nicht blockiert
        btn.addEventListener('click', (e) => {
            e.stopPropagation();
            e.preventDefault();
            runAwardFlow();
        });

        return btn;
    }

    function isCommentsPage() {
        return location.pathname.includes('/comments/');
    }

    function insertButtons() {
        if (isCommentsPage()) return;

        let count = 0;

        walkDOM(document, el => {
            if (!isShareButton(el)) return;

            const parent = el.parentElement;
            if (!parent) return;
            if (parent.querySelector('[data-tm-award-btn]')) return;

            parent.insertBefore(createAwardButton(), el);
            count++;
        });

        if (count) log('Buttons eingefügt:', count);
    }

    const observer = new MutationObserver(insertButtons);
    observer.observe(document.body, { childList: true, subtree: true });

    insertButtons();

    // ============================
    // DEIN ORIGINAL CODE (1:1)
    // ============================

    async function runAwardFlow() {
        const token = document.cookie.match(/csrf_token=([^;]+)/)?.[1];
        if (!token) {
            alert('Token error');
            return;
        }

        const awards = [
            { name: 'Heartwarming', id: 'award_free_heartwarming', img: 'https://i.redd.it/snoovatar/snoo_assets/marketing/Heartwarming_40.png' },
            { name: 'Popcorn', id: 'award_free_popcorn_2', img: 'https://i.redd.it/snoovatar/snoo_assets/marketing/Popcorn_40.png' },
            { name: 'Bravo', id: 'award_free_bravo', img: 'https://i.redd.it/snoovatar/snoo_assets/marketing/bravo_40.png' },
            { name: 'Regret', id: 'award_free_regret_2', img: 'https://i.redd.it/snoovatar/snoo_assets/marketing/regret_40.png' },
            { name: 'Mindblown', id: 'award_free_mindblown', img: 'https://i.redd.it/snoovatar/snoo_assets/marketing/mindblown_40.png' }
        ];

        let selectedAwards = [];

        const showFinishOverlay = (awarded, total) => {
            const f = document.createElement('div');
            Object.assign(f.style, { position: 'fixed', top: 0, left: 0, width: '100%', height: '100%', background: '#000', zIndex: 999999, display: 'flex', alignItems: 'center', justifyContent: 'center', fontFamily: 'system-ui', opacity: 0, transition: 'opacity 0.3s' });
            document.body.appendChild(f);
            setTimeout(() => f.style.opacity = 1, 10);

            const fb = document.createElement('div');
            Object.assign(fb.style, { background: '#000', color: '#fff', padding: '24px', borderRadius: '20px', width: '340px', maxWidth: '90%', border: '1px solid #343536', boxShadow: '0 12px 36px rgba(0,0,0,0.7)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '12px', transform: 'translateY(20px)', opacity: 0, transition: 'all 0.4s ease' });
            f.appendChild(fb);
            setTimeout(() => { fb.style.transform = 'translateY(0)'; fb.style.opacity = 1 }, 20);

            const st = document.createElement('p');
            st.textContent = `Posts awarded ${awarded}/${total}`;
            Object.assign(st.style, { fontSize: '18px', margin: 0, textAlign: 'center' });
            fb.appendChild(st);

            const bc = document.createElement('div');
            Object.assign(bc.style, { display: 'flex', gap: '12px' });

            const cb = document.createElement('button');
            cb.textContent = 'Close';
            Object.assign(cb.style, { padding: '8px 16px', borderRadius: '16px', border: 'none', cursor: 'pointer', fontWeight: '600', background: '#ff4500', color: '#fff', fontSize: '14px', boxShadow: '0 3px 8px rgba(0,0,0,0.5)', display: 'flex', alignItems: 'center', justifyContent: 'center', textAlign: 'center' });
            cb.onmouseover = () => { cb.style.transform = 'translateY(-2px)'; cb.style.boxShadow = '0 4px 12px rgba(0,0,0,0.6)' };
            cb.onmouseout = () => { cb.style.transform = 'translateY(0)'; cb.style.boxShadow = '0 3px 8px rgba(0,0,0,0.5)' };
            cb.onclick = () => document.body.removeChild(f);
            bc.appendChild(cb);

            const tb = document.createElement('button');
            tb.textContent = 'Try again';
            Object.assign(tb.style, { padding: '8px 16px', borderRadius: '16px', border: 'none', cursor: 'pointer', fontWeight: '600', background: '#0079d3', color: '#fff', fontSize: '14px', boxShadow: '0 3px 8px rgba(0,0,0,0.5)', display: 'flex', alignItems: 'center', justifyContent: 'center', textAlign: 'center' });
            tb.onmouseover = () => { tb.style.transform = 'translateY(-2px)'; tb.style.boxShadow = '0 4px 12px rgba(0,0,0,0.6)' };
            tb.onmouseout = () => { tb.style.transform = 'translateY(0)'; tb.style.boxShadow = '0 3px 8px rgba(0,0,0,0.5)' };
            tb.onclick = () => { document.body.removeChild(f); awardPosts(selectedAwards) };
            bc.appendChild(tb);

            fb.appendChild(bc);

            const info = document.createElement('p');
            info.textContent = "Due to Reddit limits, not all posts may be awarded at once. If this happens, press 'Try again'.";
            Object.assign(info.style, { fontSize: '10px', color: '#818384', margin: 0, textAlign: 'center' });
            fb.appendChild(info);

            const ft = document.createElement('p');
            ft.innerHTML = 'This script is presented by <a href="https://www.reddit.com/r/awards/" target="_blank" style="color:#ff4500;text-decoration:none">r/Awards</a>';
            Object.assign(ft.style, { fontSize: '12px', color: '#818384', marginTop: '4px', textAlign: 'center' });
            fb.appendChild(ft);
        };

        const awardPosts = async (sel) => {
            const p = document.createElement('div');
            Object.assign(p.style, { position: 'fixed', top: 0, left: 0, width: '100%', height: '100%', background: '#000', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', zIndex: 999999 });

            const contentBox = document.createElement('div');
            Object.assign(contentBox.style, { position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', width: '600px', maxWidth: '90%', height: '560px', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-start', gap: '10px', pointerEvents: 'none' });

            const authorLine = document.createElement('div');
            authorLine.textContent = '';
            Object.assign(authorLine.style, { fontSize: '14px', color: '#c7c7c7', marginTop: '10px', textAlign: 'center', width: '100%', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' });
            contentBox.appendChild(authorLine);

            const titleBox = document.createElement('div');
            Object.assign(titleBox.style, { height: '60px', width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', textAlign: 'center', padding: '0 10px', boxSizing: 'border-box' });

            const ftit = document.createElement('div');
            ftit.textContent = '';
            Object.assign(ftit.style, { fontSize: '22px', fontWeight: '700', textShadow: '0 2px 8px rgba(0,0,0,0.6)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', width: '100%' });
            titleBox.appendChild(ftit);
            contentBox.appendChild(titleBox);

            const fbodyWrap = document.createElement('div');
            Object.assign(fbodyWrap.style, { width: '100%', height: '500px', display: 'flex', alignItems: 'center', justifyContent: 'center', overflow: 'hidden', background: 'rgba(0,0,0,0.2)', borderRadius: '12px' });

            const fbody = document.createElement('div');
            Object.assign(fbody.style, { fontSize: '16px', lineHeight: '1.4', width: '100%', padding: '10px', boxSizing: 'border-box', textAlign: 'center', wordBreak: 'break-word' });
            fbodyWrap.appendChild(fbody);
            contentBox.appendChild(fbodyWrap);

            p.appendChild(contentBox);

            const statusBox = document.createElement('div');
            Object.assign(statusBox.style, { position: 'fixed', bottom: '8%', left: '50%', transform: 'translateX(-50%)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '8px', zIndex: 1000001 });

            const loaderImg = document.createElement('img');
            loaderImg.src = 'https://b.thumbs.redditmedia.com/fzCkqMKRwh_XnI9oYFsnUGaKOCq8gSqfRCwYYsfq6mg.png';
            Object.assign(loaderImg.style, { width: '144px', height: '144px' });
            statusBox.appendChild(loaderImg);

            const wt = document.createElement('div');
            wt.textContent = 'Please wait… awarding posts';
            Object.assign(wt.style, { fontSize: '18px', textAlign: 'center' });
            statusBox.appendChild(wt);

            const prog = document.createElement('div');
            prog.textContent = '0/0';
            Object.assign(prog.style, { fontSize: '12px', color: '#c7c7c7', marginTop: '2px' });
            statusBox.appendChild(prog);

            p.appendChild(statusBox);
            document.body.appendChild(p);

            const ids = [...new Set([...document.querySelectorAll('a[href*="/comments/"]')].map(a => a.href.match(/\/comments\/([^\/?#]+)/)?.[1]).filter(Boolean))];

            const postMap = new Map();
            for (const id of ids) {
                const el = document.querySelector(`a[href*="/comments/${id}"]`);
                const container = el?.closest('article') || el?.closest('div');
                if (container) postMap.set(id, container);
            }

            let awarded = 0;
            const total = ids.length;
            prog.textContent = `${awarded}/${total}`;

            for (const id of ids) {
                const thingId = 't3_' + id;
                const a = sel[Math.floor(Math.random() * sel.length)];
                const postContainer = postMap.get(id);

                const postData = await (async () => {
                    const titleDom = postContainer?.querySelector('h3')?.textContent?.trim();
                    const bodyDom = postContainer?.querySelector('p')?.textContent?.trim();
                    const authorDom = postContainer?.querySelector('a[href*="/user/"]')?.textContent?.trim();

                    try {
                        const res = await fetch(`https://www.reddit.com/comments/${id}.json?raw_json=1`);
                        const json = await res.json();
                        const post = json?.[0]?.data?.children?.[0]?.data;
                        let image = '';
                        if (post?.post_hint === 'image') image = post?.url_overridden_by_dest || post?.url || '';
                        else if (post?.preview?.images?.[0]?.source?.url) image = post.preview.images[0].source.url.replace(/&amp;/g, '');
                        return { title: post?.title || titleDom || '', body: post?.selftext || bodyDom || '', author: post?.author ? `u/${post.author}` : (authorDom || ''), subreddit: post?.subreddit_name_prefixed || '', image };
                    } catch {
                        return { title: titleDom || '', body: bodyDom || '', author: authorDom || '', subreddit: '', image: '' };
                    }
                })();

                authorLine.textContent = (postData.author || 'u/unknown') + (postData.subreddit ? ` • ${postData.subreddit}` : '');
                ftit.textContent = postData.title || 'Title not found';
                fbody.innerHTML = '';

                if (postData.image && !postData.body) {
                    const img = document.createElement('img');
                    img.src = postData.image;
                    Object.assign(img.style, { width: '300px', height: '500px', objectFit: 'cover', display: 'block' });
                    fbody.appendChild(img);
                } else {
                    fbody.textContent = postData.body || 'Body not found';
                }

                try {
                    const res = await fetch('https://www.reddit.com/svc/shreddit/graphql', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-Csrf-Token': token
                        },
                        body: JSON.stringify({
                            operation: 'CreateAwardOrder',
                            variables: { input: { nonce: crypto.randomUUID(), thingId, awardId: a, isAnonymous: false } },
                            csrf_token: token
                        })
                    });

                    const j = await res.json();
                    if (j.data?.createAwardOrder?.ok) awarded++;
                } catch (e) { }

                prog.textContent = `${awarded}/${total}`;
                await new Promise(r => setTimeout(r, 3000 + Math.random() * 2000));
            }

            document.body.removeChild(p);
            showFinishOverlay(awarded, total);
        };

        const o = document.createElement('div');
        Object.assign(o.style, { position: 'fixed', top: 0, left: 0, width: '100%', height: '100%', background: '#000', zIndex: 999999, display: 'flex', alignItems: 'center', justifyContent: 'center', fontFamily: 'system-ui', opacity: 0, transition: 'opacity 0.3s' });
        document.body.appendChild(o);
        setTimeout(() => o.style.opacity = 1, 10);

        const b = document.createElement('div');
        Object.assign(b.style, { background: '#000', color: '#fff', padding: '28px', borderRadius: '20px', width: '360px', maxWidth: '90%', border: '1px solid #343536', boxShadow: '0 12px 36px rgba(0,0,0,0.7)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '12px', transform: 'translateY(-20px)', transition: 'transform 0.3s', position: 'relative' });
        setTimeout(() => b.style.transform = 'translateY(0)', 20);

        const banner = document.createElement('img');
        banner.src = 'https://b.thumbs.redditmedia.com/RxCsr1nk28EcHbnS84sJhjNaOwyr3h1YgbRq4pzkgIU.png';
        Object.assign(banner.style, { position: 'absolute', top: 0, left: 0, width: '100%', height: 'auto', borderTopLeftRadius: '20px', borderTopRightRadius: '20px', objectFit: 'cover', boxShadow: '0 4px 12px rgba(0,0,0,0.5)' });
        b.appendChild(banner);

        const title = document.createElement('h3');
        title.textContent = 'Random Award Selection';
        Object.assign(title.style, { margin: '60px 0 0 0', fontSize: '20px', fontWeight: '700', zIndex: 1, position: 'relative' });
        b.appendChild(title);

        const desc = document.createElement('p');
        desc.textContent = 'Select the awards to be used in the randomizer.';
        Object.assign(desc.style, { color: '#818384', margin: '4px 0 16px 0', fontSize: '14px', textAlign: 'center', position: 'relative', zIndex: 1 });
        b.appendChild(desc);

        const list = document.createElement('div');
        list.style.display = 'flex';
        list.style.flexDirection = 'column';
        list.style.gap = '10px';

        awards.forEach(a => {
            const r = document.createElement('label');
            r.style.display = 'flex';
            r.style.alignItems = 'center';
            r.style.gap = '10px';
            r.style.cursor = 'pointer';
            r.style.padding = '6px';
            r.style.borderRadius = '12px';
            r.style.transition = 'background 0.2s';
            r.onmouseover = () => r.style.background = 'rgba(255,69,0,0.1)';
            r.onmouseout = () => r.style.background = 'transparent';

            const cb = document.createElement('input');
            cb.type = 'checkbox';
            cb.checked = true;
            cb.value = a.id;

            const img = document.createElement('img');
            img.src = a.img;
            img.style.width = '36px';
            img.style.height = '36px';
            img.style.borderRadius = '6px';

            r.appendChild(cb);
            r.appendChild(img);
            r.appendChild(document.createTextNode(a.name));
            list.appendChild(r);
        });

        b.appendChild(list);

        const start = document.createElement('button');
        start.textContent = 'Start';
        Object.assign(start.style, { marginTop: '12px', width: '100%', height: '44px', borderRadius: '22px', border: 'none', cursor: 'pointer', fontWeight: '700', background: 'linear-gradient(90deg,#ff4500,#ff7f50)', color: '#fff', fontSize: '16px', boxShadow: '0 4px 12px rgba(0,0,0,0.5)', transition: 'all 0.2s' });

        start.onmouseover = () => { start.style.transform = 'translateY(-2px)'; start.style.boxShadow = '0 6px 16px rgba(0,0,0,0.6)' };
        start.onmouseout = () => { start.style.transform = 'translateY(0)'; start.style.boxShadow = '0 4px 12px rgba(0,0,0,0.5)' };

        start.onclick = () => {
            selectedAwards = [...list.querySelectorAll('input:checked')].map(i => i.value);
            if (!selectedAwards.length) {
                alert('Please select at least one award.');
                return;
            }
            document.body.removeChild(o);
            awardPosts(selectedAwards);
        };

        b.appendChild(start);
        o.appendChild(b);
    }
})();