Twitter Fix Web Replies

By Richard Gibson Last update Jul 26, 2009 — Installed 322 times. Daily Installs: 2, 2, 3, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3

There are 12 previous versions of this script.

// ==UserScript==
// @name           Twitter Fix Web Replies
// @namespace      http://userscripts.org/people/336
// @description    Prevents discard of in_reply_to data when replies do not begin with the @mention (e.g. for #fixreplies).
// @source         http://userscripts.org/scripts/show/53598
// @identifier     http://userscripts.org/scripts/source/53598.user.js
// @version        0.2
// @date           2009-07-26
// @creator        Richard Gibson <@gmail.com>
// @include        http://twitter.com/*
// @include        https://twitter.com/*
// ==/UserScript==
// 
// **COPYRIGHT NOTICE**
// 
// I, Richard Gibson, hereby establish my original authorship of this
// work, and announce its release into the public domain.  I claim no
// exclusive copyrights to it, and will neither pursue myself (nor
// condone pursuit by others of) punishment, retribution, or forced
// payment for its full or partial reproduction in any form.
// 
// That being said, I would like to receive credit for this work
// whenever it, or any part thereof, is reproduced or incorporated into
// another creation; and would also like compensation whenever revenue
// is derived from such reproduction or inclusion.  At the very least,
// please let me know if you find this work useful or enjoyable, and
// contact me with any comments or criticisms regarding it.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// 
// **END COPYRIGHT NOTICE**
//
//
// Changelog:
// 0.2 (2009-07-26)
// 	Fixed: recursive init
// 	Fixed: showError $-dependence
// 0.1 (2009-07-12)
// 	original release
//
// -------------------------------------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts, select this script,
// and click Uninstall.
//
// -------------------------------------------------------------------------------------------------

(function(){

// constants
var SCRIPT = {
	name: "Twitter Fix Web Replies",
	namespace: "http://userscripts.org/people/336",
	description: "Prevents discard of in_reply_to data when replies do not begin with the @mention (e.g. for #fixreplies).",
	source: "http://userscripts.org"      // script homepage/description URL
			+ "/scripts/show/53598",
	identifier: "http://userscripts.org"  // script URL
			+ "/scripts/source/53598.user.js",
	version: "0.2",                       // version
	date: (new Date(2009, 7 - 1, 26))     // update date
			.valueOf()
};

// global variables
gstrReplyPrefix = '.'; try{ gstrReplyPrefix = GM_getValue('replyPrefix', gstrReplyPrefix); }catch(x){}

// user settings
try {
	GM_registerMenuCommand(SCRIPT.name + ": set reply prefix", function(){try{
		var newVal = prompt('#fixreplies reply prefix:', gstrReplyPrefix);
		if (newVal !== null){ GM_setValue('replyPrefix', gstrReplyPrefix = newVal); }
	}catch(x){}});
} catch (x) {}

// helper functions
function getUnsafeWindow () {
	try {
		return unsafeWindow;
	}
	catch (x) {
		return (function(){return this;})();
	}
}

function showError (strErr) {
	var c, info;
	try {
		c = $('#content').find('.wrapper')[0];
		info = $('#content').find('.bulletin.info')[0];
	} catch (x) {}
	if (!info){
		info = c ? c.appendChild(document.createElement('div')) : document.body.insertBefore(document.createElement('div'), document.body.firstChild);
		info.className = 'bulletin info';
		info.parentNode.insertBefore(info, info.parentNode.firstChild);
	}
	info.appendChild(document.createElement('span')).innerHTML = [
		 '<a href="'
		,SCRIPT.source
		,'">'
		,SCRIPT.name
		,'</a> error. <a href="mailto:'
		,['richard','.','gibson','@','gm','ail','.','com'].join('')
		,'?subject='
		,encodeURIComponent(SCRIPT.name + ' ' + SCRIPT.version)
		,'+error&amp;body='
		,encodeURIComponent(strErr)
		,'">Click to notify author</a>.'
	].join('');
}

// "real" functions
function init (intTry) {
	var global = getUnsafeWindow(), $, $fn_init, $ajax;
	if (!global.$ && !(intTry >= 15)) {
		var objThis = this, fnThis = arguments.callee;
		window.setTimeout(function(){fnThis.call(objThis, (intTry || 0) + 1);}, 2000);
		return;
	}
	try {
		$ = global.$; $();	// ensure that $ is a function

		// prefix replies
		try {
			$fn_init = $.fn.init;
			$.fn.init = function(C, E) {
				var obj = $fn_init.apply(this, arguments);
				if (C == '#status') {
					var val = obj.val;
					obj.val = function(v) {
						if (/^@\w+/.test(v)){ arguments[0] = gstrReplyPrefix + v; }
						return val.apply(this, arguments);
					};
				}
				return obj;
			};
			$.fn.init.constructor = $fn_init.constructor;
			$.fn.init.prototype = $fn_init.prototype;
				// handle initial value
			var status = $('#status');
			if ($("#in_reply_to").val() && $("#in_reply_to_status_id").val()) {
				status.val(status.val());
			}
		}
		catch (x) {
			if ($fn_init){ $.fn.init = $fn_init; }
			showError(x+'');
		}
		
		// add in_reply_to data for embedded @mentions
		try {
			$ajax = $.ajax;
			$.ajax = function(P) {
				try {
					if (P && P.url == "/status/update") {
						var L = $("#in_reply_to_status_id").val();
						var re = new RegExp('(^|\\W)@' + $("#in_reply_to").val() + '(\\W|$)', 'i');
						if (L && re.test($("#status").val())) {
							P.data.in_reply_to_status_id = L;
						}
					}
					return $ajax.apply(this, arguments);
				} catch (x) { showError(x+''); }
			};
			$.ajax.constructor = $ajax.constructor;
			$.ajax.prototype = $ajax.prototype;
		}
		catch (x) {
			if ($ajax){ $.ajax = $ajax; }
			showError(x+'');
		}
	}
	catch (x) {
		showError(x+'');
	}
}

window.addEventListener("load", init, true);

})();