Sunday, November 13, 2011

豆瓣助手 douban helper


// ==UserScript==
// @name                豆瓣助手 douban helper
// @namespace           http://userscripts.org/scripts/show/49911
// @description         为豆瓣(www.douban.com)添加各种人性化的功能。
// @require             http://autoupdate.sinaapp.com/autoupdatehelper.js
// @include             *
// @version             6.22
/* @reason
增加:配置中心显示当前豆瓣助手版本
修复:firefox3.6.X无法运行BUG
修复:论坛相册字体变灰BUG
修复:chrome下无法右键保存全部大图BUG 
修复:交流增强工具条在论坛、相册等页面失效BUG
@end*/
// ==/UserScript==

/* ************************ 备注 ************************ */
/**
 * 看到这的同学,如果希望帮助完善豆瓣助手,请在豆瓣插件小组回复: http://www.douban.com/group/topic/6604219/
 * 如果是看到某些代码还合眼,直接使用就可以了,欢迎感谢我:-)
 *
 * ====== 未来计划: ======
 * 
 * 讨论增强失效 http://alphatown.douban.com/widget/notes/2017528/note/137521849/ (太罕见,有空再修复)
 * 豆瓣“我的推荐”太杂乱了,有分类功能吗 有些推荐只想作为个人的收藏能否隐藏呢??
 * 增加卓越当当在线试读搜索或侧栏提示
 * 话题更新提醒
 * 自定义关键字的小组话题提醒
 * 当话题有新回复时弹出提醒
 * 日记可否像相册一样加载所有分页,并且缩短为只显示标题
 * 广播小图预览,跟新浪微博看图片那个放大镜的功能。 
 * 相册大图用于豆瓣社区友邻等等
 * 剧照、海报外层就可显示大图
 * 双击标题回复、双击内容引用
 * 添加过滤广播功能
 * 预读下一页、无刷新看直播
 * 刷新、翻页后仍保留 高亮/忽略
 * 默认高亮楼主
 * 高亮引用文字
 * 友邻备注
 * 关注某人(自动高亮名字或提醒有新发言)
 * 小组增强按钮 改用全局单一事件捆绑 并不再使用name 改用class
 * 
 * 自动电梯美化
 * 将小组图标在两分类中来回移动会导致消失或分类失效
 * 小组可拖离分区(or dbclick?)
 * 分区可折叠
 * 输入框输入页码跳转
 * 翻页附加到浮动工具 开放DIY,可定义快捷键对应页面
 * 中英文区分搜索
 * 鼠标置于相册回复上方自动显示回复内容
 * 推荐到豆瓣
 * 可关闭更新提醒
 * 右上角的提醒和邀请
 * 搜索引擎改为全部自定义
 * 建立自定义快捷键哈希表,如有一致就执行
 * 检测是否已加入该小组
 */

/* ************************ 判断执行 ************************ */


var Douban = /\.douban\.com/i.test(location.href), doubanHelper = {name: '豆瓣助手',id: '49911',version: '6.22'};
GM_getValue('supportHelper', true) || (unsafeWindow.__done__=window.__done__=true);
typeof(Updater)!='undefined' && new Updater(doubanHelper).check();//自动更新
var runHelper = function(){
 autoElevator();//自动电梯*
 exBoard();// 留言板增强*
 exGroup();//交流拓展
 albBig(); //相册增强*
 friendsTool();//友邻工具条*
 addPageLoader();//页面加载控件*
};
if(Douban){
// if(self.location == top.location && Douban){
 var t1 = new Date();//时间1
 hl_input(); //输入框高亮
 addSearchBar(); //添加搜索条
 hideMSG();//隐藏小组介绍
 settingBar();//添加豆瓣助手设置按钮
 addTopPager();//置顶翻页导航条
 
 runHelper();//需重复运行的部分
 
 musicLink();//破解音乐人下载链接
 ctrlEnter();//快速回复
 slideBar();//顶部滑动导航条
 multiSearch();//全站搜索
 sortGroup();//小组分类
 settle();//浮动定位工具
 addHotKey(GM_getValue('hk_db', 'alt+Q'),function(){sw_set(false)});//添加快捷键
 $('#db_hp') && ($('#db_hp').title = '版本:V' + doubanHelper.version + ' 运行耗时:' + (new Date() - t1)/1000 + ' 秒');//记录豆瓣助手耗时
 // }else{
 // parent && parent.document.getElementById('fulliframe') && (top.location = self.location);// 对付ISP广告
};
!Douban && openDouban();//秘籍 '↑↑↓↓←→←→' 打开豆瓣
showPic(); //显示链接图片

/* ************************ 基本函数准备 ************************ */

// 获取URL参数
function QueryString(item){
  var sValue=location.search.match(new RegExp('[\?\&]'+item+'=([^\&]*)','i'));
  return (sValue && sValue[1]!= '')?sValue[1]:0;
};

// 获取Cookie
function getCookie(c_name){
 if (document.cookie.length > 0){
  c_start = document.cookie.indexOf(c_name + '=');
  if (c_start!=-1){ 
   c_start = c_start + c_name.length + 1;
   c_end = document.cookie.indexOf(';',c_start);
   if (c_end == -1){ c_end = document.cookie.length;}
   return unescape(document.cookie.substring(c_start,c_end));
  }
 }
 return '';
};

// 自造 selector
function $$(w, dom){
 return (dom || document).querySelectorAll(w);
};
function $(select){
 var name = select.substring(1);
 switch(select.charAt(0)){
  case '#':
   return document.getElementById(name);
  case '.':
   return document.getElementsByClassName(name);
  case '/':
   return document.getElementsByTagName(name);
  default:
   return document.getElementsByName(select);
 }
};

// 赋予多个对象统一样式
function setStyles(obj, css){
 for (var o=obj,i=0,j=o.length; i<j; i++){
  o[i].style.cssText = css;
 }
};

// Xpath
function xpath(query, context){
    return document.evaluate(context?(query.indexOf('.')==0?query:'.' + query):query, context || document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
};

// 拓展 insertAfter
function insertAfter(newEl, targetEl){
 var parentEl = targetEl.parentNode;
 if (parentEl.lastChild == targetEl){
  parentEl.appendChild(newEl);
 }else{
  parentEl.insertBefore(newEl, targetEl.nextSibling);
 }
};

// 隐藏或显示
function toggle(obj){
 obj.style.display = (obj.style.display == 'none')?'':'none';
 return true;
};

// keycode 转换
function getKeys(e){
 var codetable={'96':'Numpad 0','97':'Numpad 1','98':'Numpad 2','99':'Numpad 3','100':'Numpad 4','101':'Numpad 5','102':'Numpad 6','103':'Numpad 7','104':'Numpad 8','105':'Numpad 9','106':'Numpad *','107':'Numpad +','108':'Numpad Enter','109':'Numpad -','110':'Numpad .','111':'Numpad /','112':'F1','113':'F2','114':'F3','115':'F4','116':'F5','117':'F6','118':'F7','119':'F8','120':'F9','121':'F10','122':'F11','123':'F12','8':'BackSpace','9':'Tab','12':'Clear','13':'Enter','16':'Shift','17':'Ctrl','18':'Alt','20':'Cape Lock','27':'Esc','32':'Spacebar','33':'Page Up','34':'Page Down','35':'End','36':'Home','37':'←','38':'↑','39':'→','40':'↓','45':'Insert','46':'Delete','144':'Num Lock','186':';:','187':'=+','188':',<','189':'-_','190':'.>','191':'/?','192':'`~','219':'[{','220':'\|','221':']}','222':'"'};
 var Keys = '';
 e.shiftKey && (e.keyCode != 16) && (Keys += 'shift+');
 e.ctrlKey && (e.keyCode != 17) && (Keys += 'ctrl+');
 e.altKey && (e.keyCode != 18) && (Keys += 'alt+');
 return Keys + (codetable[e.keyCode] || String.fromCharCode(e.keyCode) || '');
};

// 即时显示快捷键
function trans_code(ID, codes){
 var cobj = $('#'+ID);
 cobj.value = GM_getValue(ID, codes);
 cobj.addEventListener('keydown',function(e){
  this.value = getKeys(e);
  e.preventDefault();
  e.stopPropagation();
 },false);
};

// 监视并执行快捷键对应的函数
function addHotKey(codes,func){
 document.addEventListener('keydown', function(e){
  if ((e.target.tagName != 'INPUT') && (e.target.tagName != 'TEXTAREA') && getKeys(e) == codes){
   func();
   e.preventDefault();
   e.stopPropagation();
  }
 }, false);
};

// 识别本次登录用户名
function getUserName(){
 var ck = getCookie('ck'), mixName = GM_getValue('mixName', false);
 
 if(ck){//是否已登录
  if(mixName && mixName.indexOf('@'+ck) > 0){//本次登录是否上次保存用户
   return mixName.replace('@'+ck, '');
  }
  GM_xmlhttpRequest({
   method: 'GET',
   url: 'http://www.douban.com/mine/',
   onload: function(resp){
    if(resp.status < 200 || resp.status > 300) {return;};
    var name = String(resp.responseText).match(/douban\.com\/people\/([^\/]+)\//i)[1];
    GM_setValue('mixName', name + '@' + getCookie('ck'));
    return name;
   }
  });
 }
 return false;
};

/* ************************ 豆瓣助手设置界面 ************************ */

// 添加导航栏按钮
function settingBar(){
 var db_hp = document.createElement('a');
 db_hp.id = 'db_hp';
 db_hp.href = 'javascript:';
 db_hp.innerHTML = '豆瓣助手';
 var _status = $('#status') || $('.top-nav-info')[0];

 if(_status){
  if(_status.lastChild.innerHTML){
   _status.appendChild(db_hp);
  }else{
   _status.insertBefore(db_hp, _status.lastChild.previousSibling);
  };
  $('#db_hp').addEventListener('click', function(){sw_set(false);}, false);
  
  // 加载HTML等
  GM_addStyle('\
   #db_div {\
    background:#fff;\
    position:fixed;\
    width:500px;\
    top:20%;\
    left:30%;\
    border:1px solid #bbb;\
    z-index:100;\
   }\
   #db_shadow {\
    position:absolute;\
    width:100%;\
    height:100%;\
    top:-12px;\
    left:-12px;\
    border:12px solid #000;\
    opacity:0.2;\
    z-index:-1;\
   }\
   #db_div_a {\
    position:absolute;\
    left:-69px;\
    border-top:12px solid #ccc;\
    border-bottom:12px solid #ccc;\
   }\
   #db_div_a a {\
    display:block;\
    font-weight:bold;\
    background:#ccc;\
    padding:8px;\
    border-right:1px solid #999;\
    color:#000;\
    opacity:0.7;\
   }\
   #db_div div {\
    padding:20px;\
   }\
   #db_div label {\
    cursor:pointer;\
    margin:0 40px 0 0;\
    padding:2px;\
   }\
   #db_div label:hover {\
    background:#83bf73;\
    color:#fff;\
    -moz-border-radius:3px;\
    -webkit-border-radius:3px;\
   }\
   #db_div textarea {\
    font-size:12px;\
   }\
   #db_div .gact a {\
    -moz-border-radius:10px;\
    -webkit-border-radius:10px;\
    padding:2px 20px;\
   }\
   #db_btm {\
    background:#fff;\
    display:block;\
    position:absolute;\
    top:-35px;\
    left:-12px;\
    border-right:12px solid #ccc;\
    border-top:12px solid #ccc;\
    border-left:12px solid #ccc;\
    text-align:center;\
   }\
  ');
  var db_div = document.createElement('div');
  db_div.id = 'db_div';
  db_div.style.display = 'none';
  db_div.innerHTML = '\
    <span id="db_shadow">\
    </span>\
    <span id="db_div_a">\
     <a href="javascript:"  style="border-right:1px solid #fff;background:#fff;opacity:1;">功能介绍</a>\
     <a href="javascript:">交流增强</a>\
     <a href="javascript:">相册增强</a>\
     <a href="javascript:">页面加载</a>\
     <a href="javascript:">贴图识别</a>\
     <a href="javascript:">快速搜索</a>\
     <a href="javascript:">其他功能</a>\
    </span>\
    <div id="功能介绍">\
       【版本:V' + doubanHelper.version +'】\
     <br>  【全部功能】(在豆瓣网右上角有设置按钮,或者按Alt+Q):\
     <br>  1、只看/高亮/忽略某人发言\
     <br>  2、显示小组楼层数\
     <br>  3、"直播模式"(刷新或翻页后自动只看楼主)\
     <br>  4、按 Ctrl + Enter 即可提交回复\
     <br>  5、按 Alt+Enter 在回复框中添加个性签名(在豆瓣助手配置中心可自定义内容)\
     <br>  6、引用回复按钮+浮动回复框,适用于豆瓣全站讨论页面(快捷键Shift + Enter)\
     <br>  7、小组贴图:自动识别网页中的图片链接,并将其显示出来。\
     <br>  8、书影音快速搜索链接,可自定义。\
     <br>  9、聚焦时高亮输入框,帮助集中注意力\
     <br>  10、豆瓣相册直接显示大图(在缩略图上点击鼠标中键单击显示/隐藏大图)\
     <br>  11、自动隐藏小组介绍\
     <br>  12、浮动定位工具,快速置顶置地,DIY常用链接\
     <br>  13、友邻工具条\
     <br>  14、全站搜索,在任意页面搜索书影音等\
     <br>  15、顶部滑动导航条,快速跳转节省时间\
     <br>  16、自动电梯,小组标题提供最后几页链接\
     <br>  17、破解音乐人下载链接\
     <br>  18、留言板增强,可直接回复至他人留言板\
     <br>  19、自定义小组分类\
     <br>  20、自动加载下一页\
     <br>  and so on ...\
    </div>\
    <div id="交流增强" style="display:none;">\
      <label><input type="checkbox" id="just_view" />允许只看、高亮、忽略某人发言</label>\
      <br>\
      <label><input id="hlcolor" size=6 onMouseover="this.style.background=\'#fff\';" onKeyup="this.style.background=this.value" onMouseout="this.style.background=this.value" />高亮背景色</label>\
      <br>\
      <label><input type="checkbox" id="count" />允许显示楼层数</label>\
      <label><input type="checkbox" id="requote" />允许引用、回复某人发言</label>\
      <br>\
      <label><input type="checkbox" id="tidybar" />使用紧凑工具条</label>\
      <label><input type="checkbox" id="auto_hide" />自动隐藏工具条</label>\
      <br>\
      <label><input type="checkbox" id="exBoard" />增强留言板功能</label>\
      <br>\
      <label><input id="diylen" size=4 />当引用文字超过该长度,提示是否截断</label><br>\
      <textarea rows="10" cols="81" id="diyword" style="width:450px;"></textarea><br>\
      交流增强适用于豆瓣小组与论坛等,快捷键:<br>\
      打开浮动回复框为 Shift+Enter<br>\
      退出为 Esc(会清空内容)<br>\
      提交为 Ctrl+Enter(适用于豆瓣大部分输入框)<br>\
      个性签名使用快捷键为 Alt+Enter,可瞬间输入设定文字。(适用于豆瓣大部分输入框)\
    </div>\
    <div id="相册增强" style="display:none;">\
      <label><input type="checkbox" id="highslide" />开启高阶相册浏览模式</label>\
      <br>\
      <label><input type="checkbox" id="alb_big" />允许缩略图上点击鼠标中键显示大图</label>\
      <br>\
      <label><input type="checkbox" id="loadAllPic" />允许一键显示全部大图</label>\
      <br>\
      <label><input type="checkbox" id="down_photo" />允许批量下载大图</label>\
      <br>\
      <p>开启相册高阶浏览模式后,将提供前所未有的相册浏览体验。\
      <br>  从此克服豆瓣看图的种种不便,看图不再是麻烦。\
      <br>  此功能配合豆瓣助手自带的加载全部页面功能一起使用,效果更佳。\
      <br>  (后续版本将提供更丰富的配置选项)</p>\
      <br>\
      <br>\
    </div>\
    <div id="页面加载" style="display:none;">\
      <label><input type="checkbox" id="addPageLoader" />开启页面加载功能</label>\
      <br>\
      <label><input type="checkbox" id="autoLoadPage" />自动加载下一页</label>\
      <br>\
      <label>加载多个分页时,每次加载<input id="loadPageNum" size=1 />页</label>\
      <br>\
      <p>开启页面加载后,点击“加载下一页”按钮可将下一页显示在当前页面。\
      <br>  若开启“自动加载下一页”功能,浏览到网页底部时,豆瓣助手会自动加载下一页。</p>\
      <br>\
      <br>\
      <br>\
      <br>\
      <br>\
    </div>\
    <div id="贴图识别" style="display:none;">\
      <label><input type="radio" name="showpic" />不开启图片识别</label><br>\
      <label><input type="radio" name="showpic" />只对豆瓣网开启</label><br>\
      <label><input type="radio" name="showpic" />对所有网页开启</label><br>\
      <label><input type="radio" name="showpic" />对所有网页开启,点击鼠标中键时才加载(速度最快,推荐选项)</label>\
      <br>\
      <p>本功能为自动识别网页中文字链接中的图片,并将其显示出来。\
      <br>  主要用于克服豆瓣小组不能贴图的缺陷,方便看图。\
      <br>  (此功能对所有网站适用,默认只识别豆瓣中的图片;可用鼠标中键单击显示/隐藏图片)</p>\
      <br>\
      <br>\
    </div>\
    <div id="快速搜索" style="display:none;">\
      <label><input type="checkbox" id="search_bar" />开启快速搜索条</label>\
      <label>添加自定义搜索引擎↓</label>\
      <textarea rows="10" cols="81" id="otherEngines" style="width:450px;"></textarea>\
      <br><strong>添加其他引擎的使用说明:</strong><br>\
      打开 “豆瓣助手” 界面后看到添加其他搜索引擎文本框里有默认的如下内容:<br>\
      ----------------------------------------------------------<br>\
      豆瓣小组|http://www.douban.com/group/search?q={word}<br>\
      百度知道|http://zhidao.baidu.com/q?word={gb:word}&amp;ct=17&amp;tn=ikaslist&amp;rn=10<br>\
      ----------------------------------------------------------<br>\
      1、以花括号括起来的部分 {word} 代表搜索的关键字,该关键字将会以 UTF-8 方式编码。如果想要以 GB2312 方式编码,请使用{gb:word},编写地址时要注意这点。<br>\
      2、以竖线 | 为分隔符,前面的 “豆瓣小组” 是在页面上显示的链接名称,后面的 http://www.douban.com/group/search?q={word} 是发起搜索的链接地址。<br>\
      3、每行对应一个搜索引擎。特别注意每行中必须有个竖线 | 分隔前后两部分。<br>\
      你可以仿照例子添加其他搜索,比如雅虎、wiki等等的。\
    </div>\
    <div id="其他功能" style="display:none;">\
      <label>设置界面快捷键<input size=8 id="hk_db"/></label><br><br>\
      <label><input type="checkbox" id="hl_input" />高亮焦点输入框 </label>\
      <label><input type="checkbox" id="musicLink" />破解音乐人下载链接</label>\
      <br>\
      <label><input type="checkbox" id="hide_grpinfo" />自动隐藏小组介绍</label>\
      <label><input type="checkbox" id="autoElevator" />开启自动电梯</label>\
      <br>\
      <label><input type="checkbox" id="settler" />开启浮动定位条 </label>\
      <label><input type="checkbox" id="sortGroup" />开启小组分类 </label>\
      <br>\
      <label><input type="checkbox" id="multiSearch" />开启全站搜索  </label>\
      <label><input type="checkbox" id="slideBar" />开启顶部滑动导航条</label>\
      <br>\
      <label><input type="checkbox" id="supportHelper" />通过在卓越、淘宝商城上买书支持豆瓣助手</label>\
      <br>\
      <br>\
      <label><input type="checkbox" id="friendsTool" />开启友邻工具条</label>\
      <br>\
      <label>浮动定位条快捷键↓</label><br>\
      <label>置顶:<input size=8 id="hk_top"/></label>\
      <label>置底:<input size=8 id="hk_btm"/></label><br>\
      <label>上页:<input size=8 id="hk_pre"/></label>\
      <label>下页:<input size=8 id="hk_nxt"/></label><br>\
      <label>浮动定位条自定义链接↓(用法与添加自定义搜索引擎类似)</label><br>\
      <textarea rows="6" cols="81" id="otherLinks" style="width:450px;"></textarea>\
      <br>\
      <label><a href="http://userscripts.org/scripts/show/49911" target=_blank>详细介绍及使用说明</a></label>\
      <label><a href="http://www.douban.com/group/topic/6604219/" target=_blank>反馈意见</a></label>\
    </div>\
    <span id="db_btm">\
     <input type="button" title="部分选项需要刷新页面才能生效" id="s_ok" value="√ 确认">\
     <input type="button" title="取消本次设定,所有选项还原" id="s_cl" value="X 取消">\
    </span>\
  ';
  $('/body')[0].appendChild(db_div);
  $('#s_ok').addEventListener('click', function(){sw_set(true);}, false);
  $('#s_cl').addEventListener('click', function(){sw_set(false);}, false);

  $('#db_div_a').addEventListener('mouseover', function(e){
   if(!e.target.id){
    setStyles($$('#db_div DIV'), 'display:none;');
    $('#'+e.target.innerHTML).style.display = 'block';
    setStyles($$('#db_div_a A'), '');
    e.target.style.cssText = 'border-right:1px solid #fff;background:#fff;opacity:1;';
   }
  }, false);
 };
};

// 显示\隐藏设置栏
function sw_set(ch){
 toggle($('#db_div'));
 if (ch){
  //写入图片识别选项
  var pic_sw = $('showpic');
  for (var i=0,j=pic_sw.length; i<j; i++){if (pic_sw[i].checked){GM_setValue('choice', i);}};
  GM_setValue('hlcolor', $('#hlcolor').value);//写入高亮背景色
  GM_setValue('otherEngines', $('#otherEngines').value);//写入自定义搜索引擎
  GM_setValue('otherLinks', $('#otherLinks').value);//写入自定义链接
  GM_setValue('diyword', $('#diyword').value);//写入个性签名
  GM_setValue('diylen', parseInt($('#diylen').value));//自定义引文截断长度
  GM_setValue('loadPageNum', parseInt($('#loadPageNum').value));//加载多个分页的页数
  db_write('just_view');//写入只看、高亮、忽略选项
  db_write('search_bar');//写入搜索条选项
  db_write('hl_input');//写入高亮输入框选项
  db_write('count');//写入数楼选项
  db_write('auto_hide');//写入自动隐藏选项
  db_write('tidybar');//写入工具条样式
  db_write('requote');//写入引用回复选项
  db_write('settler');//写入浮动定位工具选项
  db_write('hide_grpinfo'); //写入隐藏小组介绍选项
  db_write('friendsTool'); //写入友邻工具选项
  db_write('multiSearch'); //写入全站搜索选项
  db_write('slideBar'); //写入滑动导航条选项
  db_write('autoElevator'); //写入自动电梯选项
  db_write('exBoard'); //写入留言板增强选项
  db_write('musicLink'); //写入音乐下载链接选项
  db_write('sortGroup'); //写入小组分类选项
  db_write('alb_big');//中键相册大图
  db_write('highslide'); //相册浏览模式
  db_write('loadAllPic'); //一键显示全部大图
  db_write('down_photo'); //批量下载大图
  db_write('supportHelper'); //通过在卓越、淘宝商城上买书支持豆瓣助手
  db_write('addPageLoader'); //开启加载下一页功能
  db_write('autoLoadPage'); //自动加载下一页功能
  GM_setValue('hk_db', $('#hk_db').value);//写入自定义搜索引擎
  GM_setValue('hk_top', $('#hk_top').value);//写入置顶快捷键
  GM_setValue('hk_btm', $('#hk_btm').value);//写入置底快捷键
  GM_setValue('hk_pre', $('#hk_pre').value);//写入上一页快捷键
  GM_setValue('hk_nxt', $('#hk_nxt').value);//写入下一页快捷键
 }else{                                           
  //读取高亮背景色
  $('#hlcolor').value = GM_getValue('hlcolor', '#eeffee');
  $('#hlcolor').style.background = GM_getValue('hlcolor');
  $('showpic')[GM_getValue('choice', 1)].checked = true;//读取图片识别选项
  $('#otherEngines').value = GM_getValue('otherEngines', '土豆豆单|http://so.tudou.com/psearch/{word}\n土豆视频|http://so.tudou.com/isearch/{word}\n优酷视频|http://so.youku.com/search_video/q_{word}\n优酷列表|http://so.youku.com/search_playlist/q_{word}\n豆瓣小组|http://www.douban.com/group/search?q={word}\n百度知道|http://zhidao.baidu.com/q?word={gb:word}&ct=17&tn=ikaslist&rn=10');//读取自定义搜索引擎
  $('#otherLinks').value = GM_getValue('otherLinks', '我的话题|http://www.douban.com/group/my_topics\n我的评论|http://www.douban.com/mine/discussions\n');//读取自定义链接
  $('#diyword').value = GM_getValue('diyword', '个性签名');//读取个性签名
  $('#diylen').value = GM_getValue('diylen', 500);//自定义引文截断长度
  $('#loadPageNum').value = GM_getValue('loadPageNum', 10);//加载多个分页的页数
  db_read('just_view',true);//读取只看、高亮、忽略选项
  db_read('search_bar',true);//读取搜索条选项
  db_read('hl_input',true);//读取高亮输入框选项
  db_read('count',true);//读取数楼选项
  db_read('auto_hide',false);//读取自动隐藏选项
  db_read('tidybar', true);//读取工具条样式
  db_read('requote', true);//读取引用回复选项
  db_read('settler', true);//读取浮动定位工具选项
  db_read('hide_grpinfo',true);//读取隐藏小组介绍选项
  db_read('friendsTool',true);//读取友邻工具选项
  db_read('multiSearch',true);//读取全站搜索选项
  db_read('slideBar',true);//读取滑动导航条选项
  db_read('autoElevator',true);//读取自动电梯选项
  db_read('exBoard',true);//读取留言板增强选项
  db_read('musicLink',true);//读取音乐下载链接选项
  db_read('sortGroup',true);//读取小组分类选项
  db_read('alb_big',true);//中键相册大图
  db_read('highslide',false);//相册浏览模式
  db_read('loadAllPic',true);//一键显示全部大图
  db_read('down_photo',true);//批量下载大图
  db_read('supportHelper',true);//通过在卓越、淘宝商城上买书支持豆瓣助手
  db_read('addPageLoader',true);//加载下一页功能
  db_read('autoLoadPage',true);//自动加载下一页功能
  trans_code('hk_db', 'alt+Q');//显示豆瓣助手设置界面快捷键
  trans_code('hk_top', 'W');//显示置顶快捷键
  trans_code('hk_btm', 'S');//显示置底快捷键
  trans_code('hk_pre', 'A');//显示上一页快捷键
  trans_code('hk_nxt', 'D');//显示下一页快捷键
 }              
};

//checkbox通用读取、写入函数
function db_read(id,value){$('#'+id).checked = GM_getValue(id, value);};
function db_write(id){GM_setValue(id, $('#'+id).checked);};


/* ************************ 豆瓣相册处理 ************************ */

// 相册增强
function albBig(){
 var photitle = $$('.article>.photitle, .bd>.photitle, .article>.opt-bar-line')[0];// 相册、活动,  小站、阿尔法城图片墙, 剧照、海报
    // GM_addStyle('.event-photo-list li {min-width:33%!important;} .photo_wrap {min-width:170px!important;}');
    
 if (/album|photos(?!\/photo)/i.test(location.href) && photitle){
 
  if(!$('#switchHighslide')){//避免加载下一页后重复操作
   
   // 添加相册工具条
   photitle.innerHTML += '<br/>'
    + (GM_getValue('highslide', false)
     ? '<a href="javascript:" id="switchHighslide" class="btn-donate fleft" style="background:#83BF73;color:#fff;" title="点击关闭高阶浏览模式">关闭浏览模式</a>'
     : '<a href="javascript:" id="switchHighslide" class="btn-donate fleft" title="点击开启高阶浏览模式,享受浏览图片的乐趣 by豆瓣助手">开启浏览模式</a>'
     )
    + '&nbsp;&nbsp;&gt;<a href="javascript:" class="all_page" title="在本页面合并显示多个分页的图片,若分页较多,需耗时较长">加载多个分页</a>'
    + (GM_getValue('loadAllPic', true) && '&nbsp;&nbsp;&gt;<a href="javascript:" id="showAllBig" title="点击显示本页全部大图,再次点击全部还原">显示全部大图</a>')
    + (GM_getValue('down_photo', true) && '&nbsp;&nbsp;&gt;<a href="javascript:" id="downPhoto" title="批量下载全部大图的方法介绍,点击后请看右侧栏说明">批量下载大图</a>');
   
   $('#switchHighslide').addEventListener('click', function(){// 切换高阶浏览模式
    GM_setValue('highslide', !GM_getValue('highslide', false));
    location.reload();
   }, false);
   $('.all_page')[0].addEventListener('click', function(){loadPage(false, runHelper)}, false);// 顶部加载多个分页
   $('#showAllBig') && $('#showAllBig').addEventListener('click', function(){alb_all(false);}, false);// 一键显示全部大图
   $('#downPhoto') && $('#downPhoto').addEventListener('click', function(){down_photo();}, false);// 批量下载大图
  };
  
  // 允许缩略图上点击鼠标中键显示大图
  GM_getValue('alb_big', true) && alb_all(true);
  
  // 相册浏览模式
  /** 
   * Name:    Highslide JS
   * Version: 4.1.9 (2010-07-05)
   * Author:  Torstein Hønsi
   * Support: www.highslide.com/support
   * License: www.highslide.com/#license
   */
  if(GM_getValue('highslide', true)){
  
   albMark();// set highslide mark
   
   // add JS
   !GM_getValue('highslide_js', false) && GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://js-addon.googlecode.com/files/highslide.js',
    onload: function(xhr){
     if(xhr.status == 200){
      GM_setValue('highslide_js', xhr.responseText);
      var a = document.createElement('script');
      a.innerHTML = xhr.responseText;
      $('/head')[0].appendChild(a);
     }
    }
   });
   var a = document.createElement('script');
   a.id = 'highslide_js';
   a.innerHTML = GM_getValue('highslide_js', '');
   $('#highslide_js') || $('/head')[0].appendChild(a);
   
   
   // add css
   !GM_getValue('highslide_css', false) && GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://js-addon.googlecode.com/files/highslide.css',
    onload: function(xhr){
     if(xhr.status == 200){
      GM_setValue('highslide_css', xhr.responseText);
      GM_addStyle(xhr.responseText);
     }
    }
   });
   GM_addStyle(GM_getValue('highslide_css', ''));
   
   // add config
   var a = document.createElement('script');
   a.id = 'highslide_config'
   a.innerHTML = "\
    hs.align = 'center';\
    hs.transitions = ['expand', 'crossfade'];\
    hs.fadeInOut = true;\
    hs.dimmingOpacity = 0.8;\
    hs.wrapperClassName = 'dark borderless floating-caption';\
    hs.captionEval = 'this.thumb.alt';\
    hs.marginBottom = 155;\
    hs.numberPosition = 'caption';\
\
    hs.addSlideshow({\
     interval: 2000,\
     repeat: false,\
     useControls: true,\
     overlayOptions: {\
      className: 'text-controls',\
      position: 'bottom center',\
      relativeTo: 'viewport',\
      offsetY: -60\
     },\
     thumbstrip: {\
      position: 'bottom center',\
      mode: 'horizontal',\
      relativeTo: 'viewport'\
     }\
    });\
   ";
   $('#highslide_config') || $('/head')[0].appendChild(a);
  }
 }
};

// 添加相册浏览模式标记
function albMark(){
 var unitBox = $$('.photo_wrap, .event-photo-list>ul>li, .poster-col4>li');// 相册、活动,  小站、阿尔法城图片墙, 剧照、海报 图片单元
 for (var a,img,i=0,j=unitBox.length; i<j; i++){
  img = unitBox[i].getElementsByTagName('img')[0];
  if(img && !img.alt){
   a = unitBox[i].getElementsByTagName('a')[0];
   a.className += ' highslide';
   a.setAttribute('onclick', 'return hs.expand(this, {src:"'+img.src.replace(/thumb/, /movie\.douban/i.test(location.href) ? 'raw' : 'photo')+'"});');//暂不支持私密相册,有时间再配合chk_big()支持
   
   desc = unitBox[i].getElementsByTagName('div')[unitBox[i].getElementsByTagName('div').length-1];//最后一个
   !/\/#comments/i.test(desc.innerHTML) && (desc.innerHTML += ' <a href="'+ a.href +'">0回应</a>');
   img.alt = a.title + '<br/>' + desc.innerHTML.replace(/<p>.*<\/p>/, '');//去除小站、阿尔法城图片墙的双重说明文字
  }
 }
};

// 批量下载大图 
function down_photo(){
 $('.aside')[0].innerHTML = '\
  <p style="background:#E9F4E9;color:#0C7823;font-size:14px;padding:10px;">批量下载全部大图的两种方式:</p>\
  <p>【方式一】:点击以下链接,右键点击“另存为”该页面,保存类型选择“网页,全部”,即可在与页面同名文件夹下得到全部大图;</p>\
  <p><a href="javascript:void(0);" id="allPhotoPage" class="btn-donate" title="点击后打开全部大图(包含所有分页)  by豆瓣助手"><img border="0" src="data:image/gif;base64,R0lGODlhCgAKAJEDAMzMzP9mZv8AAP///yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFAAADACwAAAAACgAKAAACF5wncgaAGgJzJ647cWua4sOBFEd62VEAACH5BAUAAAMALAEAAAAIAAMAAAIKnBM2IoMDAFMQFAAh+QQFAAADACwAAAAABgAGAAACDJwHMBGofKIRItJYAAAh+QQFAAADACwAAAEAAwAIAAACChxgOBPBvpYQYxYAIfkEBQAAAwAsAAAEAAYABgAAAgoEhmPJHOGgEGwWACH5BAUAAAMALAEABwAIAAMAAAIKBIYjYhOhRHqpAAAh+QQFAAADACwEAAQABgAGAAACDJwncqi7EQYAA0p6CgAh+QQJAAADACwHAAEAAwAIAAACCpRmoxoxvQAYchQAOw==">读取到前<span id="loadingPage"></span>页(<span id="loadPercent"></span>)</a></p>\
  <hr>\
  <p>【方式二】:使用迅雷、旋风等工具下载以下大图链接:</p>\
  <p>(以下链接为当前页面往后全部分页的大图链接集合)</p>\
  <p><textarea id="photoLinks" rows="20" style="width:100%;font-size:9px;"></textarea></p>';
 
 // 读取并写出指定页面的大图链接
 var RnWPhotoLink = function(dom){
  var imgs = $$('.photo_wrap img, .photo-item img, .cover img', dom||document);
  for (var i=0,j=imgs.length; i<j; i++){
   chk_big(imgs[i].parentNode, $('#photoLinks'));
  };
 };
 
 // 操作当前页面
 RnWPhotoLink();
 
 // 操作后续页面
 endlessPage(
  function(dom){
   RnWPhotoLink(dom);
   var totalNum = $$('.paginator>a')[$$('.paginator>a').length-1].textContent, loadingPage = $$('.paginator .thispage', dom)[0].textContent;
   $('#loadPercent').innerHTML = parseInt(eval(loadingPage + '/' + totalNum)*100).toString() + '%';
   $('#loadingPage').innerHTML = loadingPage;
  },
  function(){$('#allPhotoPage').innerHTML = '[打开全部大图]'}
 );
 
 // 新页面打开全部大图
 $('#allPhotoPage').addEventListener('click', function(){
  //构造全部大图HTML页面代码
  var photoSrc = $('#photoLinks').value.match(/\S+\n/g);
  var html = '<head>\
   <meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-Type\">\
   <title>'+document.title+'</title>\
   <style>p{font-family:微软雅黑;font-size:14px;} img{height:180px;width:320px;} img:hover{height:auto;width:auto;position:relative;left:-60px;top:-50px;z-index:2;border:10px solid #FFF;} div{float:left;height:180px;width:320px;margin:2px} body{padding:50px;}</style>\
   <script></script>\
   </head><body>\
   <p>【相册标题】:'+document.title+'</p>\
   <p>【相册来源】:<a target="_blank" href="'+location.href+'">'+location.href+'</a></p>\
   <p>【相片数量】:'+photoSrc.length+'</p>\
   <p>【下载说明】:点击“另存为”该页面,保存类型选择“网页,全部”,即可在与页面同名文件夹下得到全部大图</p>\
   <p>(以下图片均为最大尺寸原图,鼠标悬停可查看原始尺寸。建议在图片全部显示完毕后再保存,若有图片加载失败,可按下 Ctrl + F5 强制刷新)</p>';
  for (var i=0,j=photoSrc.length; i<j; i++){
   html += '<div><img src="'+photoSrc[i]+'"></div>';
  };
  html += '</body>';
  /chrome/i.test(navigator.userAgent)
   ? (location.href = "javascript:'"+html+"'")
   : window.open("javascript:'"+html+"'");
 }, false)
};

// 监视中键或全部显示
function alb_all(b){
 var imgs = $$('.photo_wrap img, .photo-item img, .cover img');
 for (var i=0,j=imgs.length; i<j; i++){
  b && imgs[i].parentNode.addEventListener('mousedown', function(e){// 鼠标中键点击
   if (e.button == 1){
    chk_big(this);// 0左键; 1中键; 2右键
    e.preventDefault();
    e.stopPropagation();
   };
  }, false);
  !b && chk_big(imgs[i].parentNode);// 批量大图
 };
};

