洛谷屏蔽灌水区讨论

开了学术还是忍不住查看灌水区的讨论怎么办?没关系!这款插件可以帮到你!(与洛谷官方学术模式功能一起使用效果更佳)

目前為 2020-09-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name         洛谷屏蔽灌水区讨论
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  开了学术还是忍不住查看灌水区的讨论怎么办?没关系!这款插件可以帮到你!(与洛谷官方学术模式功能一起使用效果更佳)
// @author       rui_er
// @match        *://*.luogu.com.cn/*
// @match        *://*.luogu.org/*
// @grant        none
// ==/UserScript==

(function() {
  $('document').ready(function(){setTimeout(function() {
    'use strict';
    var block = localStorage.doBlock;
    if(block == undefined || block == "undefined") {
        block = 0;
    }
    localStorage.doBlock = block;
    if(location.pathname == "/") {
        console.log("matched 2!");
        $sidebar = $('#app-old .lg-index-content .lg-right.am-u-lg-3');
        $firstele = $($sidebar.children()[0]);
        $finder = $(`
          <div class="lg-article" id="block-water">
            <h2>屏蔽灌水区讨论</h2>
            <script>
function do_change_status() {
    var block = parseInt(localStorage.doBlock);
    if(block != 0 && block != 1) {
        block = 0;
    }
    block = 1 - block;
    localStorage.doBlock = block;
    location.href="/";
}
var block = parseInt(localStorage.doBlock);
var html = "<p ";
if(block == 1) {
    html += "style='color:red;'>屏蔽</p>";
}
else {
    html += "style='color:green;'>允许</p>";
}
document.getElementById("status-show").innerHTML = html;
            </script>
            <button class="am-btn am-btn-sm am-btn-primary" id="change-status-button" onclick="do_change_status()" style="margin-top:16px">更改状态</button>
            <p>当前状态:</p>
            <span><div id="status-show"></div></span>
          </div>
        `);
        $finder.insertAfter($firstele);
    }
    if(location.pathname.match(/\/discuss\/show\/\d/)) {
        console.log("matched 1!");
        if(parseInt(localStorage.doBlock) == 1) {
            $.get("/api/discuss/detail/"+location.pathname[14]+location.pathname[15]+location.pathname[16]+location.pathname[17]+location.pathname[18]+location.pathname[19],
                function(arr) {
                    var data = arr;
                    console.log("data: "+data);
                    if(data['status'] != 200) {
                        show_alert("好像哪里有点问题", data['data']);
                    }
                    else {
                        if(data['data']['Forum']['ForumID'] == 4) {
                            show_alert("好像哪里有点问题", "您正在浏览灌水区讨论");
                            location.href="/";
                        }
                    }
                }
            );
        }
    }
  },500)});
})();