Zum Inhalt dieser Seite

escape_html_entities.user.js

Per Knopfdruck in Textareas unerwünschte Zeichen ersetzen (momentan spitze Klammern und [kbd]-Tags).
 Code markieren  Download Nur bei Bedarf aktiviert  Gespiegelt
// Copyright 2006 David Wilkinson. All Rights Reserved.
 
// ==UserScript==
// @name          Escape HTML Entities in TextAreas
// @namespace     http://www.dopiaza.org/tools/greasemonkey/escapeentities/
// @description   Escape HTML Entities in TextAreas
// @include       *
// ==/UserScript==
 
function updateTextAreas()
{
    // Find textareas
    var textareas = document.evaluate(
      "//textarea",
      document,
      null,
      XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
      null);
 
    for (var i = 0; i < textareas.snapshotLength; i++)
    {
        addHandlerLink(textareas.snapshotItem(i));
    }    
 
}
 
function addHandlerLink(textarea)
{
    var icon = "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%04gAMA%00%00%AF%C87%05%8A%E9%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%02BIDAT8%CB%A5%93M%88%8Dq%14%C6%7F%EF%7B%DF%B9w%A6%99%09%F7%22%1A%9F%09%0B%A5%D8LH%92%AF%7Cm%2C4Bd%81%2C%2C%AC%94%12%25%3B%85%14Y%D9)Y%11%09I%CDX%D8%F9%88%85R%94%193fL%99%B9%EF%7D%EF%FF%FF%3F%FFs%2Cf%C4%C2bj%9E%CD%B39%E79%9D%E7%9C'13%A6%83%94ib%DA%02%D9%C5%FB%C3%FF%DDAT%09Aq%22%84%00%85%18%85%13%5C%10%0Ao%E4A)%0A!%03%E8%5E%DE%F6O%AB!j%C48%C1%12%8D%20FP%25%04%23%88%12%A2%E2E%B9%FDbhB%00%60%AC!De%B2I%F1q%B2X%0C%2F%8A%93%88%0F%86%13%C5%07%A5%DA%91Q%D4e%C2%035c%DB%EAN%5C%88%EC%5E%D3%C9%FEu%B38%B4%A1%CA%D1M56%AC%EC%A0%9C%25%1CX%3F%9B%D6r%C2%B1%CDs%A9%BBH%C3E%B4%08%A4N%84%20J%25KpA%A9%B4%A4%DCz%F6%83%24%81%2B%0F%BF%B3%A0%D6%C2%8A%F9%15%BA%AAe%DA%CB)K%E6T%C8%9B%91%22(%9A%0BY%EE%84%A6(%95%96%84%83%1BkdiB%EE%043%A3%EE%22%AA%D0%BD%BC%03%03%9AA%A97%23%0D%1Fq!%C5%F2HZ%2F%0C%E7%95%C2%2B%D7%1E%0F%E2Ei4%15%03%F2f%24%9A%F1%FEk%83%0Fw%AF%B3%F6%DEV%DE%EDk%E7H%EFv%96%7D%BC%8D%3AO%F6%AB%F04%7D%1BfLN%86%FA%24%E7.%A2j%BC%BEs%95%9D%E9s6%1D%3ENe%E9*%8AwO%F9%D0%FB%8C%B7%D6%20%D9r%E9%A3%ED%5D3%8Bo%A3%0E%1F%14'%8A%0B%7F%9C%9F8%D7%E1%BE%5D%EC9y%9A%B6%CF%2F%A1%BF%0Ff%CCd%24%5B%CC%93'%9F%C8%C6%C6%85%C2G%3A%5BK%F8%96%84%D6%90%10*%86HB%90%94%A0F5%1F%A0u%DER%D8u%E6%EF%07%5E%98%CF%3C~%92%8D%16%81%1B%8F%FB%B1%60%A8S%CC%0B%16%14s%11%BCbQ%D9Z%AE%D2x%F3%88%F6%07%A7p%C5%20%0D%60%7C%AC%C4HR%23%99J%1A%FBz%BA.%94%3B%DB%CF-%ACI%96%A5%DF%18%1F%16%BE%0C%95bh%DA%F9d%AAq~%D5%B3%E8l%3E%3Ap%A2%14%93%C5%B1d%FD%067w%3C%95%CB%BF%01%8FKv%3FQ%979%D3%00%00%00%00IEND%AEB%60%82";
    var span = document.createElement('span');
    var a = document.createElement('a');
    var img = document.createElement('img');
    img.setAttribute("src", icon);
    img.setAttribute("border", "0");
    img.setAttribute("alt", "Ersetze <, > und kbd-Tags");
    img.setAttribute("title", "Ersetze <, > und kbd-Tags");
    img.style.marginLeft = "3px";
    a.setAttribute("href", window.location.href+"#");
    a.setAttribute("title", "Ersetze <, > und kbd-Tags");
    a.appendChild(img);
    a.addEventListener("click", function(evt)
    {
        escapeEntities(textarea);
    }, false);    
    span.appendChild(a);
    if (textarea.nextSibling == null)
    {
        textarea.parentNode.appendChild(span);
    }
    else
    {
        textarea.parentNode.insertBefore(span, textarea.nextSibling);
    }
}
 
function escapeEntities(textarea)
{
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var val = textarea.value;
    var len = val.length;
    var pos = textarea.scrollTop;
    var selectionMade = true;
 
    if (start == end)
    {
        start = 0;
        end = len;
        selectionMade = false;
    }
 
    var s = val.slice(start, end);
 
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    //s = s.replace(/&/g, "&amp;");
    //s = s.replace(/"/g, "&quot;");
    s = s.replace(/\[KBD\]/gi, "[CODE]");
    s = s.replace(/\[\/KBD\]/gi, "[/CODE]");
 
 
    var pre = val.slice(0, start);
    var post = val.slice(end, len);
 
    textarea.value = pre + s + post;
    if (selectionMade)
    {
        textarea.selectionStart = start;
        textarea.selectionEnd = start + s.length;
    }   
    textarea.scrollTop = pos;
}
 
window.addEventListener("load", 
    function () 
    {
        updateTextAreas();
	}, false
);
 
 
Home