Reddit - Auto-redeem awards

Auto-redeem free awards

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit - Auto-redeem awards
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Auto-redeem free awards
// @author       thedrunkendev
// @match        https://www.reddit.com/**
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// ==/UserScript==
/* global waitForKeyElements */

(async function() {
    'use strict';

    await waitFor(() => document.querySelector("#COIN_PURCHASE_DROPDOWN_ID")?.innerText.includes("Free"));
    document.querySelector("#COIN_PURCHASE_DROPDOWN_ID")?.click();

    await waitFor(() => document.querySelector(`[role="menu"] [role="button"]`));
    document.querySelectorAll(`[role="menu"] [role="button"]`).forEach(a => a.innerText === "Claim" ? a.click() : null);

    await waitFor(() => document.querySelector(`[role="dialog"] div > svg`));
    document.querySelector(`[role="dialog"] div > svg`)?.parentElement.click();
})();

function waitFor(fn) {
    let remainingAttempts = 10;
    const poll = (resolve) => {
        remainingAttempts--;
        if (fn()) resolve();
        else if (remainingAttempts===0) {console.log("Max attempts reached"); return;}
        else setTimeout(_ => poll(resolve), 500);
    }

    return new Promise(poll);
}