FDZ Thread Recorder

版本記錄
  • 2012-05-24
    • 更名為“FDZ Thread Recorder”
    • 使用localStorage存取記錄
    • 各區分開儲存記錄
  • 2012-05-11
    • 初版,命名為“FDZ Record TIDs”
    • 使用GM_setValue()儲存記錄

// ==UserScript==
// @name    FDZ Thread Recorder
// @description    記錄已讀主題
// @author    nightkid@FDZ
// @version    2012-05-25
// @run-at document-end
// @include    http://forum.fdzone.org/forumdisplay.php?fid=*
// @include    http://forum.*.fdzone.org/forumdisplay.php?fid=*
// @include    http://pro.fdzone.org/forumdisplay.php?fid=*
// @include    http://pro.*.fdzone.org/forumdisplay.php?fid=*
// ==/UserScript==
GM_addStyle('#GM_recordBtn {-moz-border-radius: 3px; background-color: #006699; color: #000000; cursor: pointer; font-size: 10px; height: 15px; position: fixed; right: 20px; top: 3px; width: 15px;}');
GM_addStyle('.GM_recorded td, .GM_recorded th {background-color:#006699  !important;}');
GM_registerMenuCommand('FDZ Thread Recorder - Show All Records',showAllRecords);
GM_registerMenuCommand('FDZ Thread Recorder - Clear Records Of This Forum',clearThisForum);
GM_registerMenuCommand('FDZ Thread Recorder - Remove Repetition',removeRepetition);
var removeStr=new RegExp ("(stick|normal)thread_","g");
var fid=location.href.replace(/.*fid=(\d{1,3}).*/,'$1');    //子區塊編號
function clearThisForum() {
    if (confirm('Do you want to clear this forum?'))
    {
        window.localStorage.removeItem(fid);
        GM_notification('Clear Records Of This Forum.',null);
    }
}
function recordBtn() {
    var btn = document.createElement("div");
    btn.id="GM_recordBtn";
    btn.title="Record all threads of this page.";
    btn.accessKey="A";
    document.getElementsByTagName("body")[0].appendChild(btn);
    document.getElementById('GM_recordBtn').addEventListener('click',function(){ recordThisPage();},false);
}
function recordThisPage() {
    var uids=document.querySelectorAll("tbody[id*=thread_]");
    var tmp=[];
    for(var i=0;uids[i];i++)
        tmp.push(uids[i].id.replace(removeStr,""));
    localStorage.setItem(fid, tmp.join() + checkNull(localStorage.getItem(fid)));
    markThisPage();
    removeRepetition();
}
function setThread(event) {
    //記錄主題id並上色
    //em → th → tr → tbody
    var tidEle=document.getElementById(event.target.parentNode.parentNode.parentNode.id);
    localStorage.setItem(fid, tidEle.id.replace(removeStr,"") + checkNull(localStorage.getItem(fid)));
    tidEle.className="GM_recorded";
    GM_notification('Recorded.',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';
    }
}

function removeRepetition() {
    if (localStorage.getItem(fid) != null)
    {
        var allRecords=localStorage.getItem(fid).split(",");
        var tmp=[];
        for(var i=0;i < allRecords[i];i++)
        {
            var tmpRegx=new RegExp(allRecords[i],"g");
            if (!( tmpRegx.test(tmp)))
                tmp.push(allRecords[i]);
        }
        localStorage.setItem(fid,tmp.join());
        GM_notification("Delete " + (allRecords.length - tmp.length) + " Repetition(s)");
    }
}
function markThisPage() {
    if (localStorage.getItem(fid) != null)
    {
        var allRecords=localStorage.getItem(fid);
        var uids=document.querySelectorAll("tbody[id*=thread_]");
        for(var i=0;uids[i];i++)
            if (allRecords.indexOf(uids[i].id.replace(removeStr,"")) != -1)
                uids[i].className="GM_recorded";
    }
}
function showAllRecords() {
    console.log(localStorage);
}
function checkNull() {
    return (arguments[0] == null)?"":("," + arguments[0].split(","));
}
/*function delAllRecords() {
    localStorage.clear();
    GM_notification('Delete all records.');
}*/
setThreadEvent();
recordBtn();
markThisPage();