590
社区成员
发帖
与我相关
我的任务
分享
// ==UserScript==
// @name CSDNViewer
// @namespace HW
// @include http://topic.csdn.net/u/*
// ==/UserScript==
var cbo = null;
addComboBox();
//改变显示样式
function OnCboChange(event)
{
updateReplyItems();
var value = this.options[this.selectedIndex].value;
for (var i = 0; i < reply_items.length; i++)
{
if (!reply_items[i].replyId) continue; // 非回复
reply_items[i].table.className = "mframe"; // 去掉隐藏属性
switch (value)
{
case "v_all": // 全部显示
reply_items[i].table.style.display = "";
reply_items[i].table.style.backgroundColor = "";
break;
case "v_only_anthor": // 楼主回复
reply_items[i].table.style.display = getAnthorName().toLowerCase()== reply_items[i].username ? "" : "none";
break;
case "hl_anthor": // 高亮楼主回复
if (getAnthorName().toLowerCase() == reply_items[i].username)
reply_items[i].table.style.backgroundColor = "yellow";
break;
case "v_only_self": //只看自己的回复
reply_items[i].table.style.display = getUserName().toLowerCase()== reply_items[i].username ? "" : "none";
break;
case "hl_self": //高亮自己的回复
if (getUserName().toLowerCase() == reply_items[i].username)
reply_items[i].table.style.backgroundColor = "yellow";
break;
case "cancel_hl":
reply_items[i].table.style.backgroundColor = "";
}
}
//对楼主主帖的处理
getAnthorContent().style.backgroundColor = value =="hl_anthor"?"yellow":"";
}
//取得楼主名字
function getAnthorName()
{
var objs = document.evaluate(
"//table[@class='mframe']/tbody/tr/td/div/ul/li/dfn/a",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
if (objs.snapshotLength == 0)
return null;
var obj = objs.snapshotItem(0);
return obj.text;
}
//取得楼主帖子正文的 td
function getAnthorContent()
{
var objs = document.evaluate(
"//table[@class='mframe']/tbody/tr/td[@class='rw']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
if (objs.snapshotLength == 0)
return null;
var obj = objs.snapshotItem(0);
return obj;
}
//取得登陆用户名
function getUserName()
{
var user = getCookie("UserName");
if (!user || (/^guest$/i).test(user))
return null;
return user;
}
//取得某个用户的下一个回复
function getNextReply(user)
{
updateReplyItems();
for (var i = 0; i < reply_items.length; i++)
{
if (!reply_items[i].replyId) continue; // 非回复
if (reply_items[i].username == user)
return reply_items[i];
}
return null;
}
//添加一个 ComBox
function addComboBox()
{
var selects = document.evaluate(
"//DIV[@class='tit']/h1/cite/select",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
if (selects.snapshotLength == 0)
return;
var select = selects.snapshotItem(0);
cbo = document.createElement("SELECT");
option = document.createElement("OPTION");
option.text = "全部";
option.value = "v_all";
cbo.add(option, null);
option = document.createElement("OPTION");
option.text = "只看楼主";
option.value = "v_only_anthor";
cbo.add(option, null);
option = document.createElement("OPTION");
option.text = "高亮楼主";
option.value = "hl_anthor";
cbo.add(option, null);
option = document.createElement("OPTION");
option.text = "只看自己";
option.value = "v_only_self";
cbo.add(option, null);
option = document.createElement("OPTION");
option.text = "高亮自己";
option.value = "hl_self";
cbo.add(option, null);
option = document.createElement("OPTION");
option.text = "取消高亮";
option.value = "cancel_hl";
cbo.add(option, null);
cbo.addEventListener("change", OnCboChange, false);
select.parentNode.insertBefore(cbo, select);
}
//摘自 csdn 的脚本 t5.js
function getCookie(name) {
var re = new RegExp("(\;|^)[^;]*(" + name + ")\=([^;]*)(;|$)");
var match = re.exec(document.cookie);
return match != null ? unescape(match[3]) : null;
}
//摘自 csdn 的脚本 t5.js
function updateReplyItems() {
if (!window.reply_items) {
var imgs = document.getElementsByTagName("img");
window.reply_items = [];
for (var i = 0; i < imgs.length; i++) {
var match = (/^grade\s+(\w+)$/i).exec(imgs[i].className);
if (match) {
var table = imgs[i].parentNode;
while (table && !(/^table$/i).test(table.tagName))
table = table.parentNode;
if (!table) continue;
grade = match[1];
var as = table.getElementsByTagName("a");
var username = "";
for (var j = 0; j < as.length; j++) {
match = (/^http\:\/\/hi\.csdn\.net\/(\w+)\/?$/i).exec(as[j].href);
if (match) {
username = match[1].toLowerCase();
break;
}
}
var dfns = table.getElementsByTagName("dfn");
var point = 0;
for (j = 0; j < dfns.length; j++) {
var p = /得分:(\d+)/.exec(dfns[j].innerHTML);
if (p) {
point = parseInt(p[1]);
break;
}
}
var ls = table.getElementsByTagName("li");
var honor = null;
for (j = 0; j < ls.length; j++) {
if (ls[j].getAttribute("name") == "honor") {
honor = ls[j];
break;
}
}
var tds = table.getElementsByTagName("td");
var body = null;
var replyId = "";
var isdeleted = false;
var tdReply = null;
for (j = 0; j < tds.length; j++) {
if (/^r?body/.test(tds[j].getAttribute("csdnid"))) {
body = tds[j];
var match = /(\d+)/.exec(tds[j].getAttribute("csdnid"));
if (match) replyId = match[1];
} else if (/^rmodify_/.test(tds[j].getAttribute("csdnid"))) {
isdeleted = /删除/.test(tds[j].innerHTML);
}
}
var divs = table.getElementsByTagName("div");
var recommend = null;
for (j = 0; j < divs.length; j++) {
if (divs[j].className == "rt") {
recommend = divs[j];
break;
}
}
reply_items.push({
"grade": grade
, "table": table
, "body": body
, "username": username
, "point": point
, "honor": honor
, "recommend": recommend
, "replyId": replyId
, "isdeleted": isdeleted
});
}
}
}
}