知乎浏览历史记录

你有没有经历过"浏览了一个知乎回答后因为没有收藏或点赞当再次想看时却找不到"的痛苦?本脚本将你在知乎首页点开过的回答保存到你的知乎收藏夹"浏览记录"下, 这样再也不用担心找不到看过的回答了

  1. // ==UserScript==
  2. // @name 知乎浏览历史记录
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.9
  5. // @description 你有没有经历过"浏览了一个知乎回答后因为没有收藏或点赞当再次想看时却找不到"的痛苦?本脚本将你在知乎首页点开过的回答保存到你的知乎收藏夹"浏览记录"下, 这样再也不用担心找不到看过的回答了
  6. // @author wang0.618@qq.com
  7. // @match https://www.zhihu.com/*
  8. // @match https://zhuanlan.zhihu.com/*
  9. // @grant unsafeWindow
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function getJson(api,callback){
  16. let oReq = new XMLHttpRequest();
  17. oReq.onload = function (e) {
  18. if(callback)
  19. callback(e.target.response);
  20. };
  21. oReq.open('GET', api, true);
  22. oReq.setRequestHeader("Content-type","application/json");
  23. oReq.responseType = 'json';
  24. oReq.withCredentials = true;
  25. oReq.send();
  26. }
  27. function postJson(api,data,callback){
  28. let oReq = new XMLHttpRequest();
  29. oReq.onload = function (e) {
  30. if(callback)
  31. callback(e.target.response);
  32. };
  33. oReq.open('POST', api, true);
  34. oReq.setRequestHeader("Content-type","application/json");
  35. oReq.responseType = 'json';
  36. oReq.withCredentials = true;
  37. if(data)
  38. oReq.send(JSON.stringify(data));
  39. else
  40. oReq.send();
  41. }
  42.  
  43. function get_collection_id(){
  44. let collection_name = '浏览记录';
  45. if(!window.localStorage.zhihu_collect_id){
  46. let uid = JSON.parse(document.querySelector('[data-zop-usertoken]').dataset["zopUsertoken"])["urlToken"];
  47. // let url = "https://www.zhihu.com/api/v4/people/"+uid+"/collections";
  48. let url = 'https://www.zhihu.com/api/v4/collections/contents/answer/12217049?offset=0&limit=100';
  49. getJson(url,function(data) {
  50. let collection_id = undefined;
  51. for (let i = data.data.length - 1; i >= 0; i--) {
  52. if(data.data[i].title==collection_name){
  53. collection_id = data.data[i].id;
  54. break;
  55. }
  56. }
  57. if(!collection_id){
  58. if(uid) // 已经登陆
  59. alert("知乎浏览历史记录脚本: 请手动创建一个名为'浏览记录'的知乎收藏夹,来保存您查看过的回答。请在创建完收藏夹后刷新本页");
  60. }else{
  61. window.localStorage.zhihu_collect_id = collection_id;
  62. console.log("知乎浏览历史记录脚本: 收藏夹id %s", collection_id)
  63. }
  64. });
  65. }
  66.  
  67. }
  68.  
  69. function collect(item_id, type){
  70. try{
  71.  
  72. if(window.localStorage.zhihu_collect_id)
  73. postJson('https://www.zhihu.com/api/v4/collections/'+window.localStorage.zhihu_collect_id+'/contents?content_id='+item_id+'&content_type='+type.toLowerCase());
  74. }catch(e){
  75. console.log(e);
  76. }
  77. }
  78.  
  79. get_collection_id();
  80.  
  81. // 回答页面
  82. let re = /www.zhihu.com\/question\/.*?\/answer\/([0-9]*)/;
  83. let match = re.exec(window.location.href);
  84. if(match){
  85. console.log("知乎浏览历史记录脚本: 收藏回答 %s", match[match.length-1]);
  86. collect(match[match.length-1], 'answer');
  87. }
  88.  
  89. // 专栏页面
  90. re = /zhuanlan.zhihu.com\/p\/([0-9]*)/;
  91. match = re.exec(window.location.href);
  92. if(match){
  93. console.log("知乎浏览历史记录脚本: 收藏文章 %s", match[match.length-1]);
  94. collect(match[match.length-1], 'answer');
  95. }
  96.  
  97. // 问题页面,收藏第一个回答
  98. re = /www.zhihu.com\/question\/([0-9]*)$/;
  99. match = re.exec(window.location.href);
  100. if(match){
  101. let info = JSON.parse(document.querySelector('.AnswerItem').dataset.zop);
  102. console.log("知乎浏览历史记录脚本: 收藏问题下的第一个回答 %s", JSON.stringify(info));
  103. collect(info['itemId'], info['type']);
  104. }
  105.  
  106. // 点击
  107. document.addEventListener("click", function(e) {
  108. // loop parent nodes from the target to the delegation node
  109. for (let target = e.target; target && target != this; target = target.parentNode) {
  110. if (target.matches(".AnswerItem,.ArticleItem")) {
  111. let info = JSON.parse(target.dataset.zop);
  112. console.log("知乎浏览历史记录脚本: 点击 %s", JSON.stringify(info));
  113. collect(info['itemId'], info['type']);
  114. break;
  115. }
  116.  
  117. }
  118. }, true);
  119.  
  120.  
  121.  
  122. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址