贴吧小助手

自动顶贴回复(立即回复)、移除贴吧列表和帖子内部广告、移除碍眼模块

  1. /*!
  2. // ==UserScript==
  3. // @name 贴吧小助手
  4. // @namespace https://github.com/maomao1996/tampermonkey-scripts
  5. // @version 0.6.3
  6. // @description 自动顶贴回复(立即回复)、移除贴吧列表和帖子内部广告、移除碍眼模块
  7. // @icon https://tb1.bdstatic.com/tb/favicon.ico
  8. // @author maomao1996
  9. // @include *://tieba.baidu.com/*
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_notification
  12. // @grant GM_addStyle
  13. // @require https://greasyfork.org/scripts/398240-gm-config-zh-cn/code/G_zh-CN.js
  14. // ==/UserScript==
  15. */
  16. ;
  17. (function () {
  18. 'use strict';
  19. var GMConfigOptions = {
  20. id: 'Helper_Cfg',
  21. title: '贴吧小助手',
  22. css: '#Helper_Cfg .config_var textarea{max-width: 100%; width: 100%; min-height: 120px;} #Helper_Cfg .inline {padding-bottom:0px;}#Helper_Cfg .config_var {margin-left: 20px;margin-right: 20px;} #Helper_Cfg input[type="checkbox"] {margin-left: 0px;vertical-align: top;} #Helper_Cfg input[type="text"] {width: 60px;} #Helper_Cfg {background-color: lightblue;} #Helper_Cfg .reset_holder {float: left; position: relative; bottom: -1.2em;}',
  23. frameStyle: {
  24. zIndex: '1314520',
  25. width: '400px',
  26. height: '520px'
  27. },
  28. fields: {
  29. removeAd: {
  30. section: ['', '全局设置'],
  31. label: '移除列表和详情页广告',
  32. labelPos: 'right',
  33. type: 'checkbox',
  34. default: true
  35. },
  36. removeEyesore: {
  37. label: '移除碍眼模块(app下载、勋章、送礼物、游戏按钮等)',
  38. labelPos: 'right',
  39. type: 'checkbox',
  40. default: true
  41. },
  42. responseTimeMin: {
  43. section: ['', '自动顶贴相关设置'],
  44. label: '顶帖最小间隔(分钟)',
  45. labelPos: 'left',
  46. type: 'unsigned int',
  47. min: 1,
  48. default: 1
  49. },
  50. responseTimeMax: {
  51. label: '顶帖最大间隔(分钟)',
  52. type: 'unsigned int',
  53. min: 1,
  54. default: 30
  55. },
  56. responseMode: {
  57. label: '顶贴模式选择',
  58. labelPos: 'left',
  59. type: 'select',
  60. options: ['自定义模式', '网络语句模式'],
  61. default: '网络语句模式'
  62. },
  63. customResponseText: {
  64. label: '自定义模式回复内容(请按如下格式输入)',
  65. type: 'textarea',
  66. default: "\u6C99\u53D1\n\u9876\n\u9876~"
  67. }
  68. },
  69. events: {
  70. save: function () {
  71. location.reload();
  72. G.close();
  73. }
  74. }
  75. };
  76. var G = GM_config;
  77. G.init(GMConfigOptions);
  78. GM_registerMenuCommand('设置', function () { return G.open(); });
  79. var CONFIG = {
  80. STATUS: false,
  81. TIME_MIN: Number(G.get('responseTimeMin')),
  82. TIME_MAX: Number(G.get('responseTimeMax')),
  83. TEXT: G.get('customResponseText').split('\n'),
  84. timer: null
  85. };
  86. var pathname = location.pathname;
  87. var message = function (text) {
  88. GM_notification({ timeout: 2e3, text: text });
  89. };
  90. var random = function (lower, upper, floating) {
  91. if (floating) {
  92. var rand = Math.random();
  93. var randLength = "".concat(rand).length - 1;
  94. return Math.min(lower + rand * (upper - lower + parseFloat("1e-".concat(randLength))), upper);
  95. }
  96. return lower + Math.floor(Math.random() * (upper - lower + 1));
  97. };
  98. var removeHtmlElement = function (selector) {
  99. selector.each(function () {
  100. $(this).remove();
  101. });
  102. };
  103. var hideHtmlElement = function (selector) {
  104. return selector.join(',') + '{display:none !important;}';
  105. };
  106. var styles = [
  107. '.core_title.core_title_theme_bright, .core_title.core_title_theme_bright .quick_reply {display: flex;}',
  108. '.core_title.core_title_theme_bright .core_title_txt {flex: 1}',
  109. '.core_title.core_title_theme_bright .btn-small {margin-left: 10px;padding-top: 3px;padding-bottom: 3px;}'
  110. ].join('');
  111. if (G.get('removeEyesore')) {
  112. styles += hideHtmlElement([
  113. '.u_member',
  114. '.celebrity',
  115. '.app_download_box',
  116. '.tbui_aside_fbar_button.tbui_fbar_down',
  117. 'tbui_fbar_props',
  118. '.tbui_fbar_tsukkomi',
  119. '.icon_wrap.icon_wrap_theme1',
  120. '#j_navtab_game',
  121. '.post-foot-send-gift-btn',
  122. '.tb_poster_placeholder'
  123. ]);
  124. }
  125. var autoResponse = function () {
  126. var $titleBtn = $('.core_title_btns');
  127. if (!$titleBtn.length) {
  128. return;
  129. }
  130. var appendResponseBtn = function () {
  131. if (!$('#ding_btn').length) {
  132. $('#quick_reply').after("<a id=\"ding_btn\" rel=\"noopener\" class=\"btn-sub btn-small\">".concat(CONFIG.STATUS ? '关闭' : '开启', "\u81EA\u52A8\u9876\u8D34\u56DE\u590D</a>"));
  133. }
  134. if (CONFIG.STATUS && !$('#reply_immediate').length) {
  135. $('#ding_btn').after('<a id="reply_immediate" rel="noopener" class="btn-sub btn-small">立即回复(重新计时)</a>');
  136. }
  137. };
  138. appendResponseBtn();
  139. var responseObserver = new MutationObserver(function (mutationsList) {
  140. mutationsList.forEach(function (mutation) {
  141. if (mutation.type === 'childList') {
  142. appendResponseBtn();
  143. }
  144. });
  145. });
  146. responseObserver.observe($titleBtn[0], {
  147. childList: true
  148. });
  149. var runResponse = function () {
  150. if (!CONFIG.STATUS) {
  151. return;
  152. }
  153. var selectors = {
  154. editor: '#j_editor_for_container',
  155. submit: '.lzl_panel_submit.j_lzl_p_sb'
  156. };
  157. try {
  158. if (!$('#j_editor_for_container:visible').length) {
  159. var lzlPSelector = '.j_lzl_p.btn-sub.pull-right:visible';
  160. if ($(lzlPSelector).length) {
  161. $(lzlPSelector).eq(0).trigger('click');
  162. }
  163. else if ($('a.lzl_link_unfold:visible').length) {
  164. $('a.lzl_link_unfold:visible').eq(0).trigger('click');
  165. }
  166. else {
  167. $('#quick_reply').trigger('click');
  168. selectors.editor = '#ueditor_replace';
  169. selectors.submit = '.j_submit.poster_submit';
  170. }
  171. }
  172. }
  173. catch (error) {
  174. message('自动回复出错,请刷新页面后重试!');
  175. console.log('runResponse', error);
  176. }
  177. var submit = function (text) {
  178. $(selectors.editor).text(text);
  179. $(selectors.submit).trigger('click');
  180. var time = random(CONFIG.TIME_MIN, CONFIG.TIME_MAX, true) * 6e4;
  181. console.log("".concat(time / 1e3, "\u79D2\u540E\u81EA\u52A8\u9876\u8D34\u56DE\u590D"));
  182. CONFIG.timer = setTimeout(function () {
  183. runResponse();
  184. }, time);
  185. };
  186. if (G.get('responseMode') === '网络语句模式') {
  187. $.ajax({
  188. type: 'GET',
  189. url: 'https://v1.hitokoto.cn',
  190. dataType: 'json',
  191. success: function (data) {
  192. submit(data.hitokoto);
  193. },
  194. error: function (_jqXHR, textStatus, errorThrown) {
  195. console.error(textStatus, errorThrown);
  196. }
  197. });
  198. }
  199. else {
  200. var index = random(0, CONFIG.TEXT.length - 1);
  201. submit(CONFIG.TEXT[index]);
  202. }
  203. };
  204. var clearTimer = function () {
  205. clearTimeout(CONFIG.timer);
  206. CONFIG.timer = null;
  207. };
  208. if (CONFIG.STATUS) {
  209. setTimeout(function () {
  210. runResponse();
  211. message('已开启自动顶贴回复');
  212. }, 1e3);
  213. }
  214. $(document).on('click', '#ding_btn', function () {
  215. if (CONFIG.STATUS) {
  216. CONFIG.STATUS = false;
  217. $(this).text('开启自动顶贴回复');
  218. $('#reply_immediate').remove();
  219. clearTimer();
  220. message('已关闭自动顶贴回复');
  221. }
  222. else {
  223. CONFIG.STATUS = true;
  224. $(this).text('关闭自动顶贴回复');
  225. runResponse();
  226. appendResponseBtn();
  227. message('已开启自动顶贴回复');
  228. }
  229. });
  230. $(document).on('click', '#reply_immediate', function () {
  231. clearTimer();
  232. runResponse();
  233. });
  234. };
  235. if (pathname === '/f') {
  236. console.log('进入贴吧列表');
  237. if (!G.get('removeAd')) {
  238. return;
  239. }
  240. styles += hideHtmlElement([
  241. '.bus-top-activity-wrap',
  242. '#thread_list > div:not([data-tid])',
  243. '.fengchao-wrap-feed',
  244. '[id="pagelet_frs-aside/pagelet/aside_ad"]',
  245. '#aside-ad',
  246. '#branding_ads'
  247. ]);
  248. }
  249. GM_addStyle(styles);
  250. $(window).on('load', function () {
  251. if (/^\/p\/\d{1,}$/.test(pathname)) {
  252. console.log('进入贴子详情');
  253. autoResponse();
  254. G.get('removeAd') && removeHtmlElement($('div[ad-dom-img="true"]'));
  255. }
  256. });
  257. })();