Greasy Fork

Greasy Fork is available in English.

消息推送,up评论优化 —— AcFun

try to take over the world!

当前为 2019-08-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         消息推送,up评论优化 —— AcFun
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  try to take over the world!
// @author       You
// @match        https://*.acfun.cn/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    let zxs_namespace = {
        originalXHR: window.XMLHttpRequest,
        myXHR: function() {
            const xhr = new zxs_namespace.originalXHR;
            for (let attr in xhr) {
                if (attr === 'onreadystatechange') {
                    xhr.onreadystatechange = (...args) => {
                        if (this.readyState == 4) {
                            // 请求成功 post同源message事件
                            window.postMessage(this.responseURL,'/');
                        }
                        this.onreadystatechange && this.onreadystatechange.apply(this, args);
                    }
                    continue;
                }
                if (typeof xhr[attr] === 'function') {
                    this[attr] = xhr[attr].bind(xhr);
                } else {
                    Object.defineProperty(this, attr, {
                        get: () => xhr[attr],
                        set: (val) => xhr[attr] = val,
                        enumerable: true
                    });
                }
            }
        },
        ready: function(fn) {
            if(document.addEventListener) {
                document.addEventListener('DOMContentLoaded', function() {
                    fn();
                }, false);
            }else{
                //兼容?不存在的
            }
        }
    }
    window.XMLHttpRequest = zxs_namespace.myXHR;
    zxs_namespace.ready(function(){
        function awsl(){
            /*******************************消息推送************************************/
            var awslFlag = true;
            $('.guide-user').hover(
                function(){
                    const msgcount = $('#user-message-con').attr("data-count")
                    if(awslFlag && msgcount>0){
                        awslFlag = false
                        $.get("https://www.acfun.cn/rest/pc-direct/feed/followFeed?isGroup=0&gid=-1&count="+msgcount+"&pcursor=1",function(data,status){
                            if(status != 'success'){
                                awslFlag = true;
                                console.log('没救了...'+status)
                                return;
                            }
                            if(data.feedList.length == 0){
                                awslFlag = true;
                                console.log('没救了...')
                                console.log(data);
                                return;
                            }
                            for(let i=msgcount-1; i>=0;i--){
                                $('#user-message-con').prepend("<li class='awsl'><a style='color: #08c;' target='_blank' href='"+data.feedList[i].url+"'>"+data.feedList[i].title+"</a></li>")
                            }
                            $('.user-message-count').hide();
                        });
                    }
                },function(){});
            /*************************昵称标记********************************/
            let upname = $('a.upname').html();
            if(typeof upname == 'undefined'){
                upname = $('a.name-wrap').html();
                if(typeof upname == 'undefined'){
                    console.log('no up');
                    return;
                }
            }
            //清除icon再遍历判断添加
            function setUpIcon(){
                $('.mp-up-icon').remove();
                $('a.name').each(function(index,element){
                    if(element.innerText == upname /*|| element.innerText == `@${upname}`*/){
                        $(this).prepend("<span class='mp-up-icon' style='border: 1px solid #2596d2;color: #2596d2;padding: 0px 2px;text-align: center;line-height: normal;margin-right: 3px;border-radius: 3px;font-weight: bold;'>up</span>")
                    }
                });
            }
            //监听xmlhttprequest请求
            window.addEventListener("message", function(event){
                if(event.data.search(/comment\/list/) != -1){
                    setUpIcon();
                }else if(event.data.search(/comment\/sublist/) != -1){
                    setUpIcon();
                }
            },false);
            //等待初次评论加载完成后执行setUpIcon
            const upnameInterval= setInterval(function(){
                let len = $('a.name').length;
                if(len > 0){
                    clearInterval(upnameInterval);
                    setUpIcon();
                }
            },1000);
        }
         /**********************************粗糙的jquery补完计划******************************************/
        if (typeof jQuery == 'undefined'){
            const awslScriptElement=document.createElement('script');
            awslScriptElement.setAttribute("type","text/javascript");
            awslScriptElement.setAttribute("src", 'https://libs.baidu.com/jquery/2.0.0/jquery.min.js');
            document.getElementsByTagName("head")[0].appendChild(awslScriptElement);
            const awslInterval= setInterval(function(){
                if(typeof jQuery == 'function'){
                    clearInterval(awslInterval);
                    awsl();
                }
            },888);
        }else{
            awsl();
        }
    });
})();