Userscripts.org Scripts Source Counter

By jerone Last update Jan 27, 2011 — Installed 255 times.

There are 6 previous versions of this script.

/*//////////////////////////////////////////////////////////////////////////
// ==UserScript===
// @name            Userscripts.org Scripts Source Counter
// @author          Jerone UserScript Productions
// @namespace       http://userscripts.org/users/31497
// @homepage        http://userscripts.org/scripts/show/37611
// @description     Count all characters, words and lines for scriptwriters.
// @description     Userscripts.org Scripts Source Counter v2.0 Alpha
// @copyright       2008 - 2011 Jerone
// @version         v2.0 Alpha
// @versiontext     Removed Framework.
// @browser         FF3
// @include         http://userscripts.org/scripts/new?form=true
// @include         http://userscripts.org/scripts/edit_src/*
// ==/UserScript==
////////////////////////////////////////////////////////////////////////////
// ToC:
// - Copyrights
// - History
// - Todo
// - Note
// - Userscript
// - Statistics
////////////////////////////////////////////////////////////////////////////
THIS  SCRIPT  IS  PROVIDED BY THE AUTHOR `AS IS' AND ANY EXPRESS OR IMPLIED
WARRANTIES,  INCLUDING, BUT  NOT  LIMITED  TO, THE  IMPLIED  WARRANTIES  OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
EVENT  SHALL  THE  AUTHOR  BE  LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER  CAUSED  AND  ON  ANY THEORY OF LIABILITY,
WHETHER  IN  CONTRACT, STRICT  LIABILITY, OR  TORT  (INCLUDING NEGLIGENCE OR
OTHERWISE)  ARISING  IN  ANY  WAY  OUT  OF  THE  USE OF THIS SCRIPT, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
////////////////////////////////////////////////////////////////////////////
// History:
// [+] = added; [-] = removed; [/] = fixed; [*] = updated;
26-11-2008 17:00 [v1 Alpha]:
	[+] initial release;
28-11-2008 01:00 [v1.1 Beta]:
	[/] fixed small bug in text;
	[+] now scrolls to added text;
10-01-2009 18:00 [v1.2 Beta]:
	[*] updated framework;
27-01-2011 23:30 [v2.0 Alpha]:
	[*] removed US Framework;
	[*] clean up code;
////////////////////////////////////////////////////////////////////////////
// Todo:
// - remember old stats and compair;
////////////////////////////////////////////////////////////////////////////
// Note:
// -
/*//////////////////////////////////////////////////////////////////////////



//*** USER SCRIPT ***//
(function (win, doc, und) {

	win.Number.prototype.toPoints = function(){return(this+"").replace(/(\d)(?=(\d{3})+$)/g,'$1.');};
	var addEvent = function(node,type,fn,useCapture){if(node.addEventListener){node.addEventListener(type,fn,useCapture);}else if(node.attachEvent){node["e"+type+fn]=fn;node[type+fn]=function(){node["e"+type+fn](win.event);};node.attachEvent("on"+type,node[type+fn]);}};

	var USSC = {
		init: function(){
			var obj, node, btn;
			if((obj = doc.getElementById("script_src"))){
				obj.style["white-space"] = "pre-wrap";
				
				btn = doc.createElement("input");
				btn.type = "button";
				btn.value = "Count!";
				btn.style["margin-right"] = "10px";
				btn.title = "Count all characters, words and lines and include the stats at the end!";
				
				node = obj.nextSibling;
				while(node.nodeType==3 || !/\S/.test(node.nodeValue)){
					node = node.nextSibling;
				}
				if(/new/i.test(location.href)){
					node.parentNode.insertBefore(btn, node.nextSibling);
				} else {
					node.insertBefore(btn, node.firstChild);
				}
				
				addEvent(btn, "click", function(){
					if(obj.value.match("STATISTICS")){
						obj.value = obj.value.replace(/\n*\/\/\*\*\* STATISTICS.*(\n.*)*$/, "");
					}
					obj.value += USSC.write(obj.value + USSC.write(obj.value));
					obj.scrollTop = obj.scrollHeight;
				});
			}	
		},
		write: function(txt){
			var msg = "\n\n\n\n//*** STATI" + "STICS ***//" + "\n";
				msg += "// Chars (exclude spaces): " + Count.charsExSpace(txt).toPoints() + "\n";
				msg += "// Chars (include spaces): " + Count.charsInSpace(txt).toPoints() + "\n";
				msg += "// Chars (Chinese): " + Count.charsChinese(txt).toPoints() + "\n";
				msg += "// Words: " + Count.words(txt).toPoints() + "\n";
				msg += "// Lines: " + Count.lines(txt).toPoints();
			return msg;
		}
	};

	var Count = {
		charsExSpace: function(str){
			return str.replace(/\s/gi, "").length;
		},
		charsInSpace: function(str){
			return str.length - Count.lines(str) + 1;
		},
		charsChinese: function(str){
			var c = 0, i = 0;
			for(; i<str.length; i++){
				if(str.charCodeAt(i)>255){
					c++;
				}
			}
			return c;
		},
		words: function(str){
			return str.split(/\s/g).filter(function(item){ return !!item!=""; }).length;
		},
		lines: function(str){
			return str.split(/\n/gi).length;
		}
	};

	USSC.init();  // execute;

})(this, document);



//*** STATISTICS ***//
// Chars (exclude spaces): 4.115
// Chars (include spaces): 4.843
// Chars (Chinese): 0
// Words: 513
// Lines: 141