Created
May 3, 2010 19:49
-
-
Save George1/388518 to your computer and use it in GitHub Desktop.
Contains utility functions commonly-used in my Greasemonkey scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Commonly used utility functions for Greasemonkey scripts | |
* | |
* Copyright © 2010, Yuri Sakhno | |
*/ | |
// version 1.1 | |
if (!Array.prototype.indexOf) { | |
Array.prototype.indexOf = function(elt, from) { | |
var len = this.length >>> 0; | |
from = Number(from) || 0; | |
from = (from < 0) ? Math.ceil(from) : Math.floor(from); | |
if (from < 0) { | |
from = Math.max(0, from + len); | |
} | |
for (; from < len; ++from) { | |
if (from in this && this[from] === elt) return from; | |
} | |
return -1; | |
}; | |
} | |
if (!Array.prototype.lastIndexOf) { | |
Array.prototype.lastIndexOf = function(elt, from) { | |
var len = this.length; | |
from = Number(from) || len-1; | |
from = (from < 0) ? Math.ceil(from) : Math.floor(from); | |
if (from < 0) { | |
from += len; | |
} else if (from >= len) { | |
from = len - 1; | |
} | |
for (; from > -1; --from) { | |
if (from in this && this[from] === elt) return from; | |
} | |
return -1; | |
}; | |
} | |
Date.prototype.daysInMonth=function(){var n=31;switch(this.getMonth()){case 1:n=this.isLeapYear()?29:28;break;case 3: | |
case 5:case 8:case 10:--n}return n};Array.prototype.chop=function(s){var i=0,c=-1,a=[];for(;i<this.length;++i)i%s?a[c]. | |
push(this[i]):a[++c]=[this[i]];return a};Date.prototype.isLeapYear=function(){var y=this.getFullYear();return!(y%400)||( | |
y%100!=0&&y%4==0)};Number.prototype.clamp=function(l,u){return Math.max(l,Math.min(this,u))};Number.prototype. | |
toHexString=function(w){if(w==undefined)w=1;var s=this.toString(16).upper();while(s.length<w)s="0"+s;return s};String. | |
prototype._split=String.prototype.split;String.prototype.contains=function(s){return-1<this.indexOf(s)};String.prototype | |
.endsWith=function(s){var i=this.lastIndexOf(s);return i+1&&i==this.length-s.length};String.prototype.startsWith= | |
function(s){return!this.indexOf(s)};String.prototype.replaceFast=function(t,r,f){return this.split(t,f?f+1:null).join(r) | |
};String.prototype.lower=function(){return this.toLowerCase()};String.prototype.upper=function(){return this.toUpperCase | |
()};String.prototype.lowerL=function(){return this.toLocaleLowerCase()};String.prototype.upperL=function(){return this. | |
toLocaleUpperCase()};String.prototype.reverse=function(){return this._split("").reverse().join("")};String.prototype. | |
split=function(d,l){var s=this._split(d);if(l&&s.length>l)s.push(s.splice(l-1).join(d));return s};String.prototype.trim= | |
function(){var s=0,e=this.length-1;for(;e+1;--e)if(this[e]>' ')break;for(;s<=e;++s)if(this[s]>' ')break;return s>e?"":s | |
||e+1!=this.length?this.substr(s,e+1-s):this} | |
function byId(i){return document.getElementById(i)}function removeChildNode(n){if(n)n.parentNode.removeChild(n)}function | |
removeAllChildren(e){for(var c=e.childNodes,i=c.length;i;--i)e.removeChild(c[i-1])}function selectSingleNode(p,c,d){d=d | |
||document;var n=d.evaluate(p,c||d,null,XPathResult.ANY_TYPE,null).iterateNext();return n}function insertNodeAfter(n,r){ | |
var s=r.nextSibling,p=r.parentNode;if(s)p.insertBefore(n,s);else p.appendChild(n);return n}function setCookie(n,v,e,p,d, | |
s){var t=new Date;t.setTime(t.getTime()+(e?e*72e5:0));document.cookie=n+"="+escape(v)+(e?";expires="+t.toGMTString():"") | |
+(p?";path="+p:"")+(d?";domain="+d:"")+(s?";s":"")}function selectNodes(p,c){return document.evaluate(p,c||document,null | |
,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null)}function getParamFromStr(s,n,i,j){var b=0,c='a',e;n+="=";while(c>' '&&c | |
!=i&&c!=j){b=s.indexOf(n,b);if(b<0)return null;c=b?s[b-1]:j;b+=n.length}e=s.indexOf(i,b);return unescape(s.slice(b,e<0?s | |
.length:e))}function deleteCookie(n,p,d){if(getCookie(n))document.cookie=n+"="+";expires=Thu, 01 Jan 1970 00:00:01 GMT"+ | |
(p?";path="+p:"")+(d?";domain="+d:"")}function getCookie(n){return getParamFromStr(document.cookie,n,';',' ')}function | |
getNextElementByTagName(c,t,i){if(!c)return null;if(i==null)i=true;if(t)t=t.lower();for(c=c.nextSibling;c&&(c.nodeType!= | |
1||c.tagName.lower()!=t);c=c.nextSibling)if((!t||i)&&c.nodeType==1)return!t?c:null;return c}function getElementByTagName | |
(p,t,x){if(!p)return null;if(!x)x=0;t=t.lower();for(var i=0,c=p.childNodes;i<c.length;++i){p=c[i];if(p.nodeType==1&&p. | |
tagName.lower()==t&&!x--)return p}return null}function getPreviousElementByTagName(c,t,i){if(!c)return null;if(i==null)i | |
=true;if(t)t=t.lower();for(c=c.previousSibling;c&&(c.nodeType!=1||c.tagName.lower()!=t);c=c.previousSibling)if((!t||i)&& | |
c.nodeType==1)return!t?c:null;return c}function getAllElementsByTagName(p,t){if(!p)return[];t=t.lower();var e=[],c=p. | |
childNodes,i;for(i=0;i<c.length;++i){p=c[i];if(p.nodeType==1&&p.tagName.lower()==t)e.push(p)}return e}function | |
addGlobalStyle(c){var n=selectNodes("//style|//link[@type='text/css'][@rel='stylesheet']"),s=document.createElement( | |
"style");if(n.snapshotLength>0)insertNodeAfter(s,n.snapshotItem(n.snapshotLength-1));else document.body.appendChild(s);s | |
.type="text/css";s.textContent=c}function getParameterValueFromURL(u,n){return getParamFromStr(u,n,'&','?')} | |
function UrlParameter(/*String*/ name, /*String*/ value) { | |
this.setValue = function(/*String*/ value) { | |
this.value = value ? unescape(value) : ""; | |
}; | |
this.toString = function() { | |
return this.name + "=" + escape(this.value); | |
}; | |
this.name = name; | |
this.setValue(value); | |
} | |
function UrlParameters() { | |
this.add = function(/*String*/ name, /*String*/ value) { | |
var i = this.p.length; | |
this.p[i] = new UrlParameter(name, value); | |
if (this.contains(name)) { | |
++this.c[name]; | |
} else { | |
this.c[name] = 1; | |
this.i[name] = i; | |
} | |
}; | |
this.addQuery = function(/*String*/ query) { | |
this.clear(); | |
var params = query.split("&"); | |
for (var i = 0; i < params.length; ++i) { | |
var pair = params[i].split("=", 2); | |
if (pair.length == 2) this.add(pair[0], pair[1]); | |
} | |
}; | |
this.clear = function() { | |
this.p = []; | |
this.i = []; | |
this.c = []; | |
}; | |
this.contains = function(/*String*/ name) { | |
return this.c[name]; | |
}; | |
this.get = function(/*String*/ name) { | |
return this.c[name]?this.p[this.i[name]].value:null; | |
}; | |
this.getAll = function(/*String*/ name) { | |
if (!this.c[name]) return []; | |
if (this.c[name] == 1) return[this.get(name)]; | |
var arr = [], i; | |
for (i = 0; i < this.p.length; ++i) { | |
if (this.p[i].name == name) arr.push(this.p[i].value); | |
} | |
return arr; | |
}; | |
this.isEmpty = function() { | |
return !this.p.length; | |
}; | |
this.remove = function(/*String*/ name) { | |
if (this.c[name]) { | |
this.p.splice(this.i[name], 1); | |
if (--this.c[name]) { | |
for (var i = this.i[name]; i < this.p.length && this.p[i].name != name; this.i[name] = ++i); | |
} | |
} | |
} | |
this.removeAll = function(/*String*/ name) { | |
if (this.c[name]) { | |
for (var i = this.p.length-1; i+1; --i) { | |
if (this.p[i].name == name) this.p.splice(i, 1); | |
} | |
this.c[name] = 0; | |
} | |
}; | |
this.set = function(/*String*/ name, /*String*/ value) { | |
if (this.c[name]) { | |
this.p[this.i[name]].setValue(value); | |
} else { | |
this.add(name, value); | |
} | |
}; | |
this.toString = function() { | |
var s = ""; | |
for (var i = 0; i < this.p.length; ++i) { | |
s += (i ? "&" : "?") + this.p[i]; | |
} | |
return s; | |
}; | |
this.clear(); | |
} | |
function Url(/*String*/ url) { | |
this.setUrl = function(/*String*/ url) { | |
this.scheme = null; | |
this.host = null; | |
this.port = null; | |
this.path = null; | |
this.search = new UrlParameters(); | |
this.anchor = null; | |
var start = 0, | |
end = url.indexOf("#"); | |
if (end+1) { | |
this.anchor = url.substr(end+1); | |
url = slice(0, end); | |
} | |
end = url.indexOf("://"); | |
if (end+1) { | |
this.scheme = (end > 0) ? url.slice(start, end) : null; | |
start = end + 3; | |
} | |
if (this.scheme) { | |
end = url.indexOf("/", start); | |
if (start < end) { | |
var end2 = url.indexOf(":", start); | |
if (start < end2 && end2 < end) { | |
this.host = url.slice(start, end2++); | |
this.port = (end2 < end) ? url.slice(end2, end) : null; | |
} else { | |
this.host = url.slice(start, end); | |
} | |
start = end; | |
} | |
} | |
end = url.indexOf("?"); | |
if (start <= end) { | |
this.path = (start<end) ? url.slice(start, end++) : null; | |
this.search.addQuery(url.substr(end)); | |
} else { | |
this.path = url.substr(start); | |
} | |
} | |
this.toString = function() { | |
return (this.scheme ? this.scheme + "://" : "") + | |
(this.host ? this.host + (this.port ? ":" + this.port : "") : "") + | |
(this.path || "") + | |
this.search + | |
(this.anchor ? "#" + this.anchor : ""); | |
} | |
this.setUrl(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment