bangumi话题收藏

收藏bangumi的话题

  1. // ==UserScript==
  2. // @name bangumi话题收藏
  3. // @namespace https://github.com/bangumi/scripts/yonjar
  4. // @version 0.1.4
  5. // @description 收藏bangumi的话题
  6. // @author Yonjar
  7. // @include /^https?:\/\/(bgm\.tv|chii\.in|bangumi\.tv)\/((blog|(group|subject)\/topic|rakuen\/topic\/(group|subject))\/\d+(\?.*)?(#.*)?)?$/
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. GM_addStyle(`
  12. #yonjar_collection_tpc .timeline{
  13. max-height: 400px;
  14. overflow: auto;
  15. }
  16. .yonjar_bgm_topic_col_btn{
  17. display: inline-block;
  18. color: #666;
  19. text-shadow: 0px 1px 2px #FFF;
  20. text-decoration: none;
  21. line-height: 20px;
  22. margin: 0 5px 5px 0;
  23. padding: 0 12px;
  24. border: 1px solid #DDD;
  25. background: -webkit-gradient(linear,left top,left bottom,from(#FCFCFC),to(#F1F1F1));
  26. background: -moz-linear-gradient(top,#FCFCFC,#F1F1F1);
  27. background: -o-linear-gradient(top,#FCFCFC,#F1F1F1);
  28. -webkit-box-shadow: 0 1px 2px #EEE,inset 0 1px 1px #FFF;
  29. -moz-box-shadow: 0 1px 2px #EEE,inset 0 1px 1px #FFF;
  30. box-shadow: 0 1px 2px #EEE,inset 0 1px 1px #FFF;
  31. -moz-border-radius: 4px;
  32. -webkit-border-radius: 4px;
  33. border-radius: 4px
  34. }
  35. .yonjar_bgm_topic_col_btn:hover {
  36. color: #FFF;
  37. text-shadow: none;
  38. background: #4F93CF;
  39. background: -moz-linear-gradient(top,#6BA6D8,#4F93CF);
  40. background: -o-linear-gradient(top,#6BA6D8,#4F93CF);
  41. background: -webkit-gradient(linear,left top,left bottom,from(#5FA3DB),to(#72B6E3));
  42. -webkit-box-shadow: 0 0 3px #EEE,inset 0 -1px 5px rgba(0,0,0,0.1);
  43. -moz-box-shadow: 0 0 3px #EEE,inset 0 -1px 5px rgba(0,0,0,0.1);
  44. box-shadow: 0 0 3px #EEE,inset 0 -1px 5px rgba(0,0,0,0.1)
  45. }
  46. .btn_del{
  47. background: transparent url(/img/ico/icons.gif) no-repeat scroll -2px -33px;
  48. display: block;
  49. height: 13px;
  50. text-indent: -999em;
  51. width: 13px;
  52. -moz-opacity: 0.8;
  53. opacity: 0.8;
  54. filter: alpha(opacity=80);
  55. overflow: hidden;
  56. float: right;
  57. }
  58. .btn_del:hover {
  59. background-position: -2px -46px;
  60. }
  61. `);
  62.  
  63. class BgmCollections {
  64. constructor() {
  65. if (!localStorage.getItem("bgm_collections_by_yonjar")) {
  66. localStorage.setItem(
  67. "bgm_collections_by_yonjar",
  68. JSON.stringify([])
  69. );
  70. }
  71. this.collections = JSON.parse(
  72. localStorage.getItem("bgm_collections_by_yonjar")
  73. );
  74. }
  75.  
  76. get list() {
  77. return this.collections;
  78. }
  79.  
  80. update() {
  81. localStorage.setItem(
  82. "bgm_collections_by_yonjar",
  83. JSON.stringify(this.collections)
  84. );
  85. }
  86.  
  87. add(topic) {
  88. this.collections.push(topic);
  89. this.update();
  90. console.log("topic_col:", "add ", topic.id);
  91. }
  92.  
  93. remove(topic) {
  94. for (let i = 0; i < this.collections.length; i++) {
  95. if (this.collections[i].id === topic.id) {
  96. this.collections.splice(i, 1);
  97. break;
  98. }
  99. }
  100. this.update();
  101. console.log("topic_col:", "remove ", topic.id);
  102. }
  103.  
  104. has(topic) {
  105. for (let li of this.collections) {
  106. if (li.id === topic.id) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. }
  113.  
  114. class Topic {
  115. constructor() {
  116. this.id = location.pathname.match(/\d+/)[0];
  117. this.path = this.pathTo(location.pathname);
  118. this.title = document.title;
  119. this.author = (
  120. document.querySelector(".postTopic > div.inner > strong > a") ||
  121. document.querySelector("#pageHeader > h1 > span > a.avatar.l")
  122. ).textContent;
  123. }
  124.  
  125. init() {
  126. let bc = new BgmCollections();
  127. let col_btn = document.createElement("button");
  128. col_btn.classList.add("rr");
  129. col_btn.classList.add("yonjar_bgm_topic_col_btn");
  130. col_btn.innerText = bc.has(this) ? "取消收藏" : "收藏";
  131. col_btn.addEventListener("click", () => {
  132. if (bc.has(this)) {
  133. bc.remove(this);
  134. col_btn.innerText = "收藏";
  135. } else {
  136. bc.add(this);
  137. col_btn.innerText = "取消收藏";
  138. }
  139. });
  140.  
  141. let titleElem =
  142. document.querySelector("#pageHeader > h1") ||
  143. document.querySelector("#header > h1");
  144. titleElem.appendChild(col_btn);
  145. }
  146.  
  147. pathTo(path) {
  148. return /rakuen/.test(path)
  149. ? path.replace(
  150. /rakuen\/topic\/(\w+)\/(\d+)/,
  151. (match, p1, p2) => `${p1}/topic/${p2}`
  152. )
  153. : path;
  154. }
  155. }
  156.  
  157. class HomePage {
  158. constructor() {
  159. this.sideInner = document.querySelector("#columnHomeB > div.sideInner");
  160. this.home_announcement = document.querySelector("#home_announcement");
  161. }
  162.  
  163. init() {
  164. let bc = new BgmCollections();
  165. let col_elem =
  166. document.querySelector("#yonjar_collection_tpc") ||
  167. document.createElement("div");
  168. let listStr = "";
  169. for (let col of bc.list) {
  170. listStr += `
  171. <li>
  172. <a href="${col.path}" title="楼主: ${col.author}" class="l" target="_blank">${col.title}</a>
  173. <a title="取消收藏" data-del-id="${col.id}" class="btn_del" style="display: block;">del</a>
  174. </li>
  175. `;
  176. }
  177. col_elem.innerHTML = `
  178. <div id="yonjar_collection_tpc" class="halfPage">
  179. <div class="sidePanelHome">
  180. <h2 class="subtitle">收藏话题(${bc.list.length})</h2>
  181. <ul class="timeline" style="margin:0 5px">
  182. ${bc.list.length < 1 ? "<li>暂无收藏</li>" : listStr}
  183. </ul>
  184. </div>
  185. </div>
  186. `;
  187.  
  188. this.sideInner.insertBefore(col_elem, this.home_announcement);
  189.  
  190. col_elem.addEventListener("click", e => {
  191. let curr = e.target;
  192. console.log(curr.dataset);
  193. if (curr.className === "btn_del") {
  194. bc.remove({ id: curr.dataset.delId });
  195. this.init();
  196. }
  197. });
  198. }
  199. }
  200.  
  201. (function() {
  202. let cur_url = location.href;
  203. if (/^https?:\/\/(bgm\.tv|chii\.in|bangumi\.tv)\/$/.test(cur_url)) {
  204. let hp = new HomePage();
  205. hp.init();
  206. return;
  207. }
  208.  
  209. let topic = new Topic();
  210. topic.init();
  211. })();

QingJ © 2025

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