bdwmTextBox

A new Text Box for PKU BBS

  1. // ==UserScript==
  2. // @name bdwmTextBox
  3. // @namespace bdwmTextBox
  4. // @description A new Text Box for PKU BBS
  5. // @include *bdwm.net/bbs/bbspst.php*
  6. // @include *bdwm.net/bbs/bbspsm.php*
  7. // @version 0.1beta
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /**
  12. * cursorPosition Object
  13. *
  14. * Created by Blank Zheng on 2010/11/12.
  15. * Copyright (c) 2010 PlanABC.net. All rights reserved.
  16. *
  17. * The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.
  18. */
  19. var cursorPosition = {
  20. get: function (textarea) {
  21. var rangeData = {text: "", start: 0, end: 0 };
  22. if (textarea.setSelectionRange) { // W3C
  23. textarea.focus();
  24. rangeData.start= textarea.selectionStart;
  25. rangeData.end = textarea.selectionEnd;
  26. rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end): "";
  27. }
  28. return rangeData;
  29. },
  30. set: function (textarea, rangeData) {
  31. if(!rangeData) {
  32. alert("You must get cursor position first.")
  33. }
  34. textarea.focus();
  35. if (textarea.setSelectionRange) { // W3C
  36. textarea.setSelectionRange(rangeData.start, rangeData.end);
  37. }
  38. },
  39.  
  40. add: function (textarea, rangeData, text) {
  41. var oValue, nValue, nStart, nEnd, st;
  42. this.set(textarea, rangeData);
  43. if (textarea.setSelectionRange) { // W3C
  44. oValue = textarea.value;
  45. nValue = oValue.substring(0, rangeData.start) + text + oValue.substring(rangeData.end);
  46. nStart = nEnd = rangeData.start + text.length;
  47. st = textarea.scrollTop;
  48. textarea.value = nValue;
  49. // Fixbug:
  50. // After textarea.values = nValue, scrollTop value to 0
  51. if(textarea.scrollTop != st) {
  52. textarea.scrollTop = st;
  53. }
  54. textarea.setSelectionRange(nStart, nEnd);
  55. }
  56. }
  57. }
  58.  
  59. function setTextAttr(textarea, attr) {
  60. range = cursorPosition.get(textarea);
  61. nText = '[' + attr + 'm' + range.text + '';
  62. cursorPosition.add(textarea, range, nText);
  63. }
  64.  
  65. // 添加颜色下拉条
  66. function addSelect(parent, id, num, name) {
  67. var colors = [
  68. '黑色',
  69. '红色',
  70. '绿色',
  71. '黄色',
  72. '蓝色',
  73. '品红',
  74. '蓝绿',
  75. '白色'
  76. ];
  77. // 添加标题
  78. var title = document.createElement('span');
  79. title.innerHTML = name;
  80. parent.appendChild(title);
  81. // 生成下拉条
  82. var mySelect = document.createElement('select');
  83. mySelect.id = id;
  84. for (var i = 0; i < colors.length; i++) {
  85. var opt = document.createElement('option');
  86. opt.value = num + i;
  87. opt.innerHTML = colors[i];
  88. mySelect.appendChild(opt);
  89. }
  90. // 默认项: 未选择颜色
  91. var opt = document.createElement('option');
  92. opt.value = - 1;
  93. opt.innerHTML = '不设置';
  94. opt.selected = 'selected';
  95. // 设为默认选择
  96. mySelect.appendChild(opt);
  97. // 加入父节点
  98. parent.appendChild(mySelect);
  99. return mySelect;
  100. }
  101.  
  102. // 添加复选框
  103. function addCheck(parent, val, name) {
  104. // 标题
  105. var title = document.createElement('span');
  106. title.innerHTML = name;
  107. parent.appendChild(title);
  108. // 选框
  109. var box = document.createElement('input');
  110. box.type = 'checkbox';
  111. box.value = val;
  112. // 加入父节点
  113. parent.appendChild(box);
  114. return box;
  115. }
  116. // 建立表格
  117.  
  118. function createForm(textarea) {
  119. var myform = document.createElement('form');
  120. //选择颜色
  121. var pColor = document.createElement('p');
  122. var sfg = addSelect(pColor, 'fgcolor', 30, '前景色');
  123. var sbg = addSelect(pColor, 'bgcolor', 40, '背景色');
  124. myform.appendChild(pColor);
  125. // 复选框
  126. var pBoxes = document.createElement('p');
  127. var boxes = [
  128. addCheck(pBoxes, 1, '高亮'),
  129. addCheck(pBoxes, 5, '闪烁'),
  130. addCheck(pBoxes, 4, '下划线')
  131. ];
  132. myform.appendChild(pBoxes);
  133. // 确定按钮
  134. var btn = document.createElement('input');
  135. btn.type = 'button';
  136. btn.value = '确定';
  137. btn.onclick = function () {
  138. var attr = '';
  139. for (var i = 0; i < boxes.length; i++) {
  140. if (boxes[i].checked) {
  141. attr += ';' + boxes[i].value;
  142. }
  143. }
  144. if (sbg.value != '-1') {
  145. attr += ';' + sbg.value;
  146. }
  147. if (sfg.value != '-1') {
  148. attr += ';' + sfg.value;
  149. }
  150. setTextAttr(textarea, attr.substring(1));
  151. };
  152. myform.appendChild(btn);
  153. // 关闭按钮
  154. var closeBtn = document.createElement('input');
  155. closeBtn.type = 'button';
  156. closeBtn.value = '关闭';
  157. closeBtn.onclick = function () {
  158. this.parentNode.parentNode.style.display = 'none';
  159. };
  160. myform.appendChild(closeBtn);
  161. return myform;
  162. }
  163.  
  164. console.log('script begins');
  165. /** 寻找目标表单
  166. var frmpost;
  167. if ((frmpost = document.getElementById('postfrm')) != null) {
  168. console.log('Post form \'postfrm\' found');
  169. } else if ((frmpost = document.getElementById('frmpost')) != null) {
  170. console.log('Post form \'frmpost\' found');
  171. } else {
  172. console.log('Post form not found!!!');
  173. }
  174. */
  175.  
  176. // 获取文字窗口
  177. console.log('Getting textarea');
  178. var textBoxes = document.getElementsByName('text');
  179. if (textBoxes.length == 0) {
  180. alert('No such text box!');
  181. }
  182. var textArea = textBoxes[0];
  183. // 设置文本框为10行...
  184. textArea.rows = '10';
  185. console.log('textarea set to 10 lines');
  186.  
  187. // 建立浮动窗体
  188. console.log('building the textAttr dialog');
  189. var mydiv = document.createElement('div');
  190. mydiv.style.position = 'fixed';
  191. mydiv.style.left = '30%';
  192. mydiv.style.top = '40%';
  193. mydiv.style.backgroundColor = '#89e842';
  194. mydiv.style.display = 'none';
  195. mydiv.appendChild(createForm(textArea));
  196. document.body.appendChild(mydiv);
  197.  
  198. // 添加按钮
  199. console.log('adding button');
  200. var Buttons = document.getElementsByTagName('input');
  201. var postButton;
  202. for (var i = 0; i < Buttons.length; i++) {
  203. //alert(Buttons[i].value);
  204. if (Buttons[i].value == '发表' || Buttons[i].value == '发送') {
  205. postButton = Buttons[i];
  206. break;
  207. }
  208. }
  209. var attrBtn = document.createElement('input')
  210. attrBtn.type = 'button';
  211. attrBtn.onclick = function () {
  212. console.log('Button clicked');
  213. mydiv.style.display = 'block';
  214. };
  215. attrBtn.value = '设置文字属性';
  216. postButton.parentNode.insertBefore(attrBtn, postButton.nextSibling);
  217. console.log('script ends');

QingJ © 2025

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