Fast_Reply

贴吧快速回帖

  1. // ==UserScript==
  2. // @name Fast_Reply
  3. // @namespace https://blog.chrxw.com/
  4. // @version 1.2
  5. // @description 贴吧快速回帖
  6. // @author Chr_
  7. // @include https://tieba.baidu.com/p/*
  8. // @license AGPL-3.0
  9. // @icon https://blog.chrxw.com/favicon.ico
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. // 快速回帖内容
  15. let VReplys = [];
  16. // 初始化
  17. (() => {
  18. 'use strict';
  19. setTimeout(() => {
  20. addBtns();
  21. initSelect()
  22. }, 1000);
  23. loadCFG();
  24. })();
  25. // 添加工具栏
  26. function addBtns() {
  27. function genSelect(id, foo) {
  28. let s = document.createElement('select');
  29. s.id = id;
  30. s.style.cssText = 'width:250px;';
  31. s.addEventListener('change', foo);
  32. return s;
  33. }
  34. function genButton(id, name, foo) {
  35. let b = document.createElement('button');
  36. b.id = id;
  37. b.textContent = name;
  38. b.addEventListener('click', foo);
  39. return b;
  40. }
  41. let btnArea = document.querySelector('.poster_component.editor_bottom_panel.clearfix');
  42. let panel = document.createElement('div');
  43. let selReply = genSelect('selReply', fastReply);
  44. let btnAdd = genButton('btnAdd', '【添加】', addFastReply);
  45. let btnDel = genButton('btnDel', '【删除】', delFastReply);
  46. panel.appendChild(selReply);
  47. panel.appendChild(btnAdd);
  48. panel.appendChild(btnDel);
  49. btnArea.appendChild(panel);
  50. }
  51. // 初始化选择框
  52. function initSelect() {
  53. function genOption(name, value) {
  54. let o = document.createElement('option');
  55. o.value = value.toString();
  56. o.textContent = name;
  57. return o;
  58. }
  59. let selReply = document.getElementById('selReply');
  60. selReply.innerHTML = '';
  61. let o = genOption('--快速回复--', -1);
  62. selReply.appendChild(o);
  63. for (let i = 0; i < VReplys.length; i++) {
  64. let t = VReplys[i].replace(/<\/?p>/g, ' ').replace(/<\/?br>/g, ' ').trim().slice(0, 20);
  65. let o = genOption(t, i);
  66. selReply.appendChild(o);
  67. }
  68. }
  69. // 快速回复
  70. function fastReply() {
  71. let selReply = document.getElementById('selReply');
  72. let editBox = document.getElementById('ueditor_replace');
  73. if (selReply.selectedOptions.length > 0) {
  74. let value = selReply.selectedOptions[0].value;
  75. let id = Number(value);
  76. if (id == -1) {
  77. return;
  78. }
  79. editBox.innerHTML = VReplys[id];
  80. saveCFG();
  81. } else {
  82. alert('未设置快速回复内容!');
  83. }
  84. }
  85. // 添加快速回复
  86. function addFastReply() {
  87. let editBox = document.getElementById('ueditor_replace');
  88. let msg = editBox.innerHTML;
  89. for (let i = 0; i < VReplys.length; i++) {
  90. if (msg == VReplys[i]) {
  91. alert('快速回复已存在!');
  92. return;
  93. }
  94. }
  95. VReplys.push(msg);
  96. initSelect();
  97. saveCFG();
  98. }
  99. // 删除快速回复
  100. function delFastReply() {
  101. let selReply = document.getElementById('selReply');
  102. if (selReply.selectedOptions.length > 0) {
  103. let value = selReply.selectedOptions[0].value;
  104. let id = Number(value);
  105. if (id == -1) {
  106. alert('未设置快速回复内容!');
  107. return;
  108. }
  109. VReplys.pop(id);
  110. initSelect();
  111. saveCFG();
  112. } else {
  113. alert('未设置快速回复内容!');
  114. }
  115. }
  116. // 保存设置
  117. function saveCFG() {
  118. GM_setValue('VReplys', VReplys);
  119. }
  120. // 读取设置
  121. function loadCFG() {
  122. VReplys = GM_getValue('VReplys') || [];
  123. }