/
2009-05-01-greasemonkey-blogger-html-encode-selection.html
50 lines (41 loc) · 1.96 KB
/
2009-05-01-greasemonkey-blogger-html-encode-selection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
title: Greasemonkey + Blogger HTML encode selection
tags:
---
<p>
Quick hit this week. Here is a <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a> script I've been using for a few months. It adds a new button to your Blogger post editor which allows you to HTML encoded any selected text. This is useful if you include a lot of code examples in your blog. <a href="http://userscripts.org/scripts/show/42696">Download</a>.
</p>
<pre name="code" class="javascript">
// ==UserScript==
// @name Blogger - HTML encode on post edit
// @namespace com.bitkickers
// @namespace http://bitkickers.blogspot.com/
// @include http*://www.blogger.com/post-*.g?*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// ==/UserScript==
function addButton() {
$("#postButtons").append($("<div dir=\"ltr\" class=\"cssButtonSize-small cssButtonSide-left\"><div class=\"cssButtonColor-blue\"><a href=\"#\" class=\"cssButton\" id=\"encodeButton\"><div class=\"cssButtonOuter\"><div class=\"cssButtonMiddle\"><div class=\"cssButtonInner\">HTML Encode Selection</div></div></div></a></div></div>"));
$("#encodeButton").click(encode);
}
function encode() {
var editBox = $("#textarea")[0];
if (typeof(editBox) != "undefined") {
var originalScrollHeight = editBox.scrollTop;
var selectedText = editBox.value.substring(editBox.selectionStart, editBox.selectionEnd);
if (selectedText != "") {
editBox.value =
editBox.value.substring(0, editBox.selectionStart)
+ escapeHTML(selectedText)
+ editBox.value.substring(editBox.selectionEnd)
editBox.scrollTop = originalScrollHeight;
}
}
}
function escapeHTML(str){
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
};
addButton();
</pre>