// ==UserScript== // @name iZhihu // @namespace https://github.com/unogz/izhihu // @version 2.10.19 // @description 知乎插件 // @match http://www.zhihu.com/* // @include http://www.zhihu.com/* // @require http://izhihu.unogz.com/dist/import/jquery-1.8.2.min.js // @require http://izhihu.unogz.com/dist/import/jquery.checkbox.min.js // @require http://izhihu.unogz.com/dist/import/clientutils.js // @require http://izhihu.unogz.com/dist/import/drag.js // @require http://izhihu.unogz.com/dist/import/purl.js // @require http://izhihu.unogz.com/dist/import/underscore-min.js // @icon http://izhihu.unogz.com/dist/logo.png // @copyright 2013+, @钢盅郭子 @刘勇 @罗大睿 // @updateinfo 发布日期 2.10 @2013-9-10|# 改进「首页」:增加「首页分栏」模式、修正额外「收起」按钮|# 改进「右舷评论」:为特性「关闭时自动卷屏至对应条目」提供可选开关|# 改进「快速收藏」:增加操作状态指示|#修复「快速屏蔽」 // @downloadURL none // ==/UserScript== var version='2.10.19'; var updateDate='2014-8-1'; if ( typeof unsafeWindow === "undefined") { unsafeWindow = ( function () { var dummyElem = document.createElement('p'); dummyElem.onclick=function(){return window;}; return dummyElem.onclick (); } ) (); } //主入口 //$(function main(){ /** * @class Utils 辅助类 */ function utils(){ }; var cfgDefault = { 'comment_sidebar': true , 'answer_orderByTime': false , 'AuthorList':false , 'ShowComment':true , 'HomeLayout':false , 'QuickFavo':true , 'QuickFavoPinned':{} , 'AuthorRear':false , 'HomeNoti':false , 'QuickBlock':false , 'Noti7':true , 'HomeFeedsColumns':false , 'RightComment_AutoScrollPageWhenClosing':true }; /** * 读取配置 */ utils.getCfg = function(key){ if(!key)return false; var cfg = $.extend(cfgDefault, this.getValue('izhihu',cfgDefault)); return key ? cfg[key] : cfg; }; utils.setCfg = function(key,value){ if(!key)return; var cfg = $.extend(cfgDefault, this.getValue('izhihu',cfgDefault)); cfg[key]=value; this.setValue('izhihu',cfg); }; /** * 读取存储 */ utils.getValue = function(key, defaultValue){ var v=unsafeWindow.localStorage[key]; if(v) return JSON.parse(v); else return defaultValue; }; /** * 写入存储 */ utils.setValue = function(key, value){ unsafeWindow.localStorage[key] = JSON.stringify(value); }; /** * 删除存储 */ utils.deleteValue = function(key){ return delete unsafeWindow.localStorage[key]; }; utils.transferOldCfg=function(){ var oldHomeLayout = unsafeWindow.localStorage['izh_HomeLayout'] , oldAuthorList = unsafeWindow.localStorage['izh_AuthorList'] , oldShowComment = unsafeWindow.localStorage['izh_ShowComment'] , oldQuickFavo = unsafeWindow.localStorage['izh_QuickFavo'] , oldAuthorRear = unsafeWindow.localStorage['izh_AuthorRear'] , oldHomeNoti = unsafeWindow.localStorage['izh_HomeNoti'] ; if(oldHomeLayout){ izhHomeLayout=oldHomeLayout; unsafeWindow.localStorage.removeItem('izh_HomeLayout'); this.setCfg('HomeLayout',izhHomeLayout); } if(oldAuthorList){ izhAuthorList=oldAuthorList; unsafeWindow.localStorage.removeItem('izh_AuthorList'); this.setCfg('AuthorList',izhAuthorList); } if(oldShowComment){ izhRightComment=oldShowComment; unsafeWindow.localStorage.removeItem('izh_ShowComment'); this.setCfg('ShowComment',izhRightComment); } if(oldQuickFavo){ izhQuickFavo=oldQuickFavo; unsafeWindow.localStorage.removeItem('izh_QuickFavo'); this.setCfg('QuickFavo',izhQuickFavo); } if(oldAuthorRear){ izhAuthorRear=oldAuthorRear; unsafeWindow.localStorage.removeItem('izh_AuthorRear'); this.setCfg('AuthorRear',izhAuthorRear); } if(oldHomeNoti){ izhHomeNoti=oldHomeNoti; unsafeWindow.localStorage.removeItem('izh_HomeNoti'); this.setCfg('HomeNoti',izhHomeNoti); } }; /** * @method formatStr * * 格式化字符串模版,支持2种格式: * * formatStr("i can speak {language} since i was {age}",{language:'javascript',age:10}); * formatStr("i can speak {0} since i was {1}",'javascript',10); * * 如果不希望被转义,则用两个括号,如: `formatStr("i can speak {0} since i was {{1}",'javascript',10);` * */ utils.formatStr = function(tpl,obj){ obj = typeof obj === 'object' ? obj : Array.prototype.slice.call(arguments, 1); return tpl.replace(/\{\{|\}\}|\{(\w+)\}/g, function (m, n){ if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return obj[n]; }); }; utils.getParamInQuery=function(queryStr,paramName){ var param=paramName+'=' , start=queryStr.indexOf(param)+param.length , end=queryStr.indexOf('&',start); return end') , iPathAnswers=_path.indexOf('/answers') , iPathCollection=_path.indexOf('/collection') ; pageIs.Home='/'==_path; pageIs.Answer=0<_path.indexOf('/answer/'); pageIs.Question=!pageIs.Answer&&0==_path.indexOf('/question/'); pageIs.Answers=0div:last-child,.zm-item-answer:last-child { border-bottom:1px solid #EEE!important }' ,'' ,'.zu-autohide,' ,'.zm-comment-op-link,' ,'.zm-side-trend-del,' ,'.unpin {' ,' visibility:visible!important;' ,' opacity:0;' ,'}' ,'.feed-item:hover .zu-autohide,' ,'.zm-item-answer .zu-autohide,' ,'.zm-item-comment:hover .zm-comment-op-link,' ,'.zm-side-trend-row:hover .zm-side-trend-del,' ,'.zm-side-nav-li:hover .unpin {' ,' opacity:1;' ,'}' ,'.zm-item-vote-count:hover,.zm-votebar button:hover{' ,' background:#a6ce56!important;' ,' color:#3E5E00 !important' ,'}' ,'' ,'a,a:hover,' ,'i,' ,'.zu-autohide,' ,'.zm-votebar button,' ,'.zm-item-comment:hover .zm-comment-op-link,' ,'.zm-comment-op-link,' ,'.zm-side-trend-row:hover .zm-side-trend-del,' ,'.zm-side-trend-del,' ,'.zm-side-nav-li,' ,'.zu-main-feed-fresh-button,' ,'.zg-icon,' ,'.zm-side-nav-li:hover .zg-icon,' ,'.zm-side-nav-li:hover i,' ,'.unpin,' ,'.zm-side-nav-li:hover .unpin {' ,' -moz-transition:color .2s linear,opacity .15s linear,background-color .2s linear,background-position .2s linear .1s;' ,' -webkit-transition:color .2s linear,opacity .15s linear,background-color .2s linear,background-position .2s linear .1s;' ,' transition:color .2s linear,opacity .15s linear,background-color .2s linear,background-position .2s linear .1s;' ,'}' ,'' ,'h3{ line-height:25px }' ,'.zu-footer-inner {padding:15px 0!important}' ,'.zm-side-pinned-topics .zm-side-nav-li{float:left;padding-right:30px!important}' ,'.zm-side-list-content{clear:both}' ,'.unpin{ display:inline-block!important }' ,''].join('\n'); } var css_comment=''; if(pageIs.Home||pageIs.Question||pageIs.Answer){ css_comment = window.iZhihu.Comment.css; } if(window.iZhihu.QuickFavo){ css += window.iZhihu.QuickFavo.css; } if(window.iZhihu.QuickBlock){ css += window.iZhihu.QuickBlock.css; } if(window.iZhihu.Noti7){ css += window.iZhihu.Noti7.css; window.iZhihu.Noti7.enhance(); } var heads = _doc.getElementsByTagName("head"); if (heads.length > 0) { var node = _doc.createElement("style"); node.type = "text/css"; node.id = "izhCSS_main"; node.appendChild(_doc.createTextNode(css)); heads[0].appendChild(node); if(css_comment!=''){ node = _doc.createElement("style") node.type = "text/css"; node.id = "izhCSS_comment"; node.appendChild(_doc.createTextNode(css_comment)); heads[0].appendChild(node); } } if(!$('.modal-dialog-bg').length){ $body.append(''); } window.iZhihu.getItem=function($c){ var $item=$(null); if($c && $c.length){ var $itemMeta=$c.closest('.zm-item-meta'); if ($itemMeta.is('.feed-meta') || $itemMeta.parent().is('.feed-meta')){ $item=$c.closest('.feed-item'); $item.attr('data-aid', $item.children('meta[itemprop=answer-id]').attr('content')) }else if($itemMeta.is('.answer-actions')){ $item=$c.closest('.zm-item-answer,.feed-item'); }else{ $item=$itemMeta.prev(); } } return $item; }; /** * @class Answer */ function Answer(iZhihu) { if ( typeof iZhihu === 'undefined' || !iZhihu) { return null; } iZhihu.Answer = this; this._e=null; this.ppWidth=0; this.ppHeight=400; this.processAnswer=function($a,$pp,bAuthorRear,bAuthorList){ if(!$a||!$a.length)return; if($a.attr('izh_processed')=='1')return; var selCollapse='.meta-item[name=collapse]' , $meta=$a.find('.zm-item-meta') , $author=$a.find('.zm-item-answer-author-info') , $favo=$a.find('.meta-item[name=favo]') , $fold=!$a.has('.zh-summary > .toggle-expand').length?null:$('