// ==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();
標籤
.net
(
17
)
工作
(
29
)
面試
(
2
)
筆記
(
2
)
筆記倉庫
(
1
)
嘸蝦米
(
2
)
繪圖
(
1
)
Add-on
(
4
)
Android
(
39
)
AngularJS
(
1
)
ASP.NET
(
14
)
AutoHotKey
(
20
)
AutoIt
(
3
)
batch
(
24
)
Blogger Hack
(
3
)
Bookmarklet
(
18
)
C#
(
16
)
Chrome
(
1
)
cmd
(
22
)
CSS
(
6
)
CSS3
(
6
)
D855
(
1
)
DOM
(
17
)
DragOnIt
(
5
)
EmEditor
(
1
)
English
(
1
)
ffmpeg
(
1
)
Firefox
(
25
)
flo
(
2
)
GIMP
(
1
)
gist
(
144
)
Graphviz
(
1
)
hardware
(
1
)
HDD
(
1
)
HTML5
(
4
)
i18n
(
1
)
IIS
(
4
)
ImageMagick
(
5
)
Java
(
1
)
JavaScript
(
92
)
jhead
(
1
)
jQuery
(
6
)
JSBin
(
19
)
jsFiddle
(
4
)
JSON
(
1
)
JustDoubleClick
(
1
)
Kindle
(
1
)
MSSQL
(
26
)
MySQL
(
2
)
Network
(
1
)
node.js
(
12
)
php
(
1
)
PowerShell
(
6
)
ramdisk
(
1
)
Reg
(
3
)
RegExp
(
3
)
rpi3
(
3
)
SJ2000
(
1
)
SQLite
(
3
)
SSD
(
1
)
Stylish
(
1
)
SublimeText
(
2
)
tips
(
6
)
userChrome.js
(
3
)
userscript
(
40
)
VB.net
(
9
)
VBScript
(
2
)
VisualStudio
(
2
)
Vue.js
(
3
)
Win
(
5
)
Win2008
(
1
)
Win7
(
29
)
WinAPI
(
1
)
Winform
(
1
)
WinXP
(
3
)
XML
(
1
)
Record Thread HTML5
標籤:
userscript