Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name tmp txt adder
- // @namespace http://pastebin.com/0smkg67S
- // @version 1.0
- // @description hopefully does a thing
- // @author icie
- // @match *.dreamwidth.org/*
- // @grant none
- // ==/UserScript==
- (
- function(){
- function addJQuery(callback) {
- var script = document.createElement("script");
- script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
- script.addEventListener('load', function() {
- var script = document.createElement("script");
- script.textContent = "window.$=jQuery.noConflict(true);(" + callback.toString() + ")();";
- document.body.appendChild(script);
- }, false);
- document.body.appendChild(script);
- }
- function main(){
- var codemap = {};
- codemap.txt = {"start":'<span style="font-family:courier new, monospace">',
- "end":"</span>"
- };
- codemap.action = {"start":'<small>[',
- "end":"]</small>"
- };
- codemap.italic = {"start":'<em>',
- "end":"</em>"
- };
- codemap.mdash = {"start":'—',
- "end":""
- };
- codemap.bold = {"start":'<strong>',
- "end":"</strong>"
- };
- var count = 0;
- function addText(e) {
- var id = e.currentTarget.id;
- var code = codemap[$("#"+id).text()];
- var sStartTag = code.start;
- var sEndTag = code.end;
- var bDouble = true,
- oMsgInput = $('[name="body"]:first')[0],
- nSelStart = oMsgInput.selectionStart,
- nSelEnd = oMsgInput.selectionEnd,
- sOldText = oMsgInput.value;
- oMsgInput.value = sOldText.substring(0, nSelStart) + (bDouble ? sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : sStartTag) + sOldText.substring(nSelEnd);
- oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, (bDouble ? nSelEnd : nSelStart) + sStartTag.length);
- oMsgInput.focus();
- }
- for (var key in codemap){
- var value = codemap[key];
- $('#subject').after($('<button></button>').prop('id',key+'btn').prop('type','button').text(key).click(addText));
- };
- onkeydown = function(e){
- if(e.ctrlKey && (e.keyCode == 'I'.charCodeAt(0) || e.keyCode == 'i'.charCodeAt(0))){
- e.preventDefault();
- $("#italicbtn").click();
- return false;
- }
- if(e.ctrlKey && (e.keyCode == 'b'.charCodeAt(0) || e.keyCode == 'B'.charCodeAt(0))){
- e.preventDefault();
- $("#boldbtn").click();
- return false;
- }
- if(e.ctrlKey && (e.keyCode == 'l'.charCodeAt(0) || e.keyCode == 'L'.charCodeAt(0))){
- e.preventDefault();
- $("#actionbtn").click();
- return false;
- }
- }
- }
- if (window.jQuery) {
- // console.log("jq loaded");
- main();
- } else {
- // console.log("jq not loaded");
- addJQuery(main);
- }
- }
- )();
- //addJQuery(main);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement