扩展 csdn 的功能

huwei001982 2009-08-04 05:20:42
加精
有时候查看一篇帖子的时候,只想看楼主的回复, 但现在的CSDN居然没有这个功能

于是用 greasemonkey 给 csdn 做了一些扩展功能(包括只看楼主,高亮楼主,只看自己的回复等)

运行后页面上会多一个下拉框,如下图。

显示的是“高亮楼主”的效果



完整的 greasemonkey 脚本如下(部分脚本来源于 csdn, 已做了标注)
// ==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
});
}
}
}
}
...全文
543 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveguaixiaobai 2009-08-07
  • 打赏
  • 举报
回复
强大,非常强大
kong521 2009-08-06
  • 打赏
  • 举报
回复

应该有此功能
dannyzhong 2009-08-06
  • 打赏
  • 举报
回复
这都可以改,学习了
MFCJCK 2009-08-06
  • 打赏
  • 举报
回复
csdn 的全称是什么哦?CS Development Network?
imcharming 2009-08-06
  • 打赏
  • 举报
回复
还附了代码,想得太周到了
jj1547 2009-08-06
  • 打赏
  • 举报
回复
恩,不错,学习了
c_hua6280 2009-08-06
  • 打赏
  • 举报
回复
尊敬的用户huwei001982,您好:

您的建议我已转相关负责人,该功能会在我们下次改版时加上。

感谢您支持CSDN 社区!

Regards

====================================
如果我的回答已经解决了您的问题,希望您能够及时结帖。以便我们能够更方便,及时地看到其他用户的问题!
huwei001982 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 tsing_best 的回复:]
csdn 的全称是什么哦?CS  Development Network?
[/Quote]

Chuang Shang Deng Ni
床 上 等 你
w398687283 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 quqiujie 的回复:]
弓虽
[/Quote]
m_nNightmire 2009-08-05
  • 打赏
  • 举报
回复
支持原创
应该把这帖子放到社区首页挂几个月啊
eagerle01 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jane_zhao 的回复:]
厉害啊,支持!
[/Quote]
scofic 2009-08-05
  • 打赏
  • 举报
回复
firebug实验的吧?哈哈 楼主很用心 呵呵
  • 打赏
  • 举报
回复
up
cby666168 2009-08-05
  • 打赏
  • 举报
回复
哦 看不懂 怎么操作啊
llsen 2009-08-05
  • 打赏
  • 举报
回复
强人
cuixiping 2009-08-05
  • 打赏
  • 举报
回复
迟迟没有修改帖子的功能!!!

只是要可以修改主贴
98132239 2009-08-05
  • 打赏
  • 举报
回复
大牛啊!
yidichaxiang 2009-08-05
  • 打赏
  • 举报
回复
厉害啊,支持!
cjj2003 2009-08-05
  • 打赏
  • 举报
回复
路过路过
haizhizi2006 2009-08-05
  • 打赏
  • 举报
回复
标注。
加载更多回复(16)

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