Source

Shlomi Fish's Greasemonkey Scripts / greasemonkey-scripts / mediawiki-edit-test.html

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
    html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<title>Shlomi Fish's Homepage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>
</head>
<body>

<form id="editform" method="post" action="/hello.cgi">

<textarea tabindex='1' accesskey="," name="wpTextbox1" id="wpTextbox1">
</textarea>

</form>

<script type="text/javascript">

function myesc(s)
{
    return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g, "&quot;");
}

var timeout;

function autocomplete_box_on_press(e)
{
    alert("foo");
    clearTimeout(timeout);
    setTimeout(autocomplete_box, 10);
}

function autocomplete_box(not1, not2)
{
    var range = $("#wpTextbox1").getSelection();
    var elem = $("#wpTextbox1")[0];

    // Multiple chars selection
    if (range.start != range.end)
    {
        return;
    }

    var line_end = range.end;
    var line_start = line_end;

    while ((line_start >= 0) && (elem.value.substring(line_start,line_start+1) != "\n"))
    {
        line_start--;
    }
    line_start++;

    var line = elem.value.substring(line_start, line_end);

    alert("Line == " + line);
    if (line == "*Oz")
    {
        elem.value = elem.value.substring(0, line_start) + "* '''Ozy''':" + elem.value.substring(line_end);
    }

    return;
}

// All your GM code must be inside this function
function letsJQuery()
{
    alert("One");
    /*
 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 */

(function() {

	var fieldSelection = {

		getSelection: function() {

			var e = this.jquery ? this[0] : this;

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
				}) ||

				/* exploder */
				(document.selection && function() {

					e.focus();

					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: e.value.length, length: 0 }
					}

					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);

					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||

				/* browser not supported */
				function() {
					return { start: 0, end: e.value.length, length: 0 };
				}

			)();

		},

		replaceSelection: function() {

			var e = this.jquery ? this[0] : this;
			var text = arguments[0] || '';

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||

				/* browser not supported */
				function() {
					e.value += text;
					return this;
				}

			)();

		}

	};

	jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();

    alert("Two");

    $("#wpTextbox1").keypress(autocomplete_box_on_press);

    }
alert("Hello");
letsJQuery();
</script>

</body>
</html>