// ==UserScript==
// @namespace https://greasyfork.org/zh-TW/scripts/39891
// @name 今年一定島 自動更新回文
// @description 汲汲營營大報社
// @author 稻米
// @version 2019.09.02.0010.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{
var190106();
}
catch(err){}
finally{}
function var190106(){
$(document).ready(function() {
//console.log( 'jquery ready' );
//全域變數//global
var time=new Date();
window.gg190524={}; //globalVar
gg190524.原標題=document.title;
gg190524.newlogcount=0;
if( window.location.href.match("\\?res=") ){
if( $('div.thread').length == 1 ){
var url_p1 = new URL( window.location.href );
var url_p2 = new URLSearchParams( url_p1.search ).get('res'); //.substr(1)
gg190524.selfres=url_p2; //首篇編號
//
poi();
}
}
});
//
$( document ).ajaxStart(function(){ //啟動ajax時 執行這段 // AJAX 请求开始
//console.log( 'ajaxStart' );
window.gg190524=chk_gg();//檢查gg是否存在 //ajax的異步問題
});
$( document ).ajaxSend(function() { // AJAX 请求发送
//console.log( "ajaxSend" );
});
};//var190106
//
function poi(){
console.log( gg190524 );
var tmp='';
tmp = gg190524;
tmp = JSON.stringify( tmp );
tmp = encodeURIComponent( tmp );
tmp = btoa(tmp);
//存
if(window.localStorage){
window.sessionStorage.poi=tmp;
}else{
$(".thread").attr('poi',tmp);
}
//
animate01();
}//poi()
function chk_gg(){
//檢查
if( window.gg190524.原標題 ){
//console.log( '有' );
}else{
console.log( '無資料' );
//讀
var tmp='';
if(window.localStorage){
tmp = window.sessionStorage.poi;
}else{
tmp = $(".thread").attr('poi');
//$(".thread").removeAttr('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 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:2000,
}, 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){
$("#poi180324inbox >poi").html('自動更新回文');
var json= x;
json = $.parseJSON( x );//分析parse
if(Object.keys(json.posts).length > 0){
animate01();//頁面下方的動畫
updatelog01(json);//更新留言
}
});
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){
//沒找到對應編號=有新文章
gg190524.newlogcount++; //計數
console.log( '新文章='+value.no );
//顯示新的回文
$(".thread").find('hr').before(value.html);//before //after
//var tmp = $.getScript("../common/js/script.js");
//console.log( tmp );
document.title = '('+ gg190524.newlogcount +')'+gg190524.原標題;
}else{
//沒有新文章
}
});
}//updatelog01
//建立動畫的網頁元素
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 = 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
//
}