BGM NSFW

NSFW for bangumi

目前为 2018-02-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name BGM NSFW
  3. // @namespace https://greasyfork.org/zh-CN/scripts/38829-bgm-nsfw
  4. // @version 0.5
  5. // @description NSFW for bangumi
  6. // @author Vincent
  7. // @include /^https?:\/\/((bangumi|bgm)\.tv|chii.in)\/group\/topic\/\d+$/
  8. // @require https://code.jquery.com/jquery-3.2.1.min.js
  9. // @grant GM_addStyle
  10. // ==/UserScript==
  11.  
  12. GM_addStyle(`
  13. .btn-nsfw {
  14. color: #AAAAAA;
  15. border-radius: 4px;
  16. padding: 0 4px;
  17. margin-left: 4px;
  18. cursor: pointer;
  19. }
  20. .btn-nsfw-cancel {
  21. color: #FFFFFF;
  22. background: #F09199;
  23. border-radius: 4px;
  24. padding: 0 4px;
  25. margin-left: 4px;
  26. cursor: pointer;
  27. }
  28. .post-nsfw {
  29. display: none !important;
  30. }
  31. `);
  32.  
  33. var nsfw_items = undefined;
  34. var topic_id = null;
  35.  
  36. const DB_NAME = 'nsfw';
  37. const DB_TOPIC_TABLE = 'topic';
  38.  
  39. var DB = {
  40. update: function() {
  41. this.action(function(objectStore) {
  42. objectStore.put(nsfw_items, topic_id);
  43. });
  44. },
  45. delete: function() {
  46. this.action(function(objectStore) {
  47. if (nsfw_items.length) {
  48. objectStore.put(nsfw_items, topic_id);
  49. } else {
  50. objectStore.delete(topic_id);
  51. }
  52. });
  53. },
  54. init: function () {
  55. this.action(function(objectStore) {
  56. var list = objectStore.get(topic_id);
  57. list.onsuccess = function(event) {
  58. if (list.result != undefined && list.result.length) {
  59. nsfw_items = list.result;
  60. item = $('#post_' + nsfw_items.join(', #post_'));
  61. addNSFW(item);
  62.  
  63. item.children('div.re_info').append('<small class="btn-nsfw-cancel">NSFW</small>');
  64. };
  65.  
  66. addNSFWBtn();
  67. }
  68. });
  69. },
  70. action: function(callback) {
  71. var connect = indexedDB.open(DB_NAME, 1);
  72. connect.onerror = function(event) {
  73. // 错误处理程序在这里。
  74. };
  75.  
  76. connect.onupgradeneeded = function(event) {
  77. var objectStore = event.target.result.createObjectStore(DB_TOPIC_TABLE);
  78. };
  79.  
  80. connect.onsuccess = function(event) {
  81. var transaction = event.target.result.transaction([DB_TOPIC_TABLE], "readwrite");
  82. var objectStore = transaction.objectStore(DB_TOPIC_TABLE);
  83. callback(objectStore);
  84. };
  85. }
  86. };
  87.  
  88. (function() {
  89. topic_id = window.location.pathname.split('/')[3];
  90. DB.init();
  91. })();
  92.  
  93. function addNSFWBtn() {
  94. $('#comment_list').on('mouseenter', 'div[id*=post_]', function () {
  95. if (!$(this).children('div.re_info').children('small.btn-nsfw, small.btn-nsfw-cancel').length) {
  96. $(this).children('div.re_info').append('<small class="btn-nsfw">NSFW</small>');
  97. }
  98. });
  99. $('#comment_list').on('mouseleave', 'div[id*=post_]', function () {
  100. $(this).children('div.re_info').children('small.btn-nsfw').remove();
  101. });
  102.  
  103. // temp action
  104. $('#comment_list').on('click', 'small.btn-nsfw', function () {
  105. var item = $(this).parent().parent();
  106.  
  107. addNSFW(item);
  108. storeNSFWLocalDB(item.prop('id').split('_')[1]);
  109.  
  110. $(this).addClass('btn-nsfw-cancel');
  111. $(this).removeClass('btn-nsfw');
  112. });
  113. $('#comment_list').on('click', 'small.btn-nsfw-cancel', function () {
  114. var item = $(this).parent().parent();
  115.  
  116. removeNSFW(item);
  117. cancelNSFWLocalDB(item.prop('id').split('_')[1]);
  118.  
  119. $(this).addClass('btn-nsfw');
  120. $(this).removeClass('btn-nsfw-cancel');
  121. });
  122. }
  123.  
  124. function addNSFW(item) {
  125. item.not('.row_reply').addClass('sub_reply_collapse');
  126. item.children('div.inner').children('div.cmt_sub_content').addClass('post-nsfw');
  127. item.children('div.inner').children('div.reply_content').children('div.message').addClass('post-nsfw');
  128. }
  129.  
  130. function removeNSFW(item) {
  131. item.filter('.sub_reply_collapse').removeClass('sub_reply_collapse');
  132. item.children('div.inner').children('div.cmt_sub_content').removeClass('post-nsfw');
  133. item.children('div.inner').children('div.reply_content').children('div.message').removeClass('post-nsfw');
  134. }
  135.  
  136. function storeNSFWLocalDB(post_id) {
  137. if (nsfw_items == undefined || nsfw_items == '') {
  138. nsfw_items = [];
  139. }
  140. if (nsfw_items.indexOf(post_id) == - 1) {
  141. // add to array
  142. nsfw_items.push(post_id);
  143. DB.update();
  144. }
  145. }
  146.  
  147. function cancelNSFWLocalDB(post_id) {
  148. if (nsfw_items == undefined || nsfw_items == '') {
  149. nsfw_items = [];
  150. }
  151. if (nsfw_items.indexOf(post_id) != - 1) {
  152. // remove from array
  153. nsfw_items.splice(nsfw_items.indexOf(post_id), 1);
  154. if (nsfw_items.length) {
  155. DB.update();
  156. } else {
  157. DB.delete();
  158. }
  159. }
  160. }