userscripts/lib.js

73 lines
1.5 KiB
JavaScript

// Nice overview:
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
if (typeof AdiUserscriptLib != 'object')
(function(public){
public.interpolate = function (s, a) {
// Oh, well... http://stackoverflow.com/a/16804092
for (var x in a) {
var token = '{' + x + '}'
var i = 0
while ( (i = s.indexOf(token, i)) >= 0) s = s.replace(token, a[x])
}
return s
}
public.pad = function (x, len, char) {
x = x.toString()
while (x.length < len) x = char + x
return x
}
public.bind3 = function (fn, arg1, arg2, arg3) {
return function (a4,a5,a6) {
return fn(arg1,arg2,arg3,a4,a5,a6)
}
}
public.binder3 = function (fn, callback) {
return function (arg1, arg2, arg3) {
var bound = public.bind3(fn, arg1, arg2, arg3)
if (callback) callback(bound)
}
}
public.doWhenReady = function (fn) {
$(document).ready(fn)
}
public.makeDoWhenReadyFn = function (fn) {
return public.binder3(fn, public.doWhenReady)
}
public.addButton = (function () {
var left = 12
var top = 64
var space = 32
var makeDoAndRemoveFn = function (fn, el) {
return function () {
fn()
el.text(el.text() + ' ✓')
}
}
var fn = function (name, fn) {
var el = $('<button>' + name + '</button>')
.css('position', 'fixed')
.css('top', top)
.css('left', left)
.css('z-index', 1000)
el.click(makeDoAndRemoveFn(fn, el))
$(document.body).append(el)
top = top + space
}
return public.makeDoWhenReadyFn(fn)
}())
}(AdiUserscriptLib = {}))