Skip to content

Instantly share code, notes, and snippets.

@IUnknown68
IUnknown68 / logWindowCalls.js
Last active December 31, 2015 01:29
Logs all calls on a DOM window (or any other object) to the console by replacing the functions with a wrapper. NOTE: This does not deal with errors, it is just for debugging purposes.
var target = window;
for (var i in target) {
if ("function" != typeof target[i]) continue;
target[i] = (function(name, fn) {
return function() {
console.log("" + name + " called");
return fn.apply(target, arguments);
}
})(i, target[i]);
}
@IUnknown68
IUnknown68 / parseVersion.js
Last active August 29, 2015 13:57
Semantic Versioning: Extract version info
function parseVersion(s) {
var m = exp.exec(s);
return (m) ?
{
major: m[1],
minor: m[2],
patch: m[3],
pre: m[5] || '',
meta: m[7] || ''
} :
@IUnknown68
IUnknown68 / BlockSizeInBytes
Last active August 29, 2015 13:58
BlockSizeInBytes - block size, alignment
// ---------------------------------------------------------------------------
// BlockSizeInBytes
// Returns the number of bytes required to hold <aCount> objects of type <T>
// in blocks of <aBlockSize> bytes.
template <typename T> inline size_t BlockSizeInBytes(size_t aCount, size_t aBlockSize = 4)
{
return (((aCount * sizeof(T)) + aBlockSize - 1) / aBlockSize) * aBlockSize;
}
@IUnknown68
IUnknown68 / DelegateCollection.js
Last active August 29, 2015 14:06
Array where you can add methods which will be called on each entry.
//==============================================================================
function DelegateCollection() {
for (var i = 0, m = arguments.length; i < m; i++) {
this.addCall(arguments[i]);
}
}
//------------------------------------------------------------------------------
DelegateCollection.prototype = new Array;
@IUnknown68
IUnknown68 / InitManager.js
Created November 22, 2014 11:44
InitManager is a simple state machine, that runs an `init` or `shutdown` function for each state. It has methods to walk up or down to a certain state, and makes sure that each `init` (for walking up) or `shutdown` (waliking down) is called exactly once and only if needed.
//==============================================================================
// class InitManager
function InitManager(arStates) {
var _state = 0;
//----------------------------------------------------------------------------
// privates
// return `aState` limited to a valid arStates index
var _sanitizeState = function(aState) {
@IUnknown68
IUnknown68 / chrome-RPC.js
Last active August 29, 2015 14:10
RPC for chrome. Connects objects between background/content/extension scripts in the most simple way.
//==============================================================================
var RPC = (function(){
var RPC = {};
var _nextConnectionId = 1;
var _nextMessageId = 1;
var _nextObjectId = 1;
/***
* Class _SharedObject
*/
@IUnknown68
IUnknown68 / gist:b32bc4c7b351b9d579f4
Created March 12, 2015 13:00
Constrain image size
bool ConstrainImageSize(
int &aWidth, // in/out - actual image width
int &aHeight, // in/out - actual image height
const int aMaxWidth, // in - max width
const int aMaxHeight // in - max height
) const
{
float ratioNeed = std::max( (float)aWidth / (float)aMaxWidth, (float)aHeight / (float)aMaxHeight );
if( ratioNeed > 1.0 ) {
// actually needs downscaling at all
function formatS(s) {
var args = Array.prototype.slice.call(arguments);
var _replFn = function(all, before, index) {
return before + args[parseInt(index)];
}
return s.replace(formatS._m, _replFn).replace('%%', '%');
}
formatS._m = /([^%])%([0-9]+)/g;
@IUnknown68
IUnknown68 / ShowScripts.js
Last active August 29, 2015 14:22
Bookmark for showing inline JS. Copy the last line of `ShowScripts.js` (without `// `) as content into a new bookmark in your browser. Then in any page click the bookmark. Does not work when the content policy forbids foreign css.
(function(){
var el = document.createElement('link');
el.rel="stylesheet";
el.type="text/css";
el.href="http://www.seiberspace.de/media/show-scripts.css";
document.head.appendChild(el);
})();
// for pasting into bookmarks:
// javascript:(function(){var%20el%20=%20document.createElement('link');el.rel="stylesheet";el.type="text/css";el.href="http://www.seiberspace.de/media/show-scripts.css";document.head.appendChild(el);})();
@IUnknown68
IUnknown68 / getLatestVersion
Created June 18, 2015 13:21
Small algorithm for getting a list of latest versions from a version history.
var g = [
{
v: '1.0.0',
f: ['README.md', 'new/images/image1.jpg','new/images/image2.jpg','new/images/image3.jpg']
},
{
v: '0.2.0',
f: ['README.md', 'foo/bar/bar.txt']
},
{