Reddit_Comments_Tab

Add a comments tab to the main nav bar.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit_Comments_Tab
// @name:en      Reddit_Comments_Tab
// @namespace    Reddit_Comments_Tab
// @version      0.3
// @description  Add a comments tab to the main nav bar.
// @author       https://github.com/kimpeek
// @include      *reddit.com*
// @grant        none
// ==/UserScript==

var current_url = document.URL;
var no_button = new RegExp("comments|inbox|user|overview|prefs|about|message");
var multireddit = new RegExp("^http(s)?:\/\/(www|old).reddit.com\/me\/m\/[A-Za-z0-9_]+");
var subreddit = new RegExp("^http(s)?:\/\/(www|old).reddit.com\/r\/[A-Za-z0-9_]+");

function main(){
    // Check if on a page to ignore
    if (no_button.test(current_url)) {
        return;
    }

    // Check if a multireddit --> extract multi --> create button with multi
    if (multireddit.test(current_url)) {
        const multi = current_url.match(multireddit);
        create_button(multi[0].concat("/comments/"));
        return;
    }

    // Check if a subreddit --> extract sub --> create button with sub
    else if (subreddit.test(current_url)) {
        const sub = current_url.match(subreddit);
        create_button(sub[0].concat("/comments/"));
        return;
    }

    // Return frontpage + comments
    else {
        create_button("https://www.reddit.com/comments/");
        return;
    }
}

function create_button(redirect_url){
    var menu = document.getElementsByClassName("tabmenu ")[0];
    var gilded = menu.getElementsByTagName('li')[5];
    var comments = document.createElement("li");
    var comment_anchor = document.createElement("a");

    menu.insertBefore(comments, gilded);
    comments.appendChild(comment_anchor);
    comment_anchor.innerHTML = "comments";
    comment_anchor.setAttribute('href', redirect_url);
}

main();