Editor

Provides HTML editing.

目前为 2017-07-07 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/31226/204775/Editor.js

  1. /**
  2. * @Anthony Pizzimenti
  3. * @desc Class containing useful style manipulation methods.
  4. * @constructor
  5. */
  6. function Editor () { }
  7.  
  8. /**
  9. * @Anthony Pizzimenti
  10. * @desc Object wrapper for styling functions.
  11. */
  12. Editor.prototype.style = {};
  13.  
  14. /**
  15. * @author Anthony Pizzimenti
  16. * @desc Changes the font style attributes of a given list of DOM nodes.
  17. *
  18. * @param {NodeList} context NodeList of DOM nodes to be modified. Usually consists of rows.
  19. * @param {string} [family="Comic Sans"] Desired font families.
  20. * @param {number} [size=12] Desired font size.
  21. * @param {string} [weight=""] Desired font weight.
  22. * @returns {undefined}
  23. *
  24. * @this Editor
  25. */
  26. Editor.prototype.style.font = function (context, family, size, weight) {
  27. var i,
  28. text;
  29. if (!_paramExist(family, "string")) {
  30. family = "Comic Sans";
  31. }
  32. if (!_paramExist(size, "number")) {
  33. size = 12;
  34. }
  35. if (!_paramExist(weight, "string")) {
  36. weight = "";
  37. }
  38. // modify text properties with defaults
  39. for (i = 0; i < context.length; i++) {
  40. text = context[i];
  41. text.style.fontFamily = family;
  42. text.style.fontSize = size.toString() + "pt";
  43. text.style.fontWeight = weight;
  44. }
  45. };
  46.  
  47. /**
  48. * @author Anthony Pizzimenti
  49. * @param {NodeList} subtext NodeList of DOM nodes to be modified.
  50. * @param {number} [height=""] Desired height.
  51. * @returns {undefined}
  52. *
  53. * @this Editor
  54. */
  55. Editor.prototype.style.height = function (subtext, height) {
  56. var i;
  57. if (!_paramExist(height, "number")) {
  58. height = "";
  59. }
  60. for (i = 0; i < subtext.length; i++) {
  61. subtext[i].style.height = height.toString() + "px";
  62. }
  63. };
  64.  
  65. /**
  66. * @author Anthony Pizzimenti
  67. * @desc Wrapper object for removeFirst.
  68. *
  69. * @this Editor
  70. */
  71. Editor.prototype.removeFirst = {};
  72.  
  73. /**
  74. * @author Anthony Pizzimenti
  75. * @desc Removes first DOM node's parent in class family.
  76. * @param {NodeList} list List of DOM nodes.
  77. * @param {string} classname Family that contains element to be deleted.
  78. * @returns {undefined}
  79. *
  80. * @this Editor
  81. */
  82. Editor.prototype.removeFirst.parent = function (list, classname) {
  83. if (_paramExist(classname, "string")) {
  84. list.getElementsByClassName(classname)[0].parentNode.remove();
  85. } else {
  86. console.error("Invalid class name provided.");
  87. }
  88. };
  89.  
  90. /**
  91. * @author Anthony Pizzimenti
  92. * @desc Removes first DOM node in class family.
  93. * @param {NodeList} list List of DOM nodes.
  94. * @param {string} classname Family that contains element to be deleted.
  95. * @returns {undefined}
  96. *
  97. * @this Editor
  98. */
  99. Editor.prototype.removeFirst.byClass = function (list, classname) {
  100. if (_paramExist(classname, "string")) {
  101. list.getElementsByClassName(classname)[0].remove();
  102. } else {
  103. console.error("Invalid class name provided.");
  104. }
  105. };
  106.  
  107. /**
  108. * @author Anthony Pizzimenti
  109. * @desc Removes first DOM node in tag family.
  110. * @param {NodeList} list List of DOM nodes.
  111. * @param {string} tagname Family that contains element to be deleted.
  112. * @returns {undefined}
  113. *
  114. * @this Editor
  115. */
  116. Editor.prototype.removeFirst.byTag = function (list, tagname) {
  117. if (_paramExist(tagname, "string")) {
  118. list.getElementsByTagName(tagname)[0].remove();
  119. } else {
  120. console.error("Invalid tag name provided.");
  121. }
  122. };
  123.  
  124. /**
  125. * @author Anthony Pizzimenti
  126. * @desc Wrapper object for links.
  127. */
  128. Editor.prototype.links = {};
  129.  
  130. /**
  131. * @author Anthony Pizzimenti
  132. * @desc Changes all relative links to absolute ones.
  133. * @param {NodeList} list List of DOM nodes.
  134. * @param {NodeList} subtext NodeList of DOM nodes.
  135. * @param {string} url Root to join paths with.
  136. * @returns {undefined}
  137. *
  138. * @this Editor
  139. */
  140. Editor.prototype.links.absolute = function (list, subtext, url) {
  141. var regpath_hide = /hide\?./,
  142. regpath = /item\?.|user\?./,
  143. regurl = /\//,
  144. link,
  145. links,
  146. i,
  147. j;
  148. if (!_paramExist(url, "string")) {
  149. console.error("URL provided is invalid.");
  150. return;
  151. }
  152. for (i = 0; i < subtext.length; i++) {
  153. links = subtext[i].getElementsByTagName("a");
  154. // check for regex matches on hiding and item links
  155. for (j = 0; j < links.length; j++) {
  156. if (regpath_hide.test(links[j].href)) {
  157. links[j].remove();
  158. }
  159. }
  160. }
  161. // check for links everywhere else
  162. links = list.getElementsByTagName("a");
  163. for (i = 0; i < links.length; i++) {
  164. link = links[i].href.split(regurl)[3];
  165. if (regpath.test(link)) {
  166. links[i].href = absurl(url, link);
  167. }
  168. }
  169. };
  170.  
  171. /**
  172. * @author Anthony Pizzimenti
  173. * @desc Sets all links' targets to be blank so they open on a new page.
  174. * @param {NodeList} subtext NodeList of DOM nodes containing links.
  175. * @returns {undefined}
  176. *
  177. * @this Editor
  178. */
  179. Editor.prototype.links.blank = function (subtext) {
  180. var i;
  181. // set links so they open a new tab
  182. for (i = 0; i < subtext.length; i++) {
  183. subtext[i].target = "_blank";
  184. }
  185. };