知乎增强

移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链

当前为 2021-03-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 知乎增强
  3. // @version 1.3.3
  4. // @author X.I.U
  5. // @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
  6. // @match *://www.zhihu.com/*
  7. // @match *://zhuanlan.zhihu.com/*
  8. // @icon https://static.zhihu.com/heifetz/favicon.ico
  9. // @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
  10. /* globals $ */
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_unregisterMenuCommand
  13. // @grant GM_openInTab
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_notification
  17. // @license GPL-3.0 License
  18. // @run-at document-end
  19. // @namespace https://greasyfork.org/scripts/412205
  20. // ==/UserScript==
  21.  
  22. var menu_ALL = [
  23. ['menu_collapsedAnswer', '一键收起回答', '一键收起回答', true],
  24. ['menu_collapsedNowAnswer', '收起当前回答/评论(点击两侧空白处)', '收起当前回答/评论', true],
  25. ['menu_backToTop', '快捷回到顶部(右键两侧空白处)', '快捷回到顶部', true],
  26. ['menu_publishTop', '置顶显示时间', '置顶显示时间', true],
  27. ['menu_allTime', '完整显示时间', '完整显示时间', true],
  28. ['menu_typeTips', '区分问题文章', '区分问题文章', true],
  29. ['menu_directLink', '默认站外直链', '默认站外直链', true]
  30. ], menu_ID = [];
  31. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  32. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  33. }
  34. registerMenuCommand();
  35.  
  36. // 注册脚本菜单
  37. function registerMenuCommand() {
  38. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  39. for (let i=0;i<menu_ID.length;i++){
  40. GM_unregisterMenuCommand(menu_ID[i]);
  41. }
  42. }
  43. for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
  44. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  45. menu_ID[i] = GM_registerMenuCommand(`[ ${menu_ALL[i][3]?'√':'×'} ] ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
  46. }
  47. menu_ID[menu_ID.length] = GM_registerMenuCommand('反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});});
  48. }
  49.  
  50. // 菜单开关
  51. function menu_switch(menu_status, Name, Tips) {
  52. if (menu_status == 'true'){
  53. GM_setValue(`${Name}`, false);
  54. GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  55. }else{
  56. GM_setValue(`${Name}`, true);
  57. GM_notification({text: `已开启 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  58. }
  59. registerMenuCommand(); // 重新注册脚本菜单
  60. };
  61.  
  62. // 返回菜单值
  63. function menu_value(menuName) {
  64. for (let menu of menu_ALL) {
  65. if (menu[0] == menuName) {
  66. return menu[3]
  67. }
  68. }
  69. }
  70.  
  71.  
  72. // 一键收起回答
  73. function collapsedAnswer(){
  74. if(menu_value('menu_collapsedAnswer')){
  75. let button_Add = `<button id="collapsed-button" data-tooltip="收起回答" data-tooltip-position="left" data-tooltip-will-hide-on-click="false" aria-label="收起回答" type="button" class="Button CornerButton Button--plain"><svg class="ContentItem-arrowIcon is-active" aria-label="收起回答" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg></button>`
  76. let style_Add = document.createElement('style');
  77. style_Add.innerHTML = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}';
  78. document.head.appendChild(style_Add);
  79. $(".CornerAnimayedFlex").prepend(button_Add);
  80. $("#collapsed-button").on("click", function () {
  81. document.querySelectorAll('.ContentItem-rightButton').forEach(function (el) {
  82. if (el.hasAttribute('data-zop-retract-question')) {
  83. el.click()
  84. }
  85. });
  86. })
  87. }
  88. }
  89.  
  90.  
  91. // 收起当前回答、评论(监听点击事件,点击网页两侧空白处)
  92. function collapsedNowAnswer(selectors){
  93. backToTop(selectors)
  94. if(menu_value('menu_collapsedNowAnswer')){
  95. document.querySelector(selectors).onclick = function(event){
  96. if (event.target==this) {
  97. let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom')
  98. if(rightButton) { // 悬浮的 [收起回答](此时正在浏览回答内容 [头部区域 + 中间区域])
  99. // 固定的 [收起评论](先看看是否展开评论)
  100. let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel')
  101. if(commentCollapseButton && commentCollapseButton.innerText.indexOf("收起评论") > -1) {
  102. commentCollapseButton.click();
  103. }
  104. // 再去收起回答
  105. rightButton = rightButton.querySelector('.ContentItem-rightButton')
  106. if(rightButton && rightButton.hasAttribute('data-zop-retract-question')) {
  107. rightButton.click();
  108. }
  109. }else{ // 固定的 [收起回答](此时正在浏览回答内容 [尾部区域])
  110. for (let el of document.querySelectorAll('.ContentItem-rightButton')) {
  111. if (el.hasAttribute('data-zop-retract-question')) {
  112. if (isElementInViewport(el)) {
  113. // 固定的 [收起评论](先看看是否展开评论)
  114. let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel')
  115. if(commentCollapseButton && commentCollapseButton.innerText.indexOf("收起评论") > -1) {
  116. commentCollapseButton.click();
  117. }
  118. el.click() // 再去收起回答
  119. break
  120. }
  121. }
  122. }
  123. }
  124.  
  125. var commentCollapseButton_ = false;
  126. // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
  127. let commentCollapseButton = document.querySelector('.CommentCollapseButton')
  128. if(commentCollapseButton) {
  129. commentCollapseButton.click();
  130. }else{ // 固定的 [收起评论](此时正在浏览评论内容 [头部区域])
  131. for (let el of document.querySelectorAll('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel')) {
  132. if (el.innerText.indexOf("收起评论") > -1) {
  133. if (isElementInViewport(el)) {
  134. el.click()
  135. commentCollapseButton_ = true // 如果找到并点击了,就没必要执行下面的代码了(可视区域中没有 [收起评论] 时)
  136. break
  137. }
  138. }
  139. }
  140. if(commentCollapseButton_ == false){ // 可视区域中没有 [收起评论] 时(此时正在浏览评论内容 [头部区域] + [尾部区域](不上不下的,既看不到固定的 [收起评论] 又看不到悬浮的 [收起评论])),需要判断可视区域中是否存在评论元素
  141. for (let el of document.querySelectorAll('.NestComment')) {
  142. if (isElementInViewport(el)) {
  143. let commentCollapseButton = el.parentNode.parentNode.parentNode.parentNode.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel')
  144. if (commentCollapseButton.innerText.indexOf("收起评论") > -1) {
  145. commentCollapseButton.click()
  146. break
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157.  
  158. // 回到顶部(监听点击事件,鼠标右键点击网页两侧空白处)
  159. function backToTop(selectors){
  160. if(menu_value('menu_backToTop')){
  161. document.querySelector(selectors).oncontextmenu = function(event){
  162. if (event.target==this) {
  163. event.preventDefault();
  164. window.scrollTo(0,0)
  165. }
  166. }
  167. }
  168. }
  169.  
  170.  
  171. //获取元素是否在可视区域
  172. function isElementInViewport(el) {
  173. let rect = el.getBoundingClientRect();
  174. return (
  175. rect.top >= 0 &&
  176. rect.left >= 0 &&
  177. rect.bottom <=
  178. (window.innerHeight || document.documentElement.clientHeight) &&
  179. rect.right <=
  180. (window.innerWidth || document.documentElement.clientWidth)
  181. );
  182. }
  183.  
  184.  
  185. var postNum;
  186. // 区分问题文章
  187. function addTypeTips() {
  188. if(menu_value('menu_typeTips')){
  189. // URL 匹配正则表达式
  190. let patt_zhuanlan = /zhuanlan.zhihu.com/,
  191. patt_question = /question\/\d+/,
  192. patt_question_answer = /answer\/\d+/,
  193. patt_video = /\/zvideo\//,
  194. patt_tip = /zhihu_e_tips/
  195. let postList = document.querySelectorAll('h2.ContentItem-title a');
  196. postNum = document.querySelectorAll('small.zhihu_e_tips');
  197. //console.log(`${postList.length} ${postNum.length}`)
  198. if (postList.length > postNum.length){
  199. for(let num = postNum.length;num<postList.length;num++){
  200. if (!patt_tip.test(postList[num].innerHTML)){ // 判断是否已添加
  201. if (patt_zhuanlan.test(postList[num].href)){ // 如果是文章
  202. postList[num].innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #2196F3;display: inline-block;height: 18px;">文章</small> ` + postList[num].innerHTML
  203. }else if (patt_question.test(postList[num].href)){ // 如果是问题
  204. if (patt_question_answer.test(postList[num].href)){ // 如果是指向回答的问题(而非指向纯问题的链接)
  205. postList[num].innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #f68b83;display: inline-block;height: 18px;">问题</small> ` + postList[num].innerHTML
  206. }else{
  207. postList[num].innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #ff5a4e;display: inline-block;height: 18px;">问题</small> ` + postList[num].innerHTML
  208. }
  209. }else if (patt_video.test(postList[num].href)){ // 如果是视频
  210. postList[num].innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #00BCD4;display: inline-block;height: 18px;">视频</small> ` + postList[num].innerHTML
  211. }
  212. //postNum += 1;
  213. }
  214. }
  215. }
  216. }
  217. }
  218.  
  219.  
  220. // 监听 网页插入元素 事件
  221. function addEventListener_DOMNodeInserted() {
  222. // 知乎免登录,来自:https://greasyfork.org/zh-CN/scripts/417126
  223. let removeLoginModal = e => {
  224. if (e.target.innerHTML && e.target.getElementsByClassName('Modal-wrapper').length > 0) {
  225. if (e.target.getElementsByClassName('Modal-wrapper')[0].querySelector('.signFlowModal')){
  226. e.target.getElementsByClassName('Modal-wrapper')[0].remove();
  227. }
  228. setTimeout(() => {document.documentElement.style.overflowY = 'scroll';}, 0);
  229. }
  230. }
  231.  
  232. // 收起当前评论(监听点击事件,点击网页两侧空白处)
  233. let collapseNowComment = e => {
  234. if (e.target.innerHTML && e.target.getElementsByClassName('Modal-wrapper Modal-enter').length > 0) {
  235. document.getElementsByClassName('Modal-backdrop')[0].onclick = function(event){
  236. if (event.target==this) {
  237. let closeButton = document.getElementsByClassName('Modal-closeButton')[0]
  238. if(closeButton) {
  239. closeButton.click();
  240. }
  241. }
  242. }
  243. }
  244. }
  245.  
  246. if (document.querySelector('button.AppHeader-login')){ // 未登录时才会监听并移除登录弹窗
  247. document.addEventListener('DOMNodeInserted', removeLoginModal);
  248. document.querySelector('button.AppHeader-login').onclick=function(){location.href='https://www.zhihu.com/signin';} // [登录]按钮跳转至登录页面
  249. document.querySelector('.AppHeader-profile button.Button--primary').onclick=function(){location.href='https://www.zhihu.com/signin';} // [加入知乎]按钮跳转至注册页面(实际上是同一个页面)
  250. } else if(window.location.href.indexOf("zhuanlan") > -1){
  251. document.addEventListener('DOMNodeInserted', removeLoginModal);
  252. }
  253. document.addEventListener('DOMNodeInserted', collapseNowComment); // 收起当前评论(监听点击事件,点击网页两侧空白处)
  254. }
  255.  
  256.  
  257. // 监听 XMLHttpRequest 事件
  258. function EventXMLHttpRequest() {
  259. var _send = window.XMLHttpRequest.prototype.send
  260. function sendReplacement(data) {
  261. //console.log(`111111`);
  262. addTypeTips();
  263. return _send.apply(this, arguments);
  264. }
  265. window.XMLHttpRequest.prototype.send = sendReplacement;
  266. }
  267.  
  268. /*(function (open) {
  269. XMLHttpRequest.prototype.open = function () {
  270. this.addEventListener("readystatechange", function () {
  271. //console.log(this.responseURL);
  272. }, false);
  273. open.apply(this, arguments);
  274. };
  275. })(XMLHttpRequest.prototype.open);*/
  276.  
  277.  
  278. // 置顶显示时间 - 首页,来自:https://greasyfork.org/scripts/402808
  279. function topTime_index()
  280. {
  281. $(".TopstoryItem").each(function(){
  282. if( !($(this).find(".ContentItem-time").hasClass("full")) && $(this).find(".ContentItem-time").length>0 && $(this).find(".ContentItem-time").find("span").text() != null)
  283. {
  284. // 完整显示时间
  285. if(menu_value('menu_allTime'))
  286. {
  287. if($(this).find(".ContentItem-time").text().indexOf("发布于")==-1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") > -1) //只有"编辑于"时增加具体发布时间data-tooltip
  288. {
  289. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  290. var oldtext =$(this).find(".ContentItem-time").find("span").text();
  291. $(this).find(".ContentItem-time").find("span").text(data_tooltip+","+oldtext);
  292. $(this).find(".ContentItem-time").addClass("full");
  293. }
  294. else if($(this).find(".ContentItem-time").text().indexOf("发布于") > -1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") == -1) //只有"发布于"时替换为具体发布时间data-tooltip
  295. {
  296. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  297. $(this).find(".ContentItem-time").find("span").text(data_tooltip);
  298. $(this).find(".ContentItem-time").addClass("full");
  299. }
  300. }
  301.  
  302. //发布时间置顶
  303. if(menu_value('menu_publishTop'))
  304. {
  305. if(!$(this).find(".ContentItem-time").parent().hasClass("ContentItem-meta"))
  306. {
  307. let temp_time = $(this).find(".ContentItem-time").clone();
  308. $(this).find(".RichContent .ContentItem-time").hide();
  309. $(this).find(".ContentItem-meta").append(temp_time);
  310. }
  311.  
  312. }
  313. }
  314.  
  315. })
  316. }
  317.  
  318.  
  319. // UTC 标准时转 UTC+8 北京时间,来自:https://greasyfork.org/zh-CN/scripts/402808
  320. function getUTC8 (datetime) {
  321. let month = (datetime.getMonth() + 1) < 10 ? "0" + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
  322. let date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
  323. let hours = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
  324. let minutes = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
  325. let seconds = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
  326. return (datetime.getFullYear() + "-" + month + "-" + date + "\xa0\xa0" + hours + ":" + minutes + ":" + seconds);
  327. }
  328.  
  329.  
  330. // 置顶显示时间 - 回答页,来自:https://greasyfork.org/scripts/402808
  331. function topTime_question()
  332. {
  333. //回答的发布时间
  334. $(".ContentItem.AnswerItem").each(function(){
  335. if( !($(this).find(".ContentItem-time").hasClass("full")) && $(this).find(".ContentItem-time").length>0 && $(this).find(".ContentItem-time").find("span").text() != null)
  336. {
  337. // 完整显示时间
  338. if(menu_value('menu_allTime'))
  339. {
  340. if($(this).find(".ContentItem-time").text().indexOf("发布于")==-1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") > -1) //只有"编辑于"时增加具体发布时间data-tooltip
  341. {
  342. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  343. var oldtext =$(this).find(".ContentItem-time").find("span").text();
  344. $(this).find(".ContentItem-time").find("span").text(data_tooltip+","+oldtext);
  345. $(this).find(".ContentItem-time").addClass("full");
  346. }
  347. else if($(this).find(".ContentItem-time").text().indexOf("发布于") > -1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") == -1) //只有"发布于"时替换为具体发布时间data-tooltip
  348. {
  349. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  350. $(this).find(".ContentItem-time").find("span").text(data_tooltip);
  351. $(this).find(".ContentItem-time").addClass("full");
  352. }
  353. }
  354.  
  355. //发布时间置顶
  356. if(menu_value('menu_publishTop'))
  357. {
  358. if(!$(this).find(".ContentItem-time").parent().hasClass("ContentItem-meta"))
  359. {
  360. let temp_time = $(this).find(".ContentItem-time").clone();
  361. $(this).find(".RichContent .ContentItem-time").hide();
  362. $(this).find(".ContentItem-meta").append(temp_time);
  363. }
  364. }
  365. }
  366.  
  367. })
  368.  
  369. $(".Pc-card.Card").attr("style","display:none")
  370.  
  371. //问题创建时间,来自:https://greasyfork.org/zh-CN/scripts/402808
  372. if ($(".QuestionPage .QuestionHeader-side p").length == 0 && window.location.href.indexOf("log") == -1) //非问题日志页
  373. {
  374. let createtime = $(".QuestionPage>[itemprop~=dateCreated]").attr("content");
  375. let modifiedtime = $(".QuestionPage>[itemprop~=dateModified]").attr("content");
  376. createtime = getUTC8(new Date(createtime));
  377. modifiedtime = getUTC8(new Date(modifiedtime));
  378.  
  379. $(".QuestionPage .QuestionHeader-side").append('<div style=\"color:#8590a6; margin-top:15px\"><p>创建时间:&nbsp;&nbsp;' + createtime + '</p><p>最后编辑:&nbsp;&nbsp;' + modifiedtime + '</p></div>');
  380. }
  381. }
  382.  
  383. // 置顶显示时间 - 专栏/文章,来自:https://greasyfork.org/scripts/402808
  384. function topTime_zhuanlan()
  385. {
  386. //隐藏推荐文章
  387. $(".Recommendations-Main").hide();
  388.  
  389. // 完整显示时间
  390. if(menu_value('menu_allTime'))
  391. {
  392. if( $(".ContentItem-time").text().indexOf("编辑于")>-1 && !$(".ContentItem-time").hasClass("done"))
  393. {
  394. let bianjiyu = $(".ContentItem-time").text();
  395. $(".ContentItem-time").click();
  396. $(".ContentItem-time").text($(".ContentItem-time").text()+","+bianjiyu)
  397. $(".ContentItem-time").addClass("done");
  398. }
  399. }
  400.  
  401. //发布时间置顶
  402. if(menu_value('menu_publishTop') && $(".Post-Header").find(".ContentItem-time").length==0)
  403. {
  404. $(".ContentItem-time").css({"padding":"0px 0px 0px 0px","margin-top": "14px"});
  405. $(".ContentItem-time").appendTo($(".Post-Header"))
  406. }
  407. }
  408.  
  409. // 置顶显示时间 - 搜索结果页,来自:https://greasyfork.org/scripts/402808
  410. function topTime_search()
  411. {
  412. $(".ContentItem.AnswerItem, .ContentItem.ArticleItem").each(function(){
  413. if( !($(this).find(".ContentItem-time").hasClass("full")) && $(this).find(".ContentItem-time").length>0 && $(this).find(".ContentItem-time").find("span").text() != null)
  414. {
  415. // 完整显示时间
  416. if(menu_value('menu_allTime'))
  417. {
  418. if($(this).find(".ContentItem-time").text().indexOf("发布于")==-1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") > -1) //只有"编辑于"时,增加具体发布时间data-tooltip
  419. {
  420. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  421. var oldtext =$(this).find(".ContentItem-time").find("span").text();
  422. $(this).find(".ContentItem-time").find("span").text(data_tooltip+","+oldtext);
  423. $(this).find(".ContentItem-time").addClass("full");
  424. }
  425. else if($(this).find(".ContentItem-time").text().indexOf("发布于") > -1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") == -1) //只有"发布于"时替换为具体发布时间data-tooltip
  426. {
  427. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  428. $(this).find(".ContentItem-time").find("span").text(data_tooltip);
  429. $(this).find(".ContentItem-time").addClass("full");
  430. }
  431. }
  432.  
  433. //发布时间置顶
  434. if(menu_value('menu_publishTop'))
  435. {
  436. if(!$(this).find(".ContentItem-time").parent().hasClass("SearchItem-meta"))
  437. {
  438. let temp_time = $(this).find(".ContentItem-time").clone();
  439. $(this).find(".RichContent .ContentItem-time").hide();
  440. $(this).find(".SearchItem-meta").append(temp_time);
  441. }
  442. }
  443. }
  444.  
  445. })
  446. }
  447.  
  448. // 置顶显示时间 - 用户主页,来自:https://greasyfork.org/scripts/402808
  449. function topTime_people()
  450. {
  451. $(".ContentItem.AnswerItem, .ContentItem.ArticleItem").each(function(){
  452. if( !($(this).find(".ContentItem-time").hasClass("full")) && $(this).find(".ContentItem-time").length>0 && $(this).find(".ContentItem-time").find("span").text() != null)
  453. {
  454. // 完整显示时间
  455. if(menu_value('menu_allTime'))
  456. {
  457. if($(this).find(".ContentItem-time").text().indexOf("发布于")==-1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") > -1) //只有"编辑于"时增加具体发布时间data-tooltip
  458. {
  459. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  460. var oldtext =$(this).find(".ContentItem-time").find("span").text();
  461. $(this).find(".ContentItem-time").find("span").text(data_tooltip+","+oldtext);
  462. $(this).find(".ContentItem-time").addClass("full");
  463. }
  464. else if($(this).find(".ContentItem-time").text().indexOf("发布于") > -1 && $(this).find(".ContentItem-time").text().indexOf("编辑于") == -1) //只有"发布于"时替换为具体发布时间data-tooltip
  465. {
  466. let data_tooltip = $(this).find(".ContentItem-time").find("span").attr("data-tooltip");
  467. $(this).find(".ContentItem-time").find("span").text(data_tooltip);
  468. $(this).find(".ContentItem-time").addClass("full");
  469. }
  470. }
  471.  
  472. //发布时间置顶
  473. if(menu_value('menu_publishTop'))
  474. {
  475. if(!$(this).find(".ContentItem-time").parent().hasClass("ContentItem-meta"))
  476. {
  477. let temp_time = $(this).find(".ContentItem-time").clone();
  478. $(this).find(".RichContent .ContentItem-time").hide();
  479. $(this).find(".ContentItem-meta").append(temp_time);
  480. }
  481. }
  482. }
  483.  
  484. })
  485. }
  486.  
  487. // 默认站外直链,来自:https://greasyfork.org/scripts/402808
  488. function directLink () {
  489. var equal, colon, external_href, protocol, path, new_href;
  490. //文字链接
  491. $("a[class*=\'external\']").each(function () {
  492. if ($(this).find("span").length > 0) {
  493. new_href = $(this).text();
  494. $(this).attr("href", new_href);
  495. } else if ($(this).attr("href").indexOf("link.zhihu.com/?target=") > -1) {
  496. external_href = $(this).attr("href");
  497. new_href = external_href.substring(external_href = $(this).attr("href").indexOf("link.zhihu.com/?target=") + "link.zhihu.com/?target=".length);
  498. //console.log(`${new_href}`)
  499. $(this).attr("href", decodeURIComponent(new_href));
  500. } else {
  501. external_href = $(this).attr("href");
  502. if (external_href.lastIndexOf("https%3A")) {
  503. new_href = $(this).attr("href").substring($(this).attr("href").lastIndexOf("https%3A"));
  504. } else if (external_href.lastIndexOf("http%3A%2F%2F")) {
  505. new_href = $(this).attr("href").substring($(this).attr("href").lastIndexOf("http%3A"));
  506. }
  507. $(this).attr("href", decodeURIComponent(new_href));
  508. }
  509. });
  510.  
  511. //卡片链接
  512. $("a[class*=\'LinkCard\']:not([class*=\'MCNLinkCard\']):not([class*=\'ZVideoLinkCard\'])").each(function () {
  513. if ($(this).find("LinkCard-title").length > 0 && $(this).find("LinkCard-title").indexOf("http") > -1) {
  514. new_href = $(this).find("LinkCard-title").text();
  515. $(this).attr("href", new_href);
  516. } else if ($(this).attr("href").indexOf("link.zhihu.com/?target=") > -1) {
  517. external_href = $(this).attr("href");
  518. new_href = external_href.substring(external_href = $(this).attr("href").indexOf("link.zhihu.com/?target=") + "link.zhihu.com/?target=".length);
  519. $(this).attr("href", decodeURIComponent(new_href));
  520. } else {
  521. external_href = $(this).attr("href");
  522. if (external_href.lastIndexOf("https%3A")) {
  523. new_href = $(this).attr("href").substring($(this).attr("href").lastIndexOf("https%3A"));
  524. } else if (external_href.lastIndexOf("http%3A%2F%2F")) {
  525. new_href = $(this).attr("href").substring($(this).attr("href").lastIndexOf("http%3A"));
  526. }
  527. $(this).attr("href", decodeURIComponent(new_href));
  528. }
  529. });
  530.  
  531. //旧版视频卡片链接
  532. $("a.VideoCard-link").each(function () {
  533. if ($(this).attr("href").indexOf("link.zhihu.com/?target=") > -1) {
  534. external_href = $(this).attr("href");
  535. equal = external_href.lastIndexOf("http");
  536. colon = external_href.lastIndexOf("%3A");
  537. protocol = external_href.substring(equal, colon);
  538. path = external_href.substring(colon + 5, external_href.length);
  539. new_href = protocol + "://" + path;
  540. $(this).attr("href", decodeURIComponent(new_href));
  541. }
  542. });
  543.  
  544. //隐藏首页广告卡片
  545. $(".TopstoryItem--advertCard").hide();
  546. }
  547.  
  548.  
  549. // 默认高清原图,来自:https://greasyfork.org/scripts/402808
  550. function originalPic(){
  551. $("img").each(function(){
  552. if ($(this).attr("data-original")!=undefined && !$(this).hasClass("comment_sticker")) {
  553. if ($(this).attr("src") != $(this).attr("data-original")) {
  554. $(this).attr("src",$(this).attr("data-original"))
  555. }
  556. }
  557. })
  558. $(".Modal-inner").css({"overflow-y":"hidden"})
  559. }
  560.  
  561.  
  562. (function() {
  563. addEventListener_DOMNodeInserted(); // 监听 网页插入元素 事件
  564.  
  565. // 默认折叠邀请,来自:https://greasyfork.org/scripts/402808
  566. let timer=setInterval(function(){
  567. if($(".QuestionInvitation-content").text().indexOf("更多推荐结果") > -1)
  568. {
  569. clearInterval(timer);
  570. $(".QuestionInvitation-content").addClass("hide");
  571. $(".QuestionInvitation-content").hide();
  572.  
  573. $(".QuestionInvitation-title").html($(".QuestionInvitation-title").text()+'<span style="color: #8590a6;font-size: 14px;"> 展开/折叠</span>')
  574.  
  575. $(".Topbar").click(function(){
  576.  
  577. if(($(".QuestionInvitation-content").hasClass("hide")))
  578. {
  579. $(".QuestionInvitation-content").removeClass("hide").addClass("show");
  580. $(".QuestionInvitation-content").show();
  581. }
  582. else
  583. {
  584. $(".QuestionInvitation-content").removeClass("show").addClass("hide");
  585. $(".QuestionInvitation-content").hide();
  586. }
  587. })
  588. }
  589. })
  590.  
  591.  
  592. setInterval(originalPic,100); // 默认高清原图
  593. if(menu_value('menu_directLink')) setInterval(directLink, 100); // 默认站外直链
  594.  
  595.  
  596. if(window.location.href.indexOf("question") > -1){ // 回答页 //
  597. if(window.location.href.indexOf("waiting") == -1){
  598. collapsedAnswer(); // 一键收起回答
  599. collapsedNowAnswer(".QuestionPage");
  600. collapsedNowAnswer(".Question-main");
  601. }
  602. setInterval(topTime_question, 300); // 置顶显示时间
  603. }else if(window.location.href.indexOf("search") > -1){ // 搜索结果页 //
  604. collapsedAnswer(); // 一键收起回答
  605. collapsedNowAnswer("main div");
  606. collapsedNowAnswer(".Search-container");
  607. setInterval(topTime_search, 300); // 置顶显示时间
  608. EventXMLHttpRequest(); // 区分问题文章
  609. }else if(window.location.href.indexOf("topic") > -1){ // 话题页 //
  610. if(window.location.href.indexOf("unanswered") == -1){
  611. collapsedAnswer(); // 一键收起回答
  612. collapsedNowAnswer("main.App-main");
  613. collapsedNowAnswer(".ContentLayout");
  614. setInterval(topTime_search, 300); // 置顶显示时间
  615. EventXMLHttpRequest(); // 区分问题文章
  616. }
  617. }else if(window.location.href.indexOf("zhuanlan") > -1){ // 文章 //
  618. backToTop("article.Post-Main.Post-NormalMain")
  619. backToTop("div.Post-Sub.Post-NormalSub")
  620. setInterval(topTime_zhuanlan, 300); // 置顶显示时间
  621. }else if(window.location.href.indexOf("column") > -1){ // 专栏 //
  622. collapsedAnswer(); // 一键收起回答
  623. collapsedNowAnswer("main div");
  624. setInterval(topTime_zhuanlan, 300); // 置顶显示时间
  625. }else if(window.location.href.indexOf("people") > -1 || window.location.href.indexOf("org") > -1){ // 用户主页 //
  626. collapsedAnswer(); // 一键收起回答
  627. collapsedNowAnswer("main div");
  628. collapsedNowAnswer(".Profile-main");
  629. setInterval(topTime_people, 300); // 置顶显示时间
  630. }else{ // 首页 //
  631. collapsedAnswer(); // 一键收起回答
  632. collapsedNowAnswer("main div");
  633. collapsedNowAnswer(".Topstory-container");
  634. setInterval(topTime_index, 300); // 置顶显示时间
  635. EventXMLHttpRequest(); // 区分问题文章
  636. }
  637. })();