// 相册切换大小图
// p:小图img的父节点; toURL:是否改为输出大图url; textarea是否输出到指定textarea
function chk_big(p, textarea){
 var h = p.innerHTML;
 if(/otho\.douban/i.test(h)){
  // 私密相册
  p.innerHTML += '';
  var img = p.getElementsByTagName('img')[0];
  if(!img.getAttribute('srcx')){
   GM_xmlhttpRequest({
    method: 'GET',
    url: p.href,
    onload: function(resp){
     if(resp.status < 200 || resp.status > 300) {return;};
     img.setAttribute('srcx', img.src);
     var bigSrc = String(resp.responseText.match(/img\ssrc="(http:\/\/otho\.[^'"]+)"/i)[1]);
     if(!!textarea){
      textarea.value += bigSrc+'\n';
     }else{
      img.src = bigSrc;
     };
    },
    onerror: function(){return;}
   });
  }else{
   var l = img.src;
   img.src = img.getAttribute('srcx');
   img.setAttribute('srcx', l);
  };
 }else{
  // 公共相册
  var bigPhotoREG = /movie\.douban/i.test(location.href) ? ['raw', /photo\/raw/] : ['photo', /photo\/photo/];
  if(!!textarea){
   textarea.value += p.getElementsByTagName('img')[0].src.replace(/thumb/, bigPhotoREG[0]) + '\n';
  }else{
   p.innerHTML = /thumb/i.test(h) ? h.replace(/thumb/, bigPhotoREG[0]) : h.replace(bigPhotoREG[1], 'photo/thumb');
  };
 };
 
 // 添加css样式,使大图方便查看
 if(!textarea){//只输出链接时不执行
  var parent2 = p.parentNode.parentNode;
  if(p.className == 'album_photo'){
   // 小站
   parent2.style.cssText = (parent2.style.width == 'auto') ? '':'width:auto;min-width:33%;';
  }else if(p.parentNode.className == 'cover'){
   // 剧照
   parent2.style.cssText = (parent2.style.width == 'auto') ? '':'width:auto;min-width:136px;position:relative;z-index:100;';
   p.getElementsByTagName('img')[0].style.cssText = 'max-width:960px;';
  }else{
   // 相册、活动
   p.parentNode.style.width = (p.parentNode.style.width == 'auto') ? '170px': 'auto';
  };
 }
};

/* ************************ 个人资料备份处理 ************************ */


// 写过2篇日记
// http://www.douban.com/people/iRSS/notes

// 我看:http://movie.douban.com/mine
// 我听:http://music.douban.com/mine
// 我读:http://book.douban.com/mine

// 共有4个相册
// http://www.douban.com/people/iRSS/photos
// 参加了54个小组
// http://www.douban.com/people/iRSS/groups
// 关注过26个小站
// http://www.douban.com/people/iRSS/minisite
// 关注的人
// http://www.douban.com/contacts/list
// 推荐
// http://www.douban.com/people/iRSS/recs
// 加心26首音乐
// http://douban.fm/mine?type=liked
// 我喜欢的歌手
// http://douban.fm/mine?type=liked


// starBackup();
// function starBackup(){
 // var user = $('#edit_signature');
 // if(user){
  // //添加备份按钮
  // var span = document.createElement('span');
  // span.innerHTML = '<a href="javascript:void(0);" id="starBackup" class="btn-donate" title="点击后备份日记  by豆瓣助手">[资料备份]</a>';
  // user.appendChild(span);
  
  // //捆绑备份事件
  // $('#starBackup').addEventListener('click', function(){
   // var textarea = document.createElement('div');
   // textarea.style.cssText = 'position:relative;width:940px;z-index:10;top:20px;';
   // textarea.innerHTML = '<span id="loadPercent"></span><a id="closeBTM" title="关闭">X</a><textarea rows="20" style="width:100%;"></textarea>';
   // $('#db-nav-main').appendChild(textarea);
   // GM_addStyle('#closeBTM{background:#FFF;border:1px solid #BBB;border-radius:10px;color:#BBB;display:block;height:17px;position:absolute;right:-17px;text-align:center;top:26px;width:17px;} #closeBTM:hover{background:#733;border:1px solid #F33;color:#FFF;}')
  
   
  // }, false)
  
  
 // }
// };


/* ************************ 书影音页面处理 ************************ */

// 添加搜索条
function addSearchBar() {
 if ($('#mainpic') && GM_getValue('search_bar', true) && !$('.s_bar')[0]){
  GM_addStyle('\
   #mainpic {\
    overflow: visible;\
   }\
   .s_bar {\
    z-index: 97;\
    position: absolute;\
   }\
   .s_lin {\
    position: absolute;\
    top: -62px;\
    left: 0px;\
    visibility: hidden;\
   }\
   .s_lin2 {\
    position: absolute;\
    top: -62px;\
    left: 62px;\
    visibility: hidden;\
   }\
   .s_bar a {\
    -moz-border-radius: 7px;\
    -webkit-border-radius: 7px;\
    display: block;\
    background: #eef9eb;\
    padding: 5px;\
    width: 50px;\
    border: 1px solid #fff;\
    line-height: 160%;\
   }\
   .s_bar a:hover {\
    border: 1px solid #aaa;\
    background: #fff;\
    color: #000;\
   }\
   .s_bar:hover .s_tt {\
    visibility: hidden;\
   }\
   .s_bar:hover .s_lin, .s_bar:hover .s_lin2 {\
    visibility: visible;\
   }\
   .s_bar .s_tt {\
    border: 1px solid #aaa;\
    width: 90px;\
    line-height: 100%;\
   }\
  ');
  
  var keyword = $$('h1 span')[0].textContent;//取得书影音标题
  var keyWd = encodeURIComponent(keyword);
  var s_div = document.createElement('div');
  s_div.className = 's_bar';
  s_div.innerHTML = '\
   <a href="javascript:" class="s_tt">Search it \
    <font color="red"><b>!</b>\
    </font></a>\
   <span class="s_lin">\
    <a href="http://www.baidu.com/s?ie=utf-8&wd=' + keyWd + ' "target="_blank">百度一下</a>\
    <a href="http://www.google.com/search?&q=' + keyWd + ' "target="_blank">Google</a>\
    <a href="http://www.gougou.com/search?id=92452&search=' + keyWd + '"target="_blank">迅雷下载</a>\
    <a href="http://book.gougou.com/search?restype=3&id=92452&search=' + keyWd + ' "target="_blank">迅雷书籍</a>\
    <a href="http://www.verycd.com/search/folders/' + keyWd + ' "target="_blank">VeryCD</a>\
    <a href="http://zh.wikipedia.org/w/index.php?search=' + keyWd + ' "target="_blank">维基百科</a>\
    <a href="http://books.google.com/books?q=' + keyWd + ' "target="_blank">谷歌书籍</a>\
    <a href="http://video.google.cn/videosearch?q=' + keyWd + '&www_google_domain=www.google.cn&emb=0#" target="_blank">谷歌视频</a>\
   </span>\
   <span class="s_lin2">\
   </span>\
  ';
  $('#mainpic').appendChild(s_div);
  otherEngines(keyword);
 }
};

// 添加其他搜索引擎
function otherEngines(keyword){
 var engines = GM_getValue('otherEngines', '土豆豆单|http://so.tudou.com/psearch/{word}\n土豆视频|http://so.tudou.com/isearch/{word}\n优酷视频|http://so.youku.com/search_video/q_{word}\n优酷列表|http://so.youku.com/search_playlist/q_{word}\n豆瓣小组|http://www.douban.com/group/search?q={word}\n百度知道|http://zhidao.baidu.com/q?word={gb:word}&ct=17&tn=ikaslist&rn=10').split(/\n/);
 for (i=0,j=engines.length; i<j; i++) {
  var engine = engines[i].split('|');
  if (engine.length > 1) {
   if (engine[1].indexOf('{gb:word}') >= 0){
    replace2GB(keyword, engines[i], engine[0]);
   }else{
    var searchlinks = '<a href="'+ engines[i].replace(engine[0] + '|', '').replace(/{word}/ig, encodeURIComponent(keyword)) +'" target="_blank" >'+ engine[0].replace(/{word}|{gb:word}/ig, keyword.replace('<', '&lt;').replace('>', '&gt;')) +'</a>';
    $('.s_lin2')[0].innerHTML += searchlinks;
   }
  }
 }
};


