yourls.js | |
---|---|
yourls-api 1.0.0 | (function (root) {
|
Private variables |
var
|
URL of YOURLS API stored by |
api = '',
|
Authentication credentials stored by |
auth = {},
|
Save the previous value of the |
previousYourls = root.yourls;
|
Public variables |
|
API to be exposed publicly later on. |
var yourls = {};
|
Callback functions for activate JSONP requests. |
yourls.__jsonp_callbacks = {};
|
Public constants |
|
Current version of |
yourls.VERSION = '1.0.0';
|
Private functions |
|
Convert the object provided in to a URL parameter string. |
function paramify(params) {
var key, str = '';
for (key in params) {
if (params.hasOwnProperty(key)) {
if (typeof params[key] !== 'undefined') {
str += key + '=' + params[key] + '&';
}
}
}
return str.replace(/&$/, '');
}
|
Send a JSONP request to the YOURLS API that calls the callback function
with the context specified as |
function jsonp(url, callback, context) {
var
id = +new Date(),
script = document.createElement('script');
while (typeof yourls.__jsonp_callbacks[id] !== 'undefined') {
id += Math.random();
}
yourls.__jsonp_callbacks[id] = function () {
delete yourls.__jsonp_callbacks[id];
callback.apply(context, arguments);
};
url = '?format=jsonp&callback=' +
encodeURIComponent('yourls.__jsonp_callbacks[' + id + ']') + '&' +
url;
url += '&' + paramify(auth);
script.setAttribute('src', api + url);
document.getElementsByTagName('head')[0].appendChild(script);
}
|
Primary functions |
|
Store the URL and user credentials to be used to connect to the YOURLS
API. |
yourls.connect = function (url, credentials) {
api = url;
auth = {};
if (credentials) {
if (credentials.signature) {
auth.signature = credentials.signature;
} else {
auth.password = credentials.password;
auth.username = credentials.username;
}
}
return this;
};
|
Retrieve the short URL for a long URL. |
yourls.shorten = function (url, keyword, callback, context) {
var data = {
action : 'shorturl',
url : url
};
if (typeof keyword === 'function') {
callback = keyword;
} else {
data.keyword = keyword;
}
jsonp(paramify(data), callback, context);
return this;
};
|
Retrieve the statistics for all the shortened URLs which can be optionally filtered. |
yourls.stats = function (filter, limit, callback, context) {
var
data = {action: 'stats'},
filterType = typeof filter;
switch (filterType) {
case 'number':
data.limit = filter;
callback = limit;
break;
case 'function':
callback = filter;
break;
default:
data.filter = filter;
if (typeof limit === 'function') {
callback = limit;
} else {
data.limit = limit;
}
}
jsonp(paramify(data), callback, context);
return this;
};
|
URL functions |
|
The constructor for url objects. |
yourls.url = function (url) {
if (!(this instanceof yourls.url)) return new yourls.url(url);
this.url = url;
};
|
Retrieve the long URL for a short URL. |
yourls.url.prototype.expand = function (callback, context) {
jsonp(paramify({
action : 'expand',
shorturl : this.url
}), callback, context);
return this;
};
|
Retrieve statistics for a single short URL. |
yourls.url.prototype.stats = function (callback, context) {
jsonp(paramify({
action : 'url-stats',
shorturl : this.url
}), callback, context);
return this;
};
|
Utility functions |
|
Run yourls.js in noConflict mode, returning the |
yourls.noConflict = function () {
root.yourls = previousYourls;
return this;
};
|
Export |
if (typeof define === 'function' && define.amd) {
define('yourls', function () {
return yourls;
});
} else {
root.yourls = yourls;
}
}(this));
|