Record Thread HTML5

// ==UserScript==
// @name    Record Thread HTML5
// @description    標記閱讀文章
// @run-at document-end
// @author    NKid
// @version    2011-07-08
// @include    http://forum.*.fdzone.org/forumdisplay.php?fid=*&orderby=dateline*
// @include    http://forum.fdzone.org/forumdisplay.php?fid=*&orderby=dateline*
// @include http://pro.fdzone.org/forumdisplay.php?fid=*&orderby=dateline*
// @include http://pro.fdzone.org/forumdisplay.php?fid=*&filter=subject#by
// ==/UserScript==
GM_addStyle('.GM_RecordThread {background-color:#006699 !important;}');
GM_registerMenuCommand('Record Thread HTML5 - [Clear Record] This Forum',clearThisForum);
GM_registerMenuCommand('Record Thread HTML5 - [Clear Record] All Forums',clearAllForums);
GM_registerMenuCommand('Record Thread HTML5 - [Sync] Upload Records',syncUpload);
GM_registerMenuCommand('Record Thread HTML5 - [Sync] Download Records',syncDownload);
GM_registerMenuCommand('Record Thread HTML5 - [Show] All Records',showAllRecords);
GM_registerMenuCommand('Record Thread HTML5 - TEST Upload Json',testUploadJson);
GM_registerMenuCommand('Record Thread HTML5 - TEST Download Json',testDownloadJson);
var fid=location.href.replace(/.*fid=(\d{1,3}).*/,'$1');    //子區塊編號
var serverIP=['140.127.22.228','140.127.22.219'];
function testUploadJson() {
    var strRecords='';
    for(var i=0;i<localStorage.length;i++)
        strRecords+=',{"tid":"' + localStorage.key(i) + '" , "fid":"' + localStorage.getItem(localStorage.key(i)) + '"}';

    var json='{"timestamp": "@timestamp","records": [@records]}';
    json=json.replace('@timestamp',localStorage.getItem('timestamp'));
    json=json.replace('@records',strRecords.substring(1));
    GM_xmlhttpRequest({
            method: "POST",
            url: "http://140.127.22.228/RecordThread/RecordThread.ashx",
            data: json,
            headers: {"Content-Type": "application/x-www-form-urlencoded","a":"testUploadJson","h":location.hostname},
            onload: function(response) {GM_notification('Done.',null);}
        });
}
function testDownloadJson() {
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://140.127.22.228/RecordThread/RecordThread.ashx",
            data: null,
            headers: {"Content-Type": "application/x-www-form-urlencoded","a":"testDownloadJson","h":location.hostname},
            onload: function(response) {
                var j=JSON.parse(response.responseText);
                alert(j.timestamp);
            }
        });
}
function showAllRecords() {
        var str='';
        for(var i=0;i<localStorage.length;i++)
            str+=localStorage.key(i) + ' : ' + localStorage.getItem(localStorage.key(i)) + '\n';
        ( str== '')?alert('No Records'):alert(str.replace(/\n$/,''));
}
function chooseIP() { return serverIP[prompt('[0]140.127.22.228\n[1]140.127.22.219')];}
function syncDownload() {
    GM_xmlhttpRequest({
        method: "POST",
        url: "http://" + chooseIP() + "/RecordThread/RecordThread.ashx",
        data: null,
        headers: {"Content-Type": "application/x-www-form-urlencoded","a":"download","h":location.hostname},
        onload: function(response) {
            //console.log(response.responseText.split('\n'))
            //GM_notification('Donwloaded Record.',null);
            if (confirm('Do you want to replace the original records of \"' + location.hostname + '\" ?'))
            {
                window.localStorage.clear();
                var newRecords=response.responseText.split('\n');
                for(var i=0;i<newRecords.length;i++)
                {
                    var red=newRecords[i].split(':');
                    window.localStorage.setItem(red[0],red[1]);
                }
                GM_notification('Donwloaded Records.',null);
            }
        }
    });
}
function syncUpload() {
    if (confirm('Do you want to upload all records of \"' + location.hostname +'\" ?'))
    {
        var records=new Array();
        for(var i=0;i<localStorage.length;i++)
            records.push(localStorage.key(i) + '=' + localStorage.getItem(localStorage.key(i)));
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://" + chooseIP() + "/RecordThread/RecordThread.ashx",
            data: records.join('&'),
            headers: {"Content-Type": "application/x-www-form-urlencoded","a":"upload","h":location.hostname},
            onload: function(response) {GM_notification(response.responseText,null);}
        });
    }
}
function clearThisForum() {
    if (confirm('Do you want to clear this forum?'))
    {
        window.localStorage.removeItem(fid);
        GM_notification('Clear Record This Forum.',null);
    }
}
function clearAllForums() {
    if (confirm('Do you want to clear all forums?'))
    {
        window.localStorage.clear();
        GM_notification('Clear Record All Forum.',null);
    }
}
//上色
function markThread(eles) {
        for(var i=0;i<eles.length;i++)
            eles[i].className='GM_RecordThread';
}
//取得文章編號
function getThread() {
    var tid=window.localStorage.getItem(fid);
    //尚無記錄
    if (tid == null)
    { GM_notification('No Record.',null); }
    else    ///有記錄
    {
        //在本頁
        if (document.getElementById(tid))
        {
            GM_notification('Found in this page.',null);
            var allEles=document.querySelectorAll('#'+ tid+ ' > tr > :nth-child(n)');
            markThread(allEles)
        }
        else    //不在本頁
        {
            GM_notification('Found, but not in this page.',null);
        }
    }
}
//設定文章編號
function setThread(event) {
    //清掉舊主題的上色
    var allEles=document.getElementsByClassName('GM_RecordThread');
    var len=allEles.length
    for(var i=0;i<len;i++)
        allEles[0].className='';
    //記錄主題id並上色
    //em → th → tr → tbody
    var tidEle=document.getElementById(event.target.parentNode.parentNode.parentNode.id);
    window.localStorage.setItem(fid,tidEle.id);
    var allEles=tidEle.querySelectorAll('tr > :nth-child(n)');
    markThread(allEles);
    GM_notification('Saved.',null);
}
//trigger設定事件
function setThreadEvent() {
    var t=document.querySelectorAll('tbody[id] >tr > th > em');
    for(var i=0;i < t.length;i++)
    {
        t[i].addEventListener('click',setThread,false);
        t[i].style.cursor='pointer';
    }
}
getThread();
setThreadEvent();