// 将其他搜索引擎中的搜索关键字替换成 GB2312 编码格式。编码通过查询 baidu 获得
// 感谢 Googlekingkong 的作者,这是他想出的方法
function replace2GB(keyword, enginesi, engine0){
 GM_xmlhttpRequest({
  method: 'GET',
  url: 'http://www.baidu.com/s?ie=utf-8&wd=' + encodeURIComponent(keyword),
  overrideMimeType: 'text/xml; charset=gb2312',
  onload: function(resp){
   if (resp.status < 200 || resp.status > 300) {
    return;
   };
   var keywordGB = String(resp.responseText.match(/word=[^'"&]+['"&]/i)).replace(/word=|['"&]/ig,'');
   var searchlinks = '<a href="'+ enginesi.replace(engine0 + '|', '').replace(/{gb:word}/ig, keywordGB) +'" target="_blank" >'+ engine0.replace(/{word}|{gb:word}/ig, keyword.replace('<', '&lt;').replace('>', '&gt;')) +'</a>';
   $('.s_lin2')[0].innerHTML += searchlinks;
  },
  onerror: function(){
   return;
  }
 });
};

/* ************************ 图片识别处理 ************************ */

// 检测、显示图片
function showPic(){
 var a_links = document.links;
 _pic_ = /^http[^\?\s]*\.(?:jpg|jpeg|jpe|jfif|bmp|png|gif|tif|tiff|ico)/i;
 switch ( parseInt(GM_getValue('choice', 1)) ){
 case 0:
  break;
 case 1:
  if (Douban || location.href == 'http://userscripts.org/scripts/show/49911') {
  }else{
   break;
  };
 case 2:
  for (var i=0,n=a_links.length; i<n; i++){
   checkIMG(a_links[i]);
  };
  break;
 case 3:// 点击时才检测图片
  for (var i=0,n=a_links.length; i<n; i++){
   a_links[i].addEventListener('mousedown', function(e){
    (e.button == 1) && checkIMG(this)
   }, false);
  };
  break;
 }
};

//检测图片
function checkIMG(link){
 var href = link.href;
 var inner = link.innerHTML;
 if ( _pic_.test(href) && !/<img\s/i.test(inner) ){
  link.addEventListener('mousedown', function(e){
   (e.button == 1) && toggle(this.childNodes[0]) && toggle(this.childNodes[1])// 图片\链接切换
  }, false);
  link.innerHTML = '<img style="max-width:520px;" alt="图片载入ing..." title="点击鼠标中键可切换链接/图片 by豆瓣助手" src="' + href + '" /><span style="display:none;">' + inner + '</span>';
 }
};

/* ************************ 阿尔法城, 小组, 书影音评论 + 论坛 + 相册 增强处理 ************************ */

// 隐藏小组介绍
function hideMSG(){
 var h2 = $('/h2')[0] && $('/h2')[$('#pop_login')?1:0];
 if(GM_getValue('hide_grpinfo',true) &&  h2 && /最近小组话题/.test(h2.innerHTML) && !$('#_toggle')){
  toggle($('.bd')[2]);
  $('#header').innerHTML += '<a id="_toggle" style="font-size:12px;position:absolute;top:165px;z-index:1;">&gt;显示/隐藏小组介绍&lt;</a>';
  $('#_toggle').addEventListener('click',function(){toggle($('.bd')[2])} ,false);

 }
};

// 自动电梯
function autoElevator(){
 var reNums = xpath('.//table[@class="list-b" or @class="olt"]/tbody/tr[not(@dbmark)]/td[last()-1]');
 if( GM_getValue('autoElevator',true) && reNums.snapshotItem(0) &&/回应/.test(reNums.snapshotItem(0).textContent + reNums.snapshotItem(1).textContent) ){//论坛表格第一行没有“回应”
  GM_addStyle('.pageNum{float:right;}');
  var reply = xpath('.//table[@class="list-b" or @class="olt"]/tbody/tr[not(@dbmark)]/td[1]');
  for(var i=0,j=reNums.snapshotLength; i<j; i++){
   var title = reply.snapshotItem(i), numWord = reNums.snapshotItem(i).textContent.replace(/[\s\D]*/g,''), num = Number(numWord), lastPage, href, linkCache = '<span class="pageNum">(';
   title.parentNode.setAttribute('dbmark', '1');//已处理标记,加载下一页后不重复处理
   if(!isNaN(num) && num>100){
    lastPage = numWord.slice(0, -2);
    href = title.getElementsByTagName('a')[0].href;
    // num > 400 && (linkCache += ' <a href="'+ href +'?start='+ (lastPage-3) +'00" title="倒数第四页">'+ (lastPage-2) +'</a> |');
    // num > 300 && (linkCache += ' <a href="'+ href +'?start='+ (lastPage-2) +'00" title="倒数第三页">'+ (lastPage-1) +'</a> |');
    num > 200 && (linkCache += ' <a href="'+ href +'?start='+ (lastPage-1) +'00" title="倒数第二页">'+ lastPage +'</a> |');
    linkCache += ' <a href="'+ href +'?start='+ lastPage +'00#last" title="自动电梯:最后一页">'+ (lastPage-1+2) +'</a> ) </span>';
    title.innerHTML = linkCache + title.innerHTML;
   }
  }
 }
};

// 添加交流拓展工具条
function exGroup(){
 var ct = GM_getValue('count', true), jv = GM_getValue('just_view', true), re = GM_getValue('requote', true);
 var postArea = $$('div.comment-item, ul.topic-reply, div#comments')[0];// 回复区域
 // 阿尔法城*, 小组*, 书影音评论!, 论坛* + 相册*
 
 if ( (ct || jv || re) && postArea ){
  
  // 书影音评论 重构
  var chgArea = $$('div#comments>span.wrap')[0];
  if (chgArea){
   chgArea.parentNode.innerHTML = chgArea.parentNode.innerHTML
   .replace(/(<h2>|<div)/, '</p><div class="group_banned"></div><br/></div>$1')// 末尾补完
   .replace(/<span class="wrap">/, '<div class="dbhp"><span class="wrap">')
   .replace(/<\/h3><\/span>/g, '</h3></span><p>')// 以p包围发言
   .replace(/<span class="wrap">/g, '</p><div class="group_banned"></div><br/></div> <div class="dbhp"><span class="wrap">')// 首位嵌套
   .replace(/<\/p><div class="group_banned"><\/div><br\/><\/div> <div class="dbhp">/, '');// 去除开头多余
  };
  
  // 论坛 + 相册 重构
  // if ($$('div#comments>table')[0]){
   // postArea.innerHTML = postArea.innerHTML
    // .replace(/<\/h4><\/span>/g, '</h4> </span><p>')
    // .replace(/<\/td><\/tr>/g, '</p></td> </tr>');
  // };
  
  //修复字体变灰问题
  GM_addStyle('div#comments td,div#comments p{color:#111 !important;} div#comments .wrap a{color:#369 !important;} div#comments a:hover,div#comments a:active{background:#039 !important;color:#FFF !important;text-decoration:none;}');
  
  // 未处理的回复楼层
  var replys = xpath(
   './/div[contains(@class,"comment-item") and not(@dbmark)]' +
   '|.//ul[contains(@class,"topic-reply")]/li[not(@dbmark)]' + // 小组
   '|.//div[@id="comments"]/div[contains(@class,"dbhp") and not(@dbmark)]' + // 书影音评论
   '|.//div[@id="comments"]/table[not(@dbmark)]'// 电影海报
   );
  
  var layerNum = $('.layerNum'), start = Number(layerNum[0] ? layerNum[layerNum.length-1].textContent : QueryString('start')) + 1;

  //工具条CSS & HTML
  GM_addStyle('a.ln-report{position:static;}');//修复“举报广告”阻挡工具条的问题
  GM_addStyle(GM_getValue('auto_hide', false)?'.ctrl_tool{display:none;} li:hover .ctrl_tool{display:block;}':'');
  var re_s = re?'<a name="db_re" href="javascript:" title="回复该用户发言" >回</a>\
     <a name="db_qt" href="javascript:" title="引用该用户发言" >引</a>':'';
  var re_l = re?'&gt;<a name="db_re" href="javascript:" title="回复该用户发言" >回复</a>&nbsp;&nbsp;\
     &gt;<a name="db_qt" href="javascript:" title="引用该用户发言" >引用</a>&nbsp;&nbsp;':'';
  var jv_s = jv?'<a name="db_jv" href="javascript:" title="只看该用户所有发言" >只</a>\
     <a name="db_hl" href="javascript:" title="高亮该用户所有发言,再次点击取消高亮" >亮</a>\
     <a name="db_ig" href="javascript:" title="忽略该用户所有发言" >略</a>\
     <a name="db_bk" href="javascript:" title="复原所有发言" >原</a>':'';
  var jv_l = jv?'&gt;<a name="db_jv" href="javascript:" title="只看该用户所有发言" >只看</a>&nbsp;&nbsp;\
     &gt;<a name="db_hl" href="javascript:" title="高亮该用户所有发言,再次点击取消高亮" >高亮</a>&nbsp;&nbsp;\
     &gt;<a name="db_ig" href="javascript:" title="忽略该用户所有发言" >忽略</a>&nbsp;&nbsp;\
     &gt;<a name="db_bk" href="javascript:" title="复原所有发言" >复原</a>&nbsp;&nbsp;':'';
  var clibtn = GM_getValue('tidybar', true) ? '&gt;'+re_s+jv_s+'&nbsp;&nbsp;' : re_l+jv_l;

  //逐楼添加工具条
  for (var i=0,l=replys.snapshotLength; i<l; i++){
   var replyI = replys.snapshotItem(i);
   var rehead = replyI.querySelector('div.author, div.bg-img-green>h4, h3>span.pl, td>span.wrap>h4');// 留言头部标题
   replyI.setAttribute('dbmark', '1');//已处理标记,加载下一页后不重复处理
   if (ct){//数楼
    // rehead.firstChild.insertData(0,Number(i) + start + '楼 ');
    // body.setAttribute('startNum', Number(i) + start);
    rehead.innerHTML = '<span class="layerNum">' + String(i+start) + '</span>楼 ' + rehead.innerHTML;
   };
   var bar = replyI.querySelector('div.group_banned, td:last-child');
   bar && (bar.className += ' admin-lnks');
   if (clibtn && bar){
    var span = document.createElement('span');
    span.className='gact fright ctrl_tool';
    span.innerHTML = clibtn;
    bar.appendChild(span);
   };
  };
  
  //监听交流拓展功能按钮
  if(!window.exGroupEven){
   window.exGroupEven = true;
   document.body.addEventListener('click',function(e){
    var tar = e.target;
    switch(tar.name || tar.id){
     case 'db_re':
     case 'db_qt':
      re && requote(tar);//引用回复
      break;
     case 'db_jv':
     case 'db_hl':
     case 'db_ig':
     case 'db_bk':
      jv && just_view(tar);//只看高亮等
      break;
    };
   } ,false);
  };
  
  //楼主工具条
  var LZbar = $$('div.note-ft, div.post-content, div.topic-opt, div.review-panel, .wr td>div.pl')[0];// 日记, 阿尔法城, 小组, 书影音评论, 论坛
  if (jv && LZbar && !$('#autolift')){
   var toptool = document.createElement('span');
   toptool.className='fleft';
   toptool.id = $$('#db-usr-profile a, .post-info>.from>a, h3>.pl20>a, .piir a, .wr a')[0].href;
   toptool.innerHTML = '\
    &gt;<a href="javascript:" id="db_jv" title="只看楼主发言" >只看</a>&nbsp;&nbsp;\
    &gt;<a href="javascript:" id="db_hl" title="高亮楼主所有发言,再次点击取消高亮" >高亮</a>&nbsp;&nbsp;\
    &gt;<a href="javascript:" id="db_ig" title="忽略楼主所有发言" >忽略</a>&nbsp;&nbsp;\
    &gt;<a href="javascript:" id="db_bk" title="复原所有发言" >复原</a>&nbsp;&nbsp;\
    &gt;<a href="javascript:" id="livemod" title="开启直播模式,刷新及翻页后自动只看楼主" >直播模式</a>&nbsp;&nbsp;\
    &gt;<a href="javascript:" id="autolift" title="点击自动生成电梯链接" >电梯助手</a>&nbsp;&nbsp;';
   LZbar.appendChild(toptool);
   
   //直播按钮
   $('#livemod').addEventListener('click', function(){
    location.href = location.href.replace(location.search,'').replace(location.hash,'') + '?jv=' + xpath('.//a[contains(@href,"/people/") and not(./child::img)]', this.parentNode.parentNode.parentNode).snapshotItem(0).href.match(/people\/([^\/\s]+)\/?/)[1];
   }, false);
   
   // 电梯助手
   $('#autolift').addEventListener('click', function(){
    if($('.paginator')[0]){
     var autolift = '请把以下内容粘帖进帖子\n直达电梯:';
     for (var p=$$('.aside .paginator>a'),i=0,l=p.length; i<l; i++){
      autolift += '\n第' + p[i].textContent + '页:' + p[i].href;
     }
     var re_text = $('#re_text');
     re_text.value += autolift + '#last\nby Douban Helper';
     $('#re_f').style.display = 'block';
     re_text.focus();
    }else{
     alert('只有一页无需电梯');
    }
   }, false);
  };
  
  // 检测是否直播模式
  islivemod();
  
  if (re && !$('#re_f')){// 添加快速回复框
   var re_f = document.createElement('div');
   re_f.id = 're_f';
   re_f.style.cssText = 'position:fixed;top:25%;border:1px solid #ccc;display:none;';
   var action = /group|forum/.test(location.href)?'add_comment':'?post=ok#last';
   re_f.innerHTML = '\
    <form name="comment_form" method="post" action="'+ action +'" >\
     <div style="display: none;"><input name="ck" value='+ getCookie('ck') +' type="hidden"></div>\
     <textarea id="re_text" name="rv_comment" rows="20" style="font-size:12px;font-family:Arial;width:310px;border:0px;border-bottom:1px solid #ccc;"></textarea><br>\
     <input value="加上去" type="submit">\
     <input value="取消" type="button" onClick="document.getElementById(\'re_f\').style.display=\'none\'">\
     <input value="清空" type="button" onClick="document.getElementById(\'re_text\').value=\'\';document.getElementById(\'re_text\').focus();">\
     <a class="fright gact" href="javascript:;" title=" \n快捷键:\n打开为Shift+Enter\n退出为Esc(会清空内容)\n提交为Ctrl+Enter\n个性签名为Alt+Enter(在豆瓣助手中设置)。\n\n在输入框内除Ctrl+Enter以及Esc外,其他快捷键无效。\n发言失败时,请自行判断是否已经登录并有权发言。" onClick="document.getElementById(\'re_text\').value+=this.title;document.getElementById(\'re_text\').focus();">[使用说明]</a>\
     <a class="fright gact" href="javascript:;" title=" \n'+GM_getValue('diyword', '个性签名')+'" onClick="document.getElementById(\'re_text\').value+=this.title;document.getElementById(\'re_text\').focus();">[个性签名]</a>\
    </form>';
   $('.aside')[0].appendChild(re_f);
   
   //修复豆瓣相册在输入框内按方向键翻页的UBG
   unsafeWindow.input_ban && unsafeWindow.input_ban();
   
   //加载 Shift + Enter 开关回复框快捷键
   addHotKey('shift+Enter',function(){
    var re_text = $('#re_text');
    toggle($('#re_f'));
    re_text.focus();
   });
   //退出快捷键为 Esc
   $('#re_f').addEventListener('keydown', function(e){
    if (e.keyCode == '27'){
     toggle($('#re_f'));
     $('#re_text').value = '';
    }
   }, false); 
   //个性签名快捷键为 Alt + Enter
   for(var i=0,j=$('rv_comment').length;i<j;i++){
    $('rv_comment')[i].addEventListener('keydown', function(e){
     if (e.altKey && e.keyCode == '13'){
      this.value += '\n'+ GM_getValue('diyword', '个性签名');
     }
    }, false);
   }
  }
 }
};

// 只看\高亮\忽略\复原 操作
function just_view(tar){
 var isLive = (typeof(tar) == 'string')?true:false;// 是否直播
 var todo = isLive ? 'db_jv' : (tar.name || tar.id);// 点击按钮类别
 var parent_3 = isLive ? '' : tar.parentNode.parentNode.parentNode;// 加三元选择符是为了兼容“直播模式”
 var clickNameUrl = isLive ? tar : (tar.parentNode.id || xpath('.//a[contains(@href,"/people/") and not(./child::img)]', parent_3).snapshotItem(0).href).match(/people\/([^\/\s]+)\/?/)[1];// 楼主||楼层发言人url
 var replys = $$('div.comment-item, ul.topic-reply>li, div#comments>div.dbhp, div#comments>table');
 for (var i=0,j=replys.length; i<j; i++){
  var reply = replys[i];
  var isit = (xpath('.//a[contains(@href,"/people/")]', reply).snapshotItem(0).href.indexOf(clickNameUrl) >= 0)?true:false;
  if (todo == 'db_jv'){
   reply.style.display = isit?'':'none';
  }else if(todo == 'db_hl' && isit){
   reply.style.background = (reply.style.background)?'':GM_getValue('hlcolor', '#eeffee');
  }else if(todo == 'db_ig' && isit){
   reply.style.display = 'none';
  }else if(todo == 'db_bk'){
   reply.style.display = '';
   reply.style.background = '';
  }
 }
};

// 直播模式
function islivemod(){
 var name = QueryString('jv');
 if (name !== 0){
  just_view(decodeURIComponent(name));
  var live = $('#livemod');
  if (live){
   live.textContent = '关闭直播';
   live.title = '已开启直播模式,点击取消直播; 点击“复原”按钮可临时查看全部帖子';
   live.style.background = '#fffe15';
   live.href = location.href.replace(location.search,'');
  };
  if ($('.paginator')[0]){
   var next = $$('.paginator a');
   for (var i=0,j=next.length; i<j; i++){
    next[i].href += '&jv='+name;
   }
  }
 }
};

// 引用回复
// 感谢 NullPointer ,该功能参照了他的 Reply buttons for new Douban 中的格式
function requote(tar){
 var re_f = $('#re_f'), re_text = $('#re_text');// 回复浮层与输入框
 var todo = tar.name || tar.id;// 点击按钮类别
 var parent_3 = tar.parentNode.parentNode.parentNode;
 var clickname = xpath('.//a[contains(@href,"/people/") and not(./child::img)]', parent_3).snapshotItem(0).textContent;// 楼层发言人名
 var rn = (re_text.value == '')?'':'\n';
 if (todo == 'db_re'){
  var alltext = rn + '@' + clickname + '\n';
 }else if(todo == 'db_qt'){
  var rehead = parent_3.querySelector('div.author, div.bg-img-green>h4, h3>span.pl, td>span.wrap>h4').textContent.replace(/\s+/g,' ');// 留言头部标题内容
  var redoc = window.getSelection().toString() || // 选中内容
     parent_3.getElementsByTagName('p')[0].textContent.replace(/\n\s*\n/g, '\n').replace(/^\s{7}/g, '');// 留言正文内容
  if (redoc.search(/(-{50,})[\s\S]+\1/g)>-1){
   if (confirm('是否删去引文中的引文?')){redoc = redoc.replace(/(-{50,})[\s\S]+\1/g,'').replace(/\n\s*\n/g, '\n');}
  };
  var len = redoc.length;
  var diylen = GM_getValue('diylen', 500);
  if (len > diylen){
   if (confirm('引文太长,是否省略一部分?')){redoc = redoc.substr(0,diylen)+ '......\n(以上引文省略'+ (len-diylen) +'字)';}
  };
  var sepr = '----------------------------------------------------------';
  var alltext  =  rn+sepr+'\n'+rehead+'\n'+redoc+'\n'+sepr+'\n';
 };
 re_text.value += alltext;
 re_f.style.display='';
 re_text.focus();
 re_text.setSelectionRange(re_text.value.length, re_text.value.length); // 修正chrome版输入框光标定位问题
};  

/* ************************ 豆瓣社区处理 ************************ */

// 友邻工具条
function friendsTool(){
 if(GM_getValue('friendsTool', true) && $('#db-timeline-hd')){
  $('#noGroupYet') || ($('.menu-list')[0].innerHTML += '<li><a id="noGroupYet" title="查看未分组友邻 by豆瓣助手" href="?tag=2">未分组</a></li>');
  
  GM_addStyle('.Friends_Tool_Bar{visibility:hidden;} .mbtr:hover .Friends_Tool_Bar{visibility:visible;}');
  // 友邻
  var firends = xpath('.//li[@class="mbtr" and not(@dbmark)]//a[contains(@href,"/people/") and not(@onclick) and not(@class) and not(contains(@href,"recs?rid=")) and not(../self::*[@class="pl"]) and not(../self::*[@class="indentrec"]) and not(contains(preceding-sibling::text(),"、"))]');
  for(var i=0,j=firends.snapshotLength; i<j; i++){
   var toolBar = document.createElement('div'), firend = firends.snapshotItem(i), name = firend.href, nname = firend.innerHTML;
   toolBar.className = 'Friends_Tool_Bar';
   toolBar.innerHTML = '<div class="clear"></div><span>\
    <a href="'+name.replace('people','book/list')+'">书</a> |\
    <a href="'+name.replace('people','movie/list')+'">影</a> |\
    <a href="'+name.replace('people','music/list')+'">音</a> |\
    <a href="'+name+'miniblogs?type=saying">说</a> |\
    <a href="'+name+'miniblogs">广播</a>|\
    <a href="'+name+'reviews">评论</a>|\
    <a href="'+name+'notes">日记</a>|\
    <a href="'+name+'photos">相册</a>|\
    <a href="'+name+'recs">推荐</a>|\
    <a href="'+name+'groups">小组</a>|\
    <a href="'+name+'board">留言板</a>|\
    <a href="'+name+'contacts'+'">关注</a>|\
    <a href="/doumail/write?to='+name.slice(29,-1)+'">发豆邮</a>\
    <a href="javascript:" style="color:#83bf73;" title="点击展开更多"\
     onclick="this.target=\'\';\
     this.innerHTML=/gt/.test(this.innerHTML)?\'&lt;&lt;&lt;\':\'&gt;&gt;&gt;\';\
     var obj=this.parentNode.getElementsByTagName(\'span\')[0];\
     obj.style.display=obj.style.display?\'\':\'none\';">&gt;&gt;&gt;</a>\
    <span style="display:none;">\
    <a href="'+name+'event?g=w">活动</a>|\
    <a href="'+name+'online">线上</a>|\
    <a href="'+name+'offers">二手</a>|\
    <a href="'+name+'doulists">豆列</a>|\
    <a href="'+name+'minisite">迷你站</a>|\
    <a href="'+name+'artist">音乐人</a>|\
    <a href="'+name+'hosts">主办方</a>|\
    </span>\
    <a href="#" onclick="$(\'textarea\').attr(\'value\',\'@\'+\''+nname+'\'+\' \'); $(\'textarea\').focus()">对<span style="color:#83bf73;">'+nname+'</span>说</a>\
    </span>';
   var theLi = (firend.parentNode.tagName == 'LI') ? firend.parentNode : firend.parentNode.parentNode;
   theLi.appendChild(toolBar);
   theLi.setAttribute('dbmark', '1');//已处理标记,加载下一页后不重复处理
  }
  // 音乐人
  var firends = xpath('.//li[@class="mbtr" and not(@dbmark)]//a[contains(@href,"/artist/") and not(@onclick) and not(@class) and not(contains(@href,"recs?rid=")) and not(../self::*[@class="pl"]) and not(../self::*[@class="indentrec"]) and not(contains(preceding-sibling::text(),"、"))]');
  for(var i=0,j=firends.snapshotLength; i<j; i++){
   var toolBar = document.createElement('div'), firend = firends.snapshotItem(i), name = firend.href, nname = firend.innerHTML;
   toolBar.className = 'Friends_Tool_Bar';
   toolBar.innerHTML = '<div class="clear"></div><span>\
    <a href="'+name+'miniblogs">广播</a> |\
    <a href="'+name+'notes">日记</a> |\
    <a href="'+name+'photos">相册</a> |\
    <a href="'+name+'public_album">乐迷相册</a> |\
    <a href="'+name+'videos'+'">视频</a> |\
    <a href="'+name+'subjects'+'">专辑</a> |\
    <a href="'+name+'discussion">论坛</a> |\
    <a href="'+name+'board">留言板</a> |\
    <a href="'+name+'events">活动</a> |\
    <a href="#" onclick="$(\'textarea\').attr(\'value\',\'@\'+\''+nname+'\'+\' \'); $(\'textarea\').focus()">对<span style="color:#83bf73;">'+nname+'</span>说</a>\
    </span>';
   var theLi = (firend.parentNode.tagName == 'LI') ? firend.parentNode : firend.parentNode.parentNode;
   theLi.appendChild(toolBar);
   theLi.setAttribute('dbmark', '1');//已处理标记,加载下一页后不重复处理
  }
 }
};

// 小组分类
function sortGroup(){
 if(/group\/mine/i.test(location.href) && GM_getValue('sortGroup', true)){
  GM_addStyle('h1 {width:590px;} h1 span{font-weight:normal;} #bydbhp {visibility:hidden;color:#BBB;font-size:12px;} h1:hover #bydbhp {visibility:visible;}\
   .article dl{cursor:move;} legend{font-weight:bold;font-size:14px;}\
   .article dl:hover {-moz-outline:1px solid #a6d098;outline:1px solid #a6d098;-moz-outline-radius:3px;-webkit-outline-radius:3px;background:#efe;}\
   fieldset {-moz-border-radius:5px;-webkit-border-radius:5px;}\
   fieldset div a{-moz-border-radius:10px;-webkit-border-radius:10px;background:#FFF;border:1px solid #BBB;color:#BBB;display:block;width:17px;height:17px;bottom:20px;left:568px;position:relative;text-align:center;}\
   fieldset div a:hover{background:#733;border:1px solid #f33;color:#fff}\
   ');
  
  // 定义分类容器响应
  var sortEven = function(obj){
   // 双击可修改分类标题
   obj.firstChild.addEventListener('dblclick', function(){
    var input=this.lastChild;
    input.value=this.textContent;
    input.style.display='block';
    input.focus();
    input.select();
    this.firstChild.style.display='none';
   }, false);
   // 回车保留标题修改
   obj.querySelector('input').addEventListener('keydown', function(e){
    if (e.keyCode == '13'){
     var span = this.previousSibling;
     this.value = (this.value == '' ? span.innerHTML : this.value);
     this.style.display = 'none';
     span.innerHTML = this.value;
     span.style.display='block';
    }
   }, false);
   // 失去焦点保留标题修改
   obj.querySelector('input').addEventListener('blur', function(){
    var span = this.previousSibling;
    this.value = (this.value == '' ? span.innerHTML : this.value);
    this.style.display = 'none';
    span.innerHTML = this.value;
    span.style.display='block';
   }, false);
   // 解散本分类
   obj.querySelector('div a').addEventListener('click', function(){
    if(confirm('确定要解散本分类?')){
     var fs = this.parentNode.parentNode;
     for(var i=0,dl=fs.querySelectorAll('dl'),l=dl.length; i<l; i++){
      $('.'+dl[i].className)[1].style.display = 'block';
     };
     fs.parentNode.removeChild(fs);
    };
   }, false);
   // allows us to drop
   obj.addEventListener('dragover', function(e){
    e.preventDefault(); 
    e.dataTransfer.dropEffect = 'copy';
    this.style.background = '#efefef';
    return false;
   }, false);
   obj.addEventListener('dragleave', function(e){
    this.style.background = '';
   }, false);
   // drop
   obj.addEventListener('drop', function(e){
    e.preventDefault();
    e.stopPropagation();
    var el = $('.'+e.dataTransfer.getData('Text'))[0]; // 获取被拖拽节点
    this.appendChild(el.cloneNode(true));
    el.style.display = 'none';
    this.getElementsByTagName('b')[0].style.display = 'none';
    this.style.background = 'block';
    return false;
   }, false);
  };
  
  // 增加分类区
  $('.article')[0].insertBefore(document.createElement('div'), $$('.article h2')[0]);
  
  // 添加按钮
  $$('h1')[0].innerHTML += '\<span class="gact fright">\
   &nbsp;&gt;<a title="添加新的小组分类" href="javascript:" id="addSort">添加分类</a>\
   &nbsp;&gt;<a title="保存小组分类数据" href="javascript:" id="saveSort">保存</a>\
   &nbsp;&gt;<a title="清空分类,恢复原始状态" href="javascript:" id="recoverSort">还原</a>\
   </span>\
   <span id="bydbhp" class="gact fright">分类完毕后请别忘了点击保存(by豆瓣助手)&nbsp;&nbsp;&nbsp;</span>';

  // 预读分类信息
  GM_getValue('sortGroupHTML', false) && ($$('.article div')[0].innerHTML =  GM_getValue('sortGroupHTML'));
  $('.indent obssin')[0].style.display = 'none';
  $('.indent obssin')[1] && ($('.indent obssin')[1].style.display = 'none');
  
  // 判断用户执行读取分类信息
  if(getUserName()){
    var cname = 'sortGroupHTML' + getUserName();
    
    var html=$$('.article')[0].innerHTML;
    // 读取写入小组分类
    GM_getValue(cname, false) && ($$('.article div')[0].innerHTML =  GM_getValue(cname));
    
    // 使小组图标可拖拽 去重 
    for(var i=0, dl=$$('.article dl'), lk=$$('.article dl a:last-child'), el, l=dl.length; i<l; i++){
     el = dl[i];
     hr = lk[i].href;
     pr = el.parentNode;
     el.className = hr + ' ob';
     var theSame = $('.'+el.className)[1];
     if(theSame){// 隐藏重复出现小组
      el.style.display = 'none';
     }else if(pr.tagName!='DIV' && html.indexOf(hr)<0){// 剔除已退出小组 
      pr.removeChild(el);
     }else{
      el.setAttribute('draggable', 'true');
      el.setAttribute('onDragstart', 'event.dataTransfer.effectAllowed = "copy";event.dataTransfer.setData("Text", this.className);'); // only dropEffect='copy' will be dropable, and can not copy event
     }
    };
    $('.indent obssin')[0].style.display = 'block';
    $('.indent obssin')[1] && ($('.indent obssin')[1].style.display = 'block');
    
    // 为现有分类附加事件响应
    for(var i=0,fs=$$('fieldset'),l=fs.length; i<l; i++){
     sortEven(fs[i]);
    };
    
    // 添加分类
    $('#addSort').addEventListener('click', function(){
     var sort = document.createElement('fieldset');
     sort.innerHTML = '<legend><span>标题(双击可修改)</span><input value="" style="display:none;"></legend><div><a title="解散本分类">X</a></div><b>拖拽小组到此区域<br><br></b>';
     $$('.article div')[0].appendChild(sort);
     sortEven(sort);
    }, false);
    
    // 保存分类
    $('#saveSort').addEventListener('click', function(){
     GM_setValue(cname, $$('.article div')[0].innerHTML);
     GM_setValue('sortGroupHTML', $$('.article div')[0].innerHTML);
     alert('小组分类保存成功!');
    }, false);
    
    // 还原
    $('#recoverSort').addEventListener('click', function(){
     if(confirm('确定要清空分类,恢复原始状态?')){
      GM_setValue(cname, false);
      GM_setValue('sortGroupHTML', false);
      location.reload();
     }
    }, false);
  }
 }
};

/* ************************ 其他杂项功能 ************************ */

// 输入框高亮
function hl_input() {
 if (GM_getValue('hl_input', true)){
  GM_addStyle('input:focus, select:focus, textarea:focus {-moz-outline: 2px solid -moz-rgba(255,153,0,0.5);outline: 2px solid -moz-rgba(255,153,0,0.5);-moz-outline-radius: 3px;-webkit-outline-radius: 3px;}');
 }
};

// 浮动定位工具
function settle(){
 if ($('.aside')[0] && GM_getValue('settler', true)){
  var settler = document.createElement('div');
  settler.className = 'gact';
  settler.style.cssText = 'position:fixed;bottom:0;border:1px solid #ccc;z-index:10;';
  settler.innerHTML = '\
   <a href="javascript:;" title="置顶,快捷键为:'+GM_getValue('hk_top', 'W')+'" onclick="scrollTo(0,0);">&nbsp;Top↑&nbsp;</a>\
   <a href="javascript:;" title="置底,快捷键为:'+GM_getValue('hk_btm', 'S')+'" onclick="scrollTo(0,99999999);">&nbsp;Btm↓&nbsp;</a>\
   <a href="javascript:;" title="上一页,快捷键为:'+GM_getValue('hk_pre', 'A')+'" id="prev">&nbsp;Prev←&nbsp;</a>\
   <a href="javascript:;" title="下一页,快捷键为:'+GM_getValue('hk_nxt', 'D')+'" id="next">&nbsp;Next→&nbsp;</a>';
  
  // 添加自定义链接
  var links = GM_getValue('otherLinks', '我的话题|http://www.douban.com/group/my_topics\n我的评论|http://www.douban.com/mine/discussions\n').split(/\n/);
  for (i=0,j=links.length; i<j; i++) {
   var l = links[i].split('|');
   if (l.length > 1){
    settler.innerHTML += '<a href="'+ links[i].replace(l[0] + '|', '') +'">'+ l[0] +'</a>';
   }
  }
  
  $('.aside')[0].appendChild(settler);
  addHotKey(GM_getValue('hk_top', 'W'),function(){scrollTo(0,0)});
  addHotKey(GM_getValue('hk_btm', 'S'),function(){scrollTo(0,99999999)});
  addHotKey(GM_getValue('hk_pre', 'A'),function(){gotopage($('#pre_photo'), $('.prev')[0])});
  addHotKey(GM_getValue('hk_nxt', 'D'),function(){gotopage($('#next_photo'), $('.next')[0])});
  $('#prev').addEventListener('click', function(){gotopage($('.prev')[0])}, false); 
  $('#next').addEventListener('click', function(){gotopage($('.next')[0])}, false); 
 }
};

// 翻页
function gotopage(obj1, obj2, obj3){
 var going = obj1 || obj2 || obj3, h = going && (going.href || going.getElementsByTagName('a')[0].href) || false;
 h && (location.href = h)||alert('呃……翻不动了');
};

// 全站搜索
function multiSearch(){
 if($$('form[name=ssform] span')[1] && GM_getValue('multiSearch', true)){
  $$('form[name=ssform] span')[1].innerHTML += '\
   <div id="db_scr_btm" title="双击:立即搜索;单击:选择搜索范围">\
   <div class="db_scr_btm">综合</div>\
   <div class="db_scr_btm">社区</div>\
   <div class="db_scr_btm">读书</div>\
   <div class="db_scr_btm">电影</div>\
   <div class="db_scr_btm">音乐</div>\
   <input type="hidden" value="" name="cat">\
   </div>';
  GM_addStyle('.db_scr_btm{background:#E9F4E9;color:#0C7823;cursor:pointer;display:none;float:left;text-align:center;position:relative;width:19%;border-left:1px solid #E9F4E9;border-right:1px solid #E9F4E9;} .db_scr_btm:hover{position:relative;top:-1px;border-bottom:1px solid #a6d098;border-left:1px solid #a6d098;border-right:1px solid #a6d098;background:#fff;} .nav-srh:hover .db_scr_btm{display:block;}');
  $('#db_scr_btm').addEventListener('click', function(e){
   if(!e.target.id){
    setStyles($('.db_scr_btm'), '');
    e.target.style.cssText = 'position:relative;top:-1px;border-bottom:1px solid #a6d098;border-left:1px solid #a6d098;border-right:1px solid #a6d098;background:#fff;';
    var n = e.target.innerHTML;
    n == '综合' && ($('ssform')[0].action = 'http://www.douban.com/subject_search') && ($('cat')[0].value = '');
    n == '社区' && ($('ssform')[0].action = 'http://www.douban.com/search') && ($('cat')[0].value = '');
    n == '读书' && ($('ssform')[0].action = 'http://book.douban.com/subject_search') && ($('cat')[0].value = '1001');
    n == '电影' && ($('ssform')[0].action = 'http://movie.douban.com/subject_search') && ($('cat')[0].value = '1002');
    n == '音乐' && ($('ssform')[0].action = 'http://music.douban.com/subject_search') && ($('cat')[0].value = '1003');
   }
  }, false);
  $('#db_scr_btm').addEventListener('dblclick', function(e){if(!e.target.id){$('ssform')[0].submit();}}, false);
 }
};

// 顶部滑动导航条
function slideBar(){
 if($('.top-nav-items')[0] && GM_getValue('slideBar', true)){
  var name = getUserName();
  
  //添加我的豆瓣
  if(name){
   var myDouban = document.createElement('li');
   myDouban.innerHTML = '<a href="http://www.douban.com/mine/" title="by 豆瓣助手">我的豆瓣</a>';
   $$('.top-nav-items ul')[0].insertBefore(myDouban, $$('.top-nav-items ul li')[0]);
  };
  
  //添加豆瓣说到导航条 因为遮挡搜索按钮暂时去除
  // if($('.top-nav-more-items')[0]){
   // $$('.top-nav-more-items ul')[0].innerHTML += '<li><a href="http://shuo.douban.com/" target="_blank" title="by 豆瓣助手">豆瓣说</a></li>';
   // $('.top-nav-more-items')[0].style.height = ($$('.top-nav-more-items ul li').length == 3) ? '78px' : '';
  // };
  
  //二级导航
  var subNavItems = document.createElement('div');
  subNavItems.innerHTML = '\
   <div id="top-nav-items">\
    '+ (name ? '\
    <ul id="我的豆瓣" style="display:none;">\
    <li><a href="http://www.douban.com/people/'+name+'/notes">日记</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/photos">相册</a></li>\
    <li><a href="http://www.douban.com/mine/discussions">讨论</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/recs">推荐</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/favorites">喜欢</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/miniblogs">广播</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/offers">二手</a></li>\
    <li><a href="http://www.douban.com/mine/doulists">豆列</a></li>\
    <li><a href="http://www.douban.com/people/'+name+'/board">留言板</a></li>\
    <li><a href="http://www.douban.com/settings/">设置</a></li>\
    </ul>\
    ' : '') + '\
    <ul id="豆瓣社区" style="display:none;">\
    <li><a href="http://www.douban.com/mine/">我的豆瓣</a></li>\
    <li><a href="http://www.douban.com/group/mine">小组分类</a></li>\
    <li><a href="http://www.douban.com/group/">我的小组</a></li>\
    <li><a href="http://www.douban.com/group/my_topics">我发起的话题</a></li>\
    <li><a href="http://www.douban.com/group/my_replied_topics">我回应的话题</a></li>\
    <li><a href="http://www.douban.com/event/">同城</a></li>\
    <li><a href="http://www.douban.com/explore/">浏览发现</a></li>\
    </ul>\
    <ul id="豆瓣读书" style="display:none;">\
    <li><a href="http://book.douban.com/mine">我读</a></li>\
    <li><a href="http://book.douban.com/recommended">豆瓣猜</a></li>\
    <li><a href="http://book.douban.com/chart">排行榜</a></li>\
    <li><a href="http://book.douban.com/tag/">分类浏览</a></li>\
    <li><a href="http://book.douban.com/review/best/">书评</a></li>\
    <li><a href="http://book.douban.com/cart">购书单</a></li>\
    </ul>\
    <ul id="豆瓣电影" style="display:none;">\
    <li>        </li>\
    <li><a href="http://movie.douban.com/tv">电视剧</a></li>\
    <li><a href="http://movie.douban.com/mine">我看</a></li>\
    <li><a href="http://movie.douban.com/chart">排行榜</a></li>\
    <li><a href="http://movie.douban.com/tag/">分类浏览</a></li>\
    <li><a href="http://movie.douban.com/review/best/">热评</a></li>\
    </ul>\
    <ul id="豆瓣音乐" style="display:none;">\
    <li>           </li>\
    <li><a href="http://music.douban.com/mine">我的音乐</a></li>\
    <li><a href="http://music.douban.com/artists/">音乐人</a></li>\
    <li><a href="http://music.douban.com/chart">排行榜</a></li>\
    <li><a href="http://music.douban.com/tag/">分类浏览</a></li>\
    <li><a target="blank" href="http://douban.fm/">豆瓣电台</a></li>\
    </ul>\
    <ul id="豆瓣同城" style="display:none;">\
    <li>                </li>\
    <li><a href="http://music.douban.com/mine">我的音乐</a></li>\
    <li><a href="http://music.douban.com/artists/">音乐人</a></li>\
    <li><a href="http://music.douban.com/chart">排行榜</a></li>\
    <li><a href="http://music.douban.com/tag/">分类浏览</a></li>\
    <li><a target="blank" href="http://douban.fm/">豆瓣电台</a></li>\
    </ul>\
    <ul id="九点" style="display:none;">\
    <li>                  </li>\
    <li><a href="http://9.douban.com/channel/culture">文化</a></li>\
    <li><a href="http://9.douban.com/channel/life">生活</a></li>\
    <li><a href="http://9.douban.com/channel/fun">趣味</a></li>\
    <li><a href="http://9.douban.com/channel/technology">科技</a></li>\
    <li><a href="http://9.douban.com/reader/">我的订阅</a></li>\
    </ul>\
    <ul id="豆瓣FM" style="display:none;">\
    <li>                          </li>\
    <li><a href="http://douban.fm/mine" target="_blank">我的电台</a></li>\
    <li><a href="http://douban.fm/app" target="_blank">应用下载</a></li>\
    </ul>\
   </div>\
  ';
  $('.top-nav-items')[0].appendChild(subNavItems);
  
  $$('.top-nav-items ul')[0].addEventListener('mouseover', function(e){
   if(e.target.href){
    setStyles($$('#top-nav-items ul'), 'display:none;');
    $('#'+e.target.innerHTML) && ($('#'+e.target.innerHTML).style.display = 'block');
   }
  }, false);
  $('#content') && $('#content').addEventListener('mouseover', function(e){
   setStyles($$('#top-nav-items ul'), 'display:none;');
  }, false);
 }
};

// 破解音乐人下载链接
function musicLink(){
 var list = $('.songlst');
 if(list[0] && GM_getValue('musicLink', true)){
  var getMusicLink = function(songlist){
   eval(songlist.parentNode.getElementsByTagName('script')[0].innerHTML.match(/(var[\s\S]+?)window/)[1]); // sdata
   var downLinks = xpath('.//tbody/tr[@class]/td[2]', songlist);
   for(var l,i=0,j=downLinks.snapshotLength; i<j; i++){
    l = downLinks.snapshotItem(i);
    l.getElementsByTagName('a').length || (l.innerHTML = '<a href="'+ atob(sdata[i]['url'])+'" title="下载:'+decodeURIComponent(sdata[i]['title'])+'   by豆瓣助手"><img src="http://t.douban.com/pics/download.gif"/></a>');
   };
  };
  for (i=0,j=list.length; i<j; i++){
   getMusicLink(list[i]);
  }
 }
};

// 留言板增强
function exBoard(){
 if(/douban\.com\/(?:[^\/]+\/[^\/]+|widget)\/board/i.test(location.href) && GM_getValue('exBoard', true)){
  if(!$('#board')){//加载下一页后不再重复处理标记
   GM_addStyle('\
    #board #bydbhp {\
     display:none;\
     float:right;\
     color:#BBBBBB;\
     font-size:12px;\
    }\
    #board:hover #bydbhp {\
     display:block;\
    }\
   ');
   var mainArea = $$('#content .indent')[0] || $$('#content .mod')[0];
   var action = /\/\/site/i.test(location.href) && (location.href.replace(/\?.*/, '') + 'add_board') || '';
   mainArea.innerHTML = '\
    <div id="board">\
     <form id="fboard" style="margin-bottom: 12px;" method="post" name="bpform" action='+ action +'>\
      <div style="display: none;"><input type="hidden" value='+ getCookie('ck') +' name="ck"></div>\
      <textarea style="width: 97%; height:100px; margin-bottom: 5px;" name="bp_text"></textarea>\
      <span class="bn-flat"><input type="submit" value=" 留言 " name="bp_submit"></span><span id="bydbhp">by豆瓣助手</span>\
      <a style="display:none;" href=# onclick="this.style.display=\'none\';var form=$(\'#fboard\');form.attr(\'action\',\'\');form.find(\'input:visible\').val(\'留言\');">点击恢复原状</a>\
     </form>\
    </div>\
   ' + mainArea.innerHTML;
  };
  
  
  for (var i=0,r=$$('.mbtrdot, .update-item .author'),l=r.length; i<l; i++){
   if(!/dbmark/.test(r[i].className)){
    r[i].innerHTML += '<span class="gact"><a title="留言到对方的留言板 by豆瓣助手" onclick="var link=this.parentNode.parentNode.getElementsByTagName(\'a\')[0];var form=$(\'#fboard\');form.attr(\'action\',link.href+\'board\');form.find(\'input:visible\').val(\'留言到\'+link.innerHTML+\'的留言板\');form.find(\'textarea\').focus();form.find(\'a\').attr(\'style\',\'\');" href=#>回复</a></span>';
    r[i].className += ' dbmark';//加载下一页后不再重复处理标记
   }
  }
 }
};

// Ctrl + Enter 回复快捷键
function ctrlEnter(){
 var form = document.forms;
 for (var i=0,j=form.length; i<j; i++){
  form[i].addEventListener('keydown', function(e){
   if ( e.ctrlKey && e.keyCode == '13'){
    this.submit();
   }
  }, false);
 }
};

// 通用页面加载函数
/*
 islimit:true为单页,false为全部(可选,默认全部)
 func:执行函数(可选)
 tarXpath:目标加载位置(可选)
 nextLink:下一页的链接(可选,默认paginator后页)
 baseDOM:下一页的链接的母体(可选,默认当前document)
*/
function loadPage(islimit, func, tarXpath, nextLink, baseDOM){
 var nextLink = nextLink || './/div[@class="paginator" and not(@id)]/span[@class="next"]/a';
 var link = xpath(nextLink, baseDOM||document).snapshotItem(0);//需要加载的页面链接
 window.autoLoading = true;//开始加载标记
 
 // 友邻页面特殊处理:避免重复捆绑友邻事件,修复点击回应打开新页面的BUG 第一步,第二步重新捆绑时间在加载下一页后执行
 if($('#db-timeline-hd')){
  var obj = $$('.j');
  for (var i=0,j=obj.length; i<j; i++){
   obj[i].className = obj[i].className.replace(/^j\s/, '');
  };
 };
 
 if(link){
  // 下一页加载中提示
  $('#nextPageLoder') && ($('#nextPageLoder').innerHTML = '努力加载中<img border="0" src="data:image/gif;base64,R0lGODlhCgAKAJEDAMzMzP9mZv8AAP///yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFAAADACwAAAAACgAKAAACF5wncgaAGgJzJ647cWua4sOBFEd62VEAACH5BAUAAAMALAEAAAAIAAMAAAIKnBM2IoMDAFMQFAAh+QQFAAADACwAAAAABgAGAAACDJwHMBGofKIRItJYAAAh+QQFAAADACwAAAEAAwAIAAACChxgOBPBvpYQYxYAIfkEBQAAAwAsAAAEAAYABgAAAgoEhmPJHOGgEGwWACH5BAUAAAMALAEABwAIAAMAAAIKBIYjYhOhRHqpAAAh+QQFAAADACwEAAQABgAGAAACDJwncqi7EQYAA0p6CgAh+QQJAAADACwHAAEAAwAIAAACCpRmoxoxvQAYchQAOw==">');
  
  //加载页面
  GM_xmlhttpRequest({
   method: 'GET',
   url: link.href,
   onload: function(xhr){
    var nextPageDOM = document.createElement('div');
    nextPageDOM.innerHTML = xhr.responseText;
    
    //函数内嵌、还是随其他函数而定
    tarXpath = tarXpath || 
     ($$('#miniblog')[0] && './/*[@id="miniblog"]')||//友邻
     ($$('ul.topic-reply, div#comments, div.post-comments')[0] && './/ul[contains(@class,"topic-reply")]|.//div[@id="comments" or @class="post-comments"]')||//小组 、 书影音评论 、 阿尔法城
     ($$('.photolst, .event-photo-list, .poster-col4')[0] && './/*[@class="photolst clearfix" or @class="photolst clearbox" or @class="event-photo-list" or @class="poster-col4 clearfix"]')||//相册、活动,  小站、阿尔法城图片墙, 剧照、海报
     ($$('table.olt, table.list-b')[0] && './/table[@class="olt" or @class="list-b"]/tbody')||//邮箱、论坛列表、阿尔法城话题列表
     ($$('.clearfix>div.article')[0] && './/div[contains(@class,"clearfix")]/div[@class="article"]')||//豆单、留言板、单页相册 等等通用模块
     ($$('body')[0] && './/body');//最后必杀
     // alert(tarXpath)
    
    //将加载的下一页内容
    var theTar = xpath(tarXpath, nextPageDOM).snapshotItem(0);
    
    if($$('div.post-comments, table.olt, table.list-b')[0]){ //使用appendChild操作会导致嵌套样式变化的: 阿尔法城、邮箱、阿尔法城话题列表等
    
     // 方法1:innerHTML操作,没有父节点嵌套但丢失捆绑事件 且加载多页后重新排版消耗资源较多
     // xpath(tarXpath).snapshotItem(0).innerHTML += theTar.innerHTML;
     
     // 方法2: insertAfter,没有嵌套,但可能由于节点排序与xpath规则不一致导致加载混乱
     insertAfter(theTar.cloneNode(true), xpath(tarXpath).snapshotItem(xpath(tarXpath).snapshotLength - 1));
    }else{
    
     // 方法3:带有父节点嵌套的DOM操作,保留捆绑事件但造成结构嵌套
     xpath(tarXpath).snapshotItem(0).appendChild(theTar.cloneNode(true));
    };
    
    // 恢复加载按钮文本
    $('#nextPageLoder') && ($('#nextPageLoder').innerHTML = '加载下一页∨');
    
    // 若加载页面含有翻页控件,则删除前一个,以保持翻页控件唯一
    var paginator = $('.paginator'), theParent = paginator[0] && paginator[0].parentNode;
    xpath('.//div[@class="paginator"]', theTar).snapshotItem(0) && theParent && theParent.removeChild(paginator[0]);
    
    if( !islimit && xpath(nextLink, nextPageDOM).snapshotItem(0)){// 若无限制,继续加载下一页
     setTimeout(function(){
      var pageCount = Number(window.pageCount || 0);
      if(pageCount < GM_getValue('loadPageNum', 10)-2){ //pageCount<N,即加载后续N+2页
       window.pageCount = pageCount+1;
       loadPage(islimit, func, tarXpath, nextLink, nextPageDOM);
      }else{
       window.pageCount = 0;
       loadPage(true, func, tarXpath, nextLink, nextPageDOM);
      };
     },500);
     return;
    }else{//加载完毕
     
     //若加载内容没有翻页控件,则更新当前页面的翻页控件
     if(!xpath('.//div[@class="paginator"]', theTar).snapshotItem(0)){
      paginator = $('.paginator'), theParent = paginator[0] && paginator[0].parentNode;
      theParent && theParent.removeChild(paginator[0]);
      theParent && theParent.appendChild(
       xpath('.//div[@class="paginator"]', nextPageDOM).snapshotItem(0)
      );
     };
     
     //若有第二个翻页控件,和第一个同步刷新 出现未知错误,待查
     paginator = $('.paginator');//原集合可能已经更改,重新获取
     paginator[1] && (paginator[1].innerHTML = paginator[0].innerHTML);
     
     //执行绑定操作
     !!func && func();
     
     // 检查是否还有下一页
     if(!$$('.paginator:not(#secPaginator)>.next>a')[0] && $('#nextPageLoder')){
      $('#nextPageLoder').style.background = '#fff';
      $('#nextPageLoder').textContent = '已经是最后一页';
     };
     
     // 修复Tampermonkey怪异BUG
     $('#TM_tmp_xpath') && ($('#TM_tmp_xpath').style.display = 'none');
     
     // 重新捆绑友邻事件,修复点击回应打开新页面的BUG
     $('#db-timeline-hd') && (location.href = 'javascript:$(function(){load_event_monitor(document)});void(0);');
     
     
     // 去除加载下一页后重复的内容
     var removeDouble = function(x){
      var obj = xpath(x).snapshotItem(1);//用xpath比CSS选择符能避免重复ID选择不能问题
      obj && (obj.parentNode.removeChild(obj));
      xpath(x).snapshotItem(1) && needRemove(x);
     };
     removeDouble('.//*[@id="db-talk"]');//豆瓣说部分
     removeDouble('.//*[@id="db-dlg-reclink"]');//豆瓣说部分
     removeDouble('.//*[@id="db-timeline-hd"]');//豆瓣说部分
     removeDouble('.//div[@class="col2_doc clearfix"]');//豆列
     
     //加载完标记
     window.autoLoading = false;
    }
   }
  });
 };
};

// 页面加载按钮
function addPageLoader(){
 var paginator = $('.paginator')[0];
 if(paginator && GM_getValue('addPageLoader', true)){
  
  //加载按钮样式
  GM_addStyle('.btn-donate{display:inline-block;*display:inline;zoom:1;padding:0 8px;vertical-align:middle;*vertical-align:baseline;height:19px;line-height:19px;line-height:21px\9;overflow:hidden;border:1px solid#edceba;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px}a.btn-donate:link,a.btn-donate:visited{border-color:#f2ece7;background-color:#fff6ee;color:#99776b}a.btn-donate:hover,a.btn-donate:active{border-color:#edceba;background-color:#fff0e2;color:#99776b}');
  
  //第一个翻页控件下增加加载按钮
  var next = document.createElement('span');
  next.id = 'pageLoader';
  next.innerHTML = '<br><a id="nextPageLoder" style="text-align:center;width:80%;margin:0;font:12px/162% Arial,Helvetica,sans-serif;" href="#nothing" title="将下一页加载到当前页面" class="btn-donate">加载下一页∨</a>'
   + '<a id="allPageLoder" style="text-align:center;width:15%;margin:0;font:12px/162% Arial,Helvetica,sans-serif;" href="#nothing" title="在本页面合并显示多个个分页,页数可自行设置" class="btn-donate">加载多个分页</a>';
  $('#nextPageLoder') || paginator.appendChild(next);
  
  if($('#nextPageLoder')){
   
   // 加载下一页
   $('#nextPageLoder').addEventListener('click', function(){
    loadPage(true, runHelper);
   }, false);
   
   // 加载多个分页
   $('#allPageLoder').addEventListener('click', function(){
    if(!!$$('.paginator>span.break')[0]){//超过10页
     if(!window.loadPageConfirm){//本页未提示过
      window.loadPageConfirm = true;
      confirm('检测到分页较多,可能耗时较长,是否加载?\n(当前设定为每次加载'+GM_getValue('loadPageNum', 10)+'页,可点击“豆瓣助手”设置)') && loadPage(false, runHelper);
     }else{//本页已提示过
      loadPage(false, runHelper);
     };
    }else{
     loadPage(false, runHelper);
    };
   }, false);
   
   // 自动加载下一页
   GM_getValue('autoLoadPage', true) && document.body.addEventListener(navigator.userAgent.indexOf('Chrome')>0 ? 'mousewheel' : 'DOMMouseScroll', function(){//firefox使用DOMMouseScroll chrome使用mousewheel
    if($('#pageLoader').offsetTop - window.scrollY <= window.innerHeight && $$('.paginator:not(#secPaginator)>.next>a')[0] && window.autoLoading !== true){// 滚动到加载控件所在位置,存在下一页,且不在加载中,则自动加载
     loadPage(true, runHelper);
    };
   }, false);
  }
 }
};

// 无尽处理下一页
// func 对下一页dom所进行的操作函数;noNextPageFunc 当前页面为最终页面的操作函数;dom初始操作对象
function endlessPage(func, noNextPageFunc, dom){
 var nextPage = $$('.paginator>.next>a', dom||document)[0];
 if(nextPage){
  GM_xmlhttpRequest({
   method: 'GET',
   url: nextPage.href,
   onload: function(resp){
    if(resp.status < 200 || resp.status > 300) {return;};
    var nextPageDOM = document.createElement('div');
    nextPageDOM.innerHTML = resp.responseText;
    
    func(nextPageDOM);
    
    var andNextPage = $$('.paginator>.next>a', nextPageDOM)[0];
    if(andNextPage){
    endlessPage(func, noNextPageFunc, nextPageDOM);
    }else{
     noNextPageFunc();
    }
   },
   onerror: function(){return;}
  })
 }else{
  noNextPageFunc();
 }
};

//置顶翻页导航条
function addTopPager(){
 $('.paginator')[0] && $$('.aside>*')[0] && ($$('.aside>*')[0].innerHTML += ('<div class="paginator" id="secPaginator">' + $('.paginator')[0].innerHTML + '</div>'));
};

// 彩蛋(Konami Code):在非豆瓣页面依次按下 ↑↑↓↓←→←→ 键,可在新标签中打开豆瓣
function openDouban(){
 var k=[];
 document.addEventListener('keydown', function(e){
  k.push(e.keyCode);
  if(k.toString().indexOf('38,38,40,40,37,39,37,39')>=0){
     GM_openInTab('http://www.douban.com/');
     k=[];
  }
 }, false); 
};

0 comments:

Post a Comment