RES filter button

Replaces "Join Subreddit" button on r/all/ with a button that filters the subreddit using Reddit Enhancement Suite (which needs to be installed).

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         RES filter button
// @version      0.2
// @description  Replaces "Join Subreddit" button on r/all/ with a button that filters the subreddit using Reddit Enhancement Suite (which needs to be installed).
// @author       cosmik_debree
// @include       http://reddit.com/*
// @include       https://reddit.com/*
// @include       http://*.reddit.com/*
// @include       https://*.reddit.com/*
// @namespace https://greasyfork.org/users/830718
// ==/UserScript==

(function() {
    'use strict';
    var cusid_ele = document.getElementsByClassName("subreddit-subscribe");
    var elems = [].slice.call(cusid_ele);
    // replaces all of the "Join subreddit" buttons with the filter subreddit button
    for (var i = 0; i < elems.length; ++i) { (function(i) {
        elems[i].removeAttribute("class");
        elems[i].removeAttribute("data-success-title");
        elems[i].title = elems[i].title.replace("Join", "Filter");
        elems[i].innerHTML = "➖";
        elems[i].onclick = function(){
            filter(elems[i].title.replace("Filter r/", ''));
        }})(i);
                                           }
}
)();
// somewhat janky, but opens the RES cmd line and pastes the cmd to filter the sub. if RES has an API to filter subs, this would be where it would be called.
async function filter(sub) {
    document.getElementById("RESSettingsButton").click();
    await new Promise(r => setTimeout(r, 200));
    document.getElementsByClassName("RESMenuItemButton res-icon")[1].click();
    var cmdLine = document.getElementById("keyCommandInput");
    cmdLine.value = "f " + sub;
    const ke = new KeyboardEvent('keydown', {
        bubbles: true, cancelable: true, key: "Enter"
    });
    cmdLine.dispatchEvent(ke);
}