Skip to content

Instantly share code, notes, and snippets.

@IUnknown68
IUnknown68 / HistoryManager.js
Created August 18, 2015 18:13
History manager.
//==============================================================================
(function(){
var _currentIndex = 0;
var _history = [{
url: location.hash.substr(1) || '/',
index: _currentIndex
}];
var _currentState = _history[_currentIndex];
var _ignorePopstate = false;
@IUnknown68
IUnknown68 / MessageLoop.js
Created August 13, 2015 09:56
A hierarchical message loop
//==============================================================================
//==============================================================================
window.MessageLoop = (function(){
//============================================================================
var _Set;
if ('undefined' !== typeof Set) {
_Set = Set;
}
else {
@IUnknown68
IUnknown68 / DependencyManager.js
Last active August 29, 2015 14:27
The DependencyManager handles asynchronous module initialization with dependencies.
//------------------------------------------------------------------------------
// DependencyManager
// The DependencyManager handles asynchronous module initialization with
// dependencies.
//
// Example:
// =======
// Modules A and B load asynchronously.
// Module C requires A and B and loads synchronously.
// Module D requires C and loads asynchronously.
@IUnknown68
IUnknown68 / createCollectionProxyFn.js
Created June 29, 2015 11:33
Collection function calling each item (Like `setModified()`, `getModified()`).
//------------------------------------------------------------------------------
// Create a function `collection.fnName`.
// The function applies `_.lodashFn` on `collection` and calls `fnName` on
// each item.
// @param collection Any collection lodash can process
// @param fnName Name of function generated.
// @param lodashFn Name of the lodash collection function to apply.
// Defaults to 'each'.
function createCollectionProxyFn(collection, fnName, lodashFn) {
lodashFn = lodashFn || 'each';
@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']
},
{
@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);})();
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 / 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
@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 / 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) {