今年一定島 自動更新回文

汲汲營營大報社

Version vom 22.03.2020. Aktuellste Version

// ==UserScript==
// @namespace    https://greasyfork.org/zh-TW/scripts/39891
// @name         今年一定島 自動更新回文
// @description  汲汲營營大報社
// @author       稻米
// @version      2020.03.22.0020.build16299
// @grant        none

// @include      *://*.komica.org/00/*
// @include      *://*.komica.org/00/*
// @exclude      *://*.komica.org/00/src/*
// @exclude      *://*.komica.org/00/thumb/*
// @exclude      *.jpg
// @exclude      *.png
// @exclude      *.webm

// ==/UserScript==

//jquery
try{}
catch(err){}
finally{}
$(document).ready(function() {
    // 從sessionStorage移除之前存的所有資料
    //window.sessionStorage.clear();
    //console.log( window.sessionStorage );
    //window.localStorage.clear();
    //console.log( window.localStorage );

    //console.log( 'jquery ready' );
    //全域變數//global
    var time=new Date();
    var postnum=poi_findpostnum();//首篇編號
    window.gg190524=[];
    window.gg190524.原始標題=document.title;
    window.gg190524.標題編號=postnum;
    window.gg190524.新的文章數量=0;//新的文章數量
    if(window.localStorage){
        var aa=$("div.bar_reply:contains('回應模式')");
        if( aa.length ){
            poi(); //回應模式才執行
        }
    }else{
        //$(".thread").attr('poi',tmp);
    }
});

function var190106(){
    //
    $( document ).ajaxStart(function(){ //啟動ajax時 執行這段 // AJAX 请求开始
        //console.log( 'ajaxStart' );
        //window.gg190524=chk_gg();//檢查gg是否存在 //ajax的異步問題
    });
    $( document ).ajaxSend(function() { // AJAX 请求发送
        //console.log( "ajaxSend" );
    });

};//var190106

function poi_findpostnum(){
    if( window.location.href.match("\\?res=") ){
        //var url_p1 = new URL( window.location.href );
        //var url_p2 = new URLSearchParams( url_p1.search ).get('res');//首篇編號
        var selfres = $('.post.threadpost').attr('data-no'); //首篇編號
        return selfres;
    }
}


//



function poi(){
    //console.log( gg190524 );
    //console.log( window.sessionStorage );
    //console.log( window.gg190524 );

/*
    var tmp='';
    tmp = window.gg190524;
    tmp = JSON.stringify( tmp );
    tmp = encodeURIComponent( tmp );
    tmp = btoa(tmp);
    //存
    //$(".thread").attr('gg190524',tmp);
    //

*/
    animate01();

}//poi()



function chk_gg(){ //???
    //檢查
    if( window.gg190524.原始標題 ){
        //console.log( '有' );
    }else{
        console.log( '無資料' );
        //讀
        var tmp='';
        //讀取
        tmp = $(".thread").attr('poi');
        //解碼
		tmp = atob(tmp);
        tmp = decodeURIComponent(tmp);
        tmp = JSON.parse(tmp);

        //console.log( tmp );
        window.gg190524=tmp;
        console.log( gg190524 );
    }
    //
    return gg190524;
}//chk_gg(){


//建立動畫的網頁元素
function animate01(){
    if( $("#poi180324append").length > 0){
        //已存在=還原長度
        $("#poi180324inbox").css({
            "width":"600px",
        });
    }else{
        //不存在=建立元素
        var div = $("<div>").html("180324append").attr({
            'id':'poi180324append',
            'class':'class_poi180324',
        });//
        //$(".thread").append(div);
        $(".thread").find('hr').before(div);//before //after
        //
        $("#poi180324append").html('');
        $("#poi180324append").before('<br clear="both">');
        $("#poi180324append").append('<div id="poi180324inbox"></div>');
        $("#poi180324inbox").html('<poi>自動更新回文</poi><span style="float: right;"></span>');

        //外框
        $("#poi180324append").css({
            "border":'5px solid #ff0000',
            "background-color":"#ffff00",//yellow
            "display":"block",
            "width":"600px",
        });
        //進度條
        $("#poi180324inbox").css({
            "background-color":"#ff0000",
            "white-space":"nowrap",
            "display":"block",
            "width":"600px",
        });
        //height
    }//if
    //
    animate01b();
}

function animate01b(){
    //更新進度條狀態
    var f1=function(){
        var starttime=Date.now();
        var tmp='';
        var cc=0;
        var oneElement = document.querySelector('#poi180324inbox');
        var duration = 30; //動畫持續時間(秒)60
        var progress = 0;
        var timer=function(){
            //var tt=
            setTimeout(function(){
                cc++;
                //經過時間
                //window.performance.now() 網頁開啟的時間
                progress= ( (Date.now()-starttime) / 1000).toFixed(1); //Math.floor
                //document.title = progress; //Date.now();
                //console.log( cc, progress);

                tmp =600 - (progress / duration)*600; //600=寬度
                oneElement.style.width = tmp+'px';
                //
                if(progress > duration){
                    //clearTimeout(tt);
                    getapi();//跑完動畫 再抓一次api
                }else{
                    timer();
                }

            }, 1*1000/5);
        };
        timer();
    };
    f1();//setTimeout
    //
}



function getapi(){
    var selfres = $('.post.threadpost').attr('data-no'); //首篇編號
    var apiurl='./pixmicat.php?mode=module&load=mod_ajax&action=thread&html=true&op='+selfres; //綜合版原生api
    //var apiurl='http://homu.homu-api.com/res/'+gg190524.selfres; //第三方api
    var jqxhr=$.ajax({ //用ajax獲取網址內容
        url:apiurl,
        //timeout:30*1000, //不影響其他ajax //雞肋的功能
        //global:false, //不觸發global事件
    }, function(x,y,z){
        console.log('ajax');
    });

    //console.log('x',y,z);
    //jQuery has deprecated synchronous XMLHTTPRequest
    //var jqxhr=$.ajax({url:apiurl,async:false}, function(x,y,z){});

    //jqxhr.always();
    jqxhr.always(function(x,y,z){
    });

    //發生錯誤後 再次嘗試
    jqxhr.fail(function(x,y,z){
        console.log('fail',x,y,z);
        console.log('失敗 10分鐘後重試');
        $("#poi180324inbox >poi").html('失敗 10分鐘後重試');
        setTimeout(function(){
            console.log('再次嘗試');
            getapi();
        }, 600*1000);
    });
    //成功
    jqxhr.done(function(x,y,z){
        var json= x;
        json = $.parseJSON( x );//分析parse
        if(json.error){
            console.log( json.msg );
            $("#poi180324inbox >poi").html( json.msg );
        }

        if(json.posts){
            //console.log( json.posts );
            $("#poi180324inbox >poi").html('自動更新回文');
            animate01();//頁面下方的動畫
            updatelog01(json);//更新留言
        }
        //if(Object.keys(json.posts).length > 0){}

    });

    try{}
    catch(err){}
    finally{}
    //

}

function updatelog01(x){
    //input
    var json=x;
    //
    //console.log(Object.keys(json.posts).length ,json ); //Object length
    var new_time = new Date();
    console.log( new_time , Object.keys(json.posts).length ); //在控制台顯示更新時間//Object length
    //
    //$(".thread").find('hr').before('<div>資料'+ gg190524.cc +'</div>');
    var allElements = document.querySelectorAll('.post');
    var origlog=[];
    //網頁上的文章編號 集合起來
    $.each(allElements, function( index, value ) {
        origlog.push( $(value).attr('data-no') );
    });
    //
    //var newlogcount=0;
    Array.from(json.posts).forEach((value, index) => {
        //console.log(value.no);
        var find_post_id=origlog.indexOf( value.no.toString()  );
        //console.log(find_post_id);
        if(find_post_id<0){
            //沒找到對應編號=有新文章
            window.gg190524.新的文章數量++; //計數
            console.log( '新文章='+value.no );
            //顯示新的回文
            $(".thread").find('hr').before(value.html);//before //after
            //var tmp = $.getScript("../common/js/script.js");
            //console.log( tmp );
			document.title = '('+ window.gg190524.新的文章數量 +')'+gg190524.原始標題;
        }else{
            //沒有新文章
        }
    });
}//updatelog01




//