Advertisement
Guest User

Untitled

a guest
May 27th, 2014
3,458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. // ==UserScript==
  2. //
  3. // @name HSTextadder
  4. // @description Adds the html for all characters to text boxes
  5. // @author SimplerUser
  6. // @version 1.4.1
  7. // @include http://*.dreamwidth.org/*
  8. //
  9. // ==/UserScript==
  10.  
  11. //edit for ff users by janekptacijarabaci
  12. function detect_txtbx() {
  13.  
  14. var txtbx = document.getElementById("body") || document.getElementById("commenttext");
  15.  
  16. if (txtbx != null){
  17.  
  18. clearInterval(timer);
  19.  
  20. //get the currentusername
  21. var length = document.getElementsByClassName("ljuser").length;
  22. var user = document.getElementsByClassName("ljuser")[length-1].getAttribute("lj:user");
  23.  
  24.  
  25. var usermap = new Array('baby', 'poop', 'sexy', 'crab', 'monster'); //list of usernames
  26. var charactermap = new Array('Text', 'Audio', 'Voicemail', 'Courier', 'Colored');
  27.  
  28. //characters they match to
  29. var character = "";
  30.  
  31. for (var i = 0 ; i < usermap.length; i++){ //set character to username
  32. if (usermap[i] == user) {
  33. character = charactermap[i];
  34. }
  35.  
  36. }
  37.  
  38. //hs characters and their codes
  39. var htmlcharmap = new Array( 'Courier', 'Colored', 'Text', 'Audio', 'Voicemail');
  40. var htmlmap = new Array(
  41. '<span style="font-family:courier;">',
  42. '<span style="font-family:courier; color:#5e5a63;">',
  43. '<span style="font-family:courier;"><B>FROM:</B> whoever@whatever.org<br/><b>TO:</B> whoever@whatever.org',
  44. '<span style="font-family:courier;"><B>CALLING:</B> LAST NAME, FIRST NAME</span>',
  45. '<span style="font-family:courier;"><B>(1) VOICEMAIL:</B> LAST NAME, FIRST NAME</span>');
  46.  
  47.  
  48. var list = document.createElement('select');
  49. list.setAttribute('id', 'charList');
  50. for (var i = 0; i < htmlcharmap.length; i++){
  51. var nameEl = document.createElement('option');
  52. nameEl.setAttribute('value', htmlmap[i]);
  53. nameEl.innerHTML = htmlcharmap[i];
  54. if (htmlcharmap[i] == character) {
  55. nameEl.setAttribute('selected', 'selected');
  56. }
  57. list.appendChild(nameEl);
  58. }
  59.  
  60.  
  61.  
  62. function insertAfter(newElement,targetElement) {
  63.  
  64. var parent = targetElement.parentNode;
  65.  
  66. if(parent.lastchild == targetElement) {
  67. parent.appendChild(newElement);
  68. } else {
  69. parent.insertBefore(newElement, targetElement.nextSibling);
  70. }
  71. }
  72.  
  73. //make the button
  74. var zNode = document.createElement ('button');
  75. zNode.setAttribute ('id', 'myButton');
  76. zNode.setAttribute ('type', 'button');
  77. zNode.innerHTML = 'Add Code';
  78.  
  79. //make the button
  80. var end = document.createElement ('button');
  81. end.setAttribute ('id', 'endButton');
  82. end.setAttribute ('type', 'button');
  83. end.innerHTML = 'close tags';
  84.  
  85.  
  86. //make the button
  87. var act = document.createElement ('button');
  88. act.setAttribute ('id', 'actButton');
  89. act.setAttribute ('type', 'button');
  90. act.innerHTML = 'small tags';
  91.  
  92.  
  93. insertAfter(list, document.getElementById("subject"));
  94. insertAfter(zNode, document.getElementById("charList"));
  95. insertAfter(end, document.getElementById("myButton"));
  96. insertAfter(act, document.getElementById("endButton"));
  97.  
  98.  
  99.  
  100.  
  101. function doGetCaretPosition (ctrl) {
  102. var CaretPos = 0; // IE Support
  103. if (document.selection) {
  104. ctrl.focus ();
  105. var Sel = document.selection.createRange ();
  106. Sel.moveStart ('character', -ctrl.value.length);
  107. CaretPos = Sel.text.length;
  108. }
  109. // Firefox support
  110. else if (ctrl.selectionStart || ctrl.selectionStart == '0')
  111. CaretPos = ctrl.selectionStart;
  112. return (CaretPos);
  113. }
  114. function setCaretPosition(ctrl, pos){
  115. if(ctrl.setSelectionRange)
  116. {
  117. ctrl.focus();
  118. ctrl.setSelectionRange(pos,pos);
  119. }
  120. else if (ctrl.createTextRange) {
  121. var range = ctrl.createTextRange();
  122. range.collapse(true);
  123. range.moveEnd('character', pos);
  124. range.moveStart('character', pos);
  125. range.select();
  126. }
  127. }
  128. function ButtonClickAction (zEvent)
  129. {
  130. html = document.getElementById('charList').value
  131. var curPos = doGetCaretPosition(txtbx);
  132. var curVal = txtbx.value;
  133. txtbx.value = curVal.substring(0, curPos) + html + curVal.substring(curPos); //adds the stuff
  134. setCaretPosition(txtbx, curPos + html.length);
  135. }
  136. function EndButtonClickAction (zEvent)
  137. {
  138. var toAdd = '</span>';
  139.  
  140. var curPos = doGetCaretPosition(txtbx);
  141. var curVal = txtbx.value;
  142.  
  143. txtbx.value = curVal.substring(0, curPos) + toAdd + curVal.substring(curPos); //adds the stuff
  144. setCaretPosition(txtbx, curPos + toAdd.length);
  145. }
  146.  
  147.  
  148. function ActButtonClickAction (zEvent)
  149. {
  150.  
  151. var toAdd = '<small>[]</small>';
  152.  
  153. var curPos = doGetCaretPosition(txtbx);
  154. var curVal = txtbx.value;
  155.  
  156. txtbx.value = curVal.substring(0, curPos) + toAdd + curVal.substring(curPos); //adds the stuff
  157. setCaretPosition(txtbx, curPos + 8);
  158. }
  159.  
  160.  
  161.  
  162. //--- Activate the newly added button.
  163. document.getElementById ("myButton").addEventListener ("click", ButtonClickAction, false);
  164.  
  165. document.getElementById ("endButton").addEventListener ("click", EndButtonClickAction, false);
  166. document.getElementById ("actButton").addEventListener ("click", ActButtonClickAction, false);
  167.  
  168. }
  169.  
  170. }
  171.  
  172. timer = setInterval(detect_txtbx, 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement