Difference between revisions of "MediaWiki:Common.js"

From NSWiki
Jump to: navigation, search
(google analytics)
 
(add utility functions)
Line 7: Line 7:
 
   ga('create', 'UA-47265691-1', 'nswiki.org');
 
   ga('create', 'UA-47265691-1', 'nswiki.org');
 
   ga('send', 'pageview');
 
   ga('send', 'pageview');
 +
 +
    $.QueryString = (function(a) {
 +
        if (a == "") return {};
 +
        var b = {};
 +
        for (var i = 0; i < a.length; ++i)
 +
        {
 +
            var p=a[i].split('=');
 +
            if (p.length != 2) continue;
 +
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
 +
        }
 +
        return b;
 +
    })(window.location.search.substr(1).split('&'))
 +
 +
//Add string.startsWith
 +
if (typeof String.prototype.startsWith != 'function') {
 +
String.prototype.startsWith = function (str){
 +
return this.slice(0, str.length) == str;
 +
};
 +
}
 +
 +
//Add string.endsWith
 +
if (typeof String.prototype.endsWith != 'function') {
 +
String.prototype.endsWith = function (s) {
 +
return this.length >= s.length && this.substr(this.length - s.length) == s;
 +
}
 +
}
 +
 +
//Add string.contains
 +
if (typeof String.prototype.contains != 'function') {
 +
String.prototype.contains = function (str){
 +
return this.indexOf(str) != -1;
 +
};
 +
}
 +
 +
//Add string.toTitleCase
 +
if (typeof String.prototype.toTitleCase != 'function') {
 +
String.prototype.toTitleCase = function (){
 +
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
 +
};
 +
}
 +
 +
if (typeof String.prototype.count != 'function') {
 +
String.prototype.count = function(substr,start,overlap) {
 +
overlap = overlap || false;
 +
start = start || 0;
 +
 +
var count = 0,
 +
offset = overlap ? 1 : substr.length;
 +
 +
while((start = this.indexOf(substr, start) + offset) !== (offset - 1))
 +
++count;
 +
return count;
 +
};
 +
}
 +
 +
//Add escape
 +
RegExp.escape = function(text) {
 +
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
 +
}
 +
 +
//Add replaceAll
 +
String.prototype.replaceAll = function(search, replace) {
 +
return this.replace(new RegExp(RegExp.escape(search),'g'), replace);
 +
};

Revision as of 19:00, 26 January 2014

 
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
 
  ga('create', 'UA-47265691-1', 'nswiki.org');
  ga('send', 'pageview');
 
    $.QueryString = (function(a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i)
        {
            var p=a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'))
 
	//Add string.startsWith
	if (typeof String.prototype.startsWith != 'function') {
		String.prototype.startsWith = function (str){
			return this.slice(0, str.length) == str;
		};
	}
 
	//Add string.endsWith
	if (typeof String.prototype.endsWith != 'function') {
		String.prototype.endsWith = function (s) {
			return this.length >= s.length && this.substr(this.length - s.length) == s;
		}
	}
 
	//Add string.contains
	if (typeof String.prototype.contains != 'function') {
		String.prototype.contains = function (str){
			return this.indexOf(str) != -1;
		};
	}
 
	//Add string.toTitleCase
	if (typeof String.prototype.toTitleCase != 'function') {
		String.prototype.toTitleCase = function (){
			return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
		};
	}
 
	if (typeof String.prototype.count != 'function') {
		String.prototype.count = function(substr,start,overlap) {
			overlap = overlap || false;
			start = start || 0;
 
			var count = 0, 
				offset = overlap ? 1 : substr.length;
 
			while((start = this.indexOf(substr, start) + offset) !== (offset - 1))
				++count;
			return count;
		};
	}
 
	//Add escape
	RegExp.escape = function(text) {
		return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
	}
 
	//Add replaceAll
	String.prototype.replaceAll = function(search, replace) {
		return this.replace(new RegExp(RegExp.escape(search),'g'), replace);
	};