[SVN] Automatically Scroll (Follow) webpreview output

Nick Channon spudnyk at gmail.com
Tue Oct 31 01:05:56 UTC 2006


I had some commands using the web preview window that outputted pages
of information. I wanted to view this information as it arrived but
did not want to scroll manually. So I developed some javascript code
that adds a 'Follow' checkbox to the webpreview header that when
checked automatically scrolls preview window to the bottom as it
updates, much like the unix tail command.

I figured this might be useful to other people out there. So if you
want to give it a try you can just paste the contents onto the end of
$TM_SUPPORT_PATH/script/webpreview.js

Nick
-------------- next part --------------
var _follower = null
function Follower() {
	this._interval = null;
	// Don't Follow output by default
	this._follow = false;
	this._loadPrefs();
	this._injectControl();
	if (this._follow) {
		this.start()
	}
}
Follower.prototype = {
	
	_loadPrefs: function() {
		var result = TextMate.system("defaults read com.macromates.textmate.webpreview follow", null);
		if (result.status == 0)
		{
			this._follow = /1/.test(result.outputString)
		}
	},
	
	_savePrefs: function() {
		var follow = (this._follow) ? 'yes' : 'no';
		TextMate.system("defaults write com.macromates.textmate.webpreview follow -boolean " + follow, null);
	},
	
	_injectControl: function() {
		var self = this
		
		var chkbox = document.createElement('input');
		chkbox.type = 'checkbox';
		chkbox.checked = this._follow;
		chkbox.onclick = function() {
			self.checkboxToggle(chkbox);
		}
		
		var label = document.createElement('label');
		// Webkit does not support the for attribute on label element so we have to do this manually
		label.onclick = function() {
			chkbox.checked = !chkbox.checked;
			// called manually as onchange doesn't fire
			self.checkboxToggle(chkbox);
		}
		label.appendChild(document.createTextNode('Follow:'));
		
		var form = document.getElementById('theme_switcher').getElementsByTagName('form')[0];
		form.insertBefore(chkbox, form.firstChild);
		form.insertBefore(label, chkbox);
	},
	
	checkboxToggle: function(chkbox) {
		this._follow = chkbox.checked;
		if (this._follow) {
			this.start()
		} else {
			this.stop()
		}
		this._savePrefs()
	},
	
	start: function() {
		var self = this;
		this.stop();
		this._interval = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				self.stop();
			}
			window.scroll(0, document.height);
		}, 10);
	},
	
	stop: function() {
		if (this._interval != null) {
			clearInterval(this._interval);
			this._interval = null
		}
	}
}
_follower = new Follower()


More information about the textmate-dev mailing list