// ==UserScript==
// @name 今年一定島 自動更新回文
// @namespace https://greasyfork.org/zh-TW/scripts/39891
// @description 汲汲營營大報社
// @author 稻米
// @include http://*.komica2.net/00/*
// @include https://*.komica2.net/00/*
// @include http://*.komica2.net/*/pixmicat.php?res=*
// @include https://*.komica2.net/*/pixmicat.php?res=*
// @version 2018.03.29.1744.build16299
// @grant none
// ==/UserScript==
//jquery
$(document).ready(function() {
//console.log( 'jquery ready' );
//全域變數//global
time=new Date();
//console.log( time );
//$.globalEval('gg=[];');
//window.gg=[];
gg=[];
gg.time=time;
gg.timestamp=Date.parse(time); //time.getTime(); //Date.now()
$.gginin=gg;//jquery global variable
gg.cc=0;
gg.origlog=[];
gg.newlogcc=0;
gg.selfurl=window.location.href;
gg.原標題=document.title;
//
jQuery.fx.interval = 1000; //動畫更新間隔(毫秒) 預設=13//沒效果??
//console.log( gg );
//console.log( $.gginin );
//
poi();
});
//
//
//var tmp=$(function(){console.log('aaa1');}); //$(document).ready(function(){})
// iife
//var tmp=(function(){console.log('aaa2');})();
/*
http://ram.komica2.net/00/pixmicat.php?mode=module&load=mod_ajax&action=thread&html=true&op=11168423
http://www.homu-api.com/
http://www.homu-api.com/follow/10455981
http://homu.homu-api.com/res/10455438
http://homu.homu-api.com/page/0
*/
function poi(){
//
var url_p1 = new URL( gg.selfurl);
//console.log( url_p1 );//解析網址參數
//
var tmp='';
tmp=url_p1.search;//.substr(1)
var url_p2 = new URLSearchParams( tmp );
tmp=url_p2.get('res');
//console.log( tmp );//解析網址參數
gg.selfres=tmp;
//console.log( gg );
//
if(gg.selfres){ //有找到res參數才執行(回文模式)
if($('div.thread').length == 1){
//getapi();//使用api
animate01();
}
}
//poi_title();
}//poi()
function poi_title(x){
//非焦點視窗不會更新???
if(gg.selfres){ //有找到res參數才執行(回文模式)
if($('div.thread').length == 1){
if(gg.newlogcc > 0){
//gg.原標題='改變標題';
document.title = gg.原標題 +'('+gg.newlogcc+')';
}
}
}
}//poi_title()
function getapi(){
//console.log( $.gginin );
gg=$.gginin;
gg.cc=gg.cc + 1;
//console.log( gg );
try{
var apiurl='https://ram.komica2.net/00/pixmicat.php?mode=module&load=mod_ajax&action=thread&html=true&op='+gg.selfres; //綜合版原生api
//var apiurl='http://homu.homu-api.com/res/'+gg.selfres; //第三方api
//console.log(apiurl);
//jQuery 的ajax 就是返回一个promise 对象,里面含有done(), fail()方法;
//var jqxhr=$.get(apiurl, function(x,y,z){});
var jqxhr=$.ajax({
url:apiurl,
timeout:2000,
}, function(x,y,z){});
//console.log('x',y,z);
//jQuery has deprecated synchronous XMLHTTPRequest
//var jqxhr=$.ajax({url:apiurl,async:false}, function(x,y,z){});
//jqxhr.always();
//發生錯誤後 再次嘗試
jqxhr.fail(function(x,y,z){
//console.log(x,y,z);
console.log('錯誤 10分鐘後重試');
setTimeout(function(){
getapi();
}, 600*1000);
});
//成功
jqxhr.done(function(x,y,z){
//console.log(x,y,z);
json= x;
json = $.parseJSON( x );//分析parse
//console.log(Object.keys(json.posts).length ,json ); //Object length
//
//updatelog();
animate01();//頁面下方的動畫
updatelog01(json);//更新留言
poi_title(gg.newlogcc); //改變標題
});
}
catch(err){
console.log(err);
}
finally {
//console.log( 'try-catch-finally' );
}
//
}
function updatelog01(x){
json=x;
//console.log(Object.keys(json.posts).length ,json ); //Object length
var new_time = new Date();
console.log( new_time ); //在控制台顯示更新時間//Object length
//
//$(".thread").find('hr').before('<div>資料'+ gg.cc +'</div>');
var allElements = document.querySelectorAll('.post');
gg.origlog=[];//清空
//網頁上的文章編號 集合起來
$.each(allElements, function( index, value ) {
gg.origlog.push( $(value).attr('data-no') );
});
//console.log( gg.origlog );
//
Array.from(json.posts).forEach((value, index) => {
//console.log(value.no);
var find_post_id=gg.origlog.indexOf( value.no.toString() ); // 2
//console.log(find_post_id);
if(find_post_id<0){
//有新文章 //沒找到對應編號
gg.newlogcc++; //計數
console.log( '新文章='+value.no );
//顯示新的回文
$(".thread").find('hr').before(value.html);//before //after
//console.log( tmp );
}else{
//沒有新文章
//console.log( '有找到='+value.no );
}
});
}//updatelog01
//建立動畫的dom
function animate01(){
if( $("#poi180324append").length > 0){
//已存在
//還原長度
$("#poi180324inbox").css({
"width":"600px",
});
}else{
//不存在
//建立元素
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('自動更新回文<span style="float: right;"></apsn>');
//
$("#poi180324append").css({
"border":'5px solid #ff0000',
"background-color":"#ffff00",//yellow
"display":"block",
//"height":"10px",
"width":"600px",
});//連結上背景色 不想上色就把這段刪除
$("#poi180324inbox").css({
//"border":'5px solid #ff0000',
"background-color":"#ff0000",
"white-space":"nowrap",
"display":"block",
//"height":"10px",
"width":"600px",
});//連結上背景色 不想上色就把這段刪除
}//if
//
animate01b();
}
//
function animate01b(){
gg=$.gginin;
//console.log( gg );
//setTimeout
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
//
var f2=function(){
var cc=0;
var tmp='';
$("#poi180324inbox").stop().animate({width:'1px'},{
duration:60*1000, //動畫時間
easing:'linear', //'linear'
queue: false,
complete:function(){
//$(".pp3").prepend('CMPL1');
//$FFF=$(".pp3").html().substr(0,100);
//$(".pp3").html($FFF);
//跑完動畫 再抓一次api
getapi();
},
start:function(){
//$(".pp3").prepend('START1');
},
step:function(now, fx ){
//console.log( now, fx );
cc++;
//$("#poi180324inbox >span").html(cc);
},
progress: function(promise, remaining){
//console.log( promise, remaining );
var tmp = ((1-remaining) * 60).toFixed(1);
//$("#poi180324inbox >span").html(tmp);
//document.title = tmp;
}
});//animate
};
//f2();//jq animate
var f3=function(){
var start = window.performance.now();
var duration = 60*1000; //動畫持續時間
var interval = 1000/60; //動畫更新間隔 //fps
var oneElement = document.querySelector('#poi180324inbox');
//console.log( oneElement );
//
var progress = 0;
var poipoi=function(timestamp){
//console.log( timestamp ); //window.performance.now()
//if(start === null){start = timestamp;}
progress = timestamp - start;
//progress++;
//console.log( progress );
var tmp='';
//tmp=Math.floor((duration-progress)/1000);
tmp=((duration-progress)/1000).toFixed(1);
//$("#poi180324inbox >span").html(tmp);
document.title = tmp;
tmp =600 - (progress / duration)*600; //600=寬度
oneElement.style.width = tmp+'px';
if(progress < duration) {
window.requestAnimationFrame(poipoi);
}else{
//跑完動畫 再抓一次api
getapi();
}
};
var requestID=window.requestAnimationFrame(poipoi);
//console.log( requestID );
};
//f3();//requestAnimationFrame
/*
$( "div:animated" ).toggleClass( "colored" );
myDiv.clearQueue();
myDiv.stop();
*/
}