Advertisement
simpleruser

dw txt adder

Mar 12th, 2025 (edited)
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         tmp txt adder
  3. // @namespace    http://pastebin.com/0smkg67S
  4. // @version      1.0
  5. // @description  hopefully does a thing
  6. // @author       icie
  7. // @match        *.dreamwidth.org/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (
  12.     function(){
  13.         function addJQuery(callback) {
  14.             var script = document.createElement("script");
  15.             script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
  16.             script.addEventListener('load', function() {
  17.                 var script = document.createElement("script");
  18.                 script.textContent = "window.$=jQuery.noConflict(true);(" + callback.toString() + ")();";
  19.                 document.body.appendChild(script);
  20.             }, false);
  21.             document.body.appendChild(script);
  22.         }
  23.  
  24.         function main(){
  25.  
  26.             var codemap = {};
  27.             codemap.txt = {"start":'<span style="font-family:courier new, monospace">',
  28.                            "end":"</span>"
  29.                           };
  30.             codemap.action = {"start":'<small>[',
  31.                               "end":"]</small>"
  32.                              };
  33.             codemap.italic = {"start":'<em>',
  34.                               "end":"</em>"
  35.                              };
  36.             codemap.mdash = {"start":'&mdash;',
  37.                               "end":""
  38.                              };
  39.             codemap.bold = {"start":'<strong>',
  40.                               "end":"</strong>"
  41.                              };
  42.  
  43.             var count = 0;
  44.  
  45.             function addText(e) {
  46.                 var id = e.currentTarget.id;
  47.                 var code = codemap[$("#"+id).text()];
  48.                 var sStartTag = code.start;
  49.                 var sEndTag = code.end;
  50.                 var bDouble = true,
  51.                     oMsgInput = $('[name="body"]:first')[0],
  52.                     nSelStart = oMsgInput.selectionStart,
  53.                     nSelEnd = oMsgInput.selectionEnd,
  54.                     sOldText = oMsgInput.value;
  55.                 oMsgInput.value = sOldText.substring(0, nSelStart) + (bDouble ? sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : sStartTag) + sOldText.substring(nSelEnd);
  56.                 oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, (bDouble ? nSelEnd : nSelStart) + sStartTag.length);
  57.                 oMsgInput.focus();
  58.             }
  59.             for (var key in codemap){
  60.                 var value = codemap[key];
  61.                 $('#subject').after($('<button></button>').prop('id',key+'btn').prop('type','button').text(key).click(addText));
  62.             };
  63.  
  64.             onkeydown = function(e){
  65.                 if(e.ctrlKey && (e.keyCode == 'I'.charCodeAt(0) || e.keyCode == 'i'.charCodeAt(0))){
  66.                     e.preventDefault();
  67.                     $("#italicbtn").click();
  68.                     return false;
  69.                 }
  70.                 if(e.ctrlKey && (e.keyCode == 'b'.charCodeAt(0) || e.keyCode == 'B'.charCodeAt(0))){
  71.                     e.preventDefault();
  72.                     $("#boldbtn").click();
  73.                     return false;
  74.                 }
  75.                 if(e.ctrlKey && (e.keyCode == 'l'.charCodeAt(0) || e.keyCode == 'L'.charCodeAt(0))){
  76.                     e.preventDefault();
  77.                     $("#actionbtn").click();
  78.                     return false;
  79.                 }
  80.             }
  81.         }
  82.         if (window.jQuery) {
  83.  
  84.             //   console.log("jq loaded");
  85.             main();
  86.         } else {
  87.  
  88.             //   console.log("jq not loaded");
  89.             addJQuery(main);
  90.         }
  91.  
  92.     }
  93. )();
  94. //addJQuery(main);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement