Skip to content

Instantly share code, notes, and snippets.

View lebbe's full-sized avatar

Lars-Erik Bruce lebbe

View GitHub Profile
@lebbe
lebbe / Optional.java
Last active August 29, 2015 14:01
For those who want an Optional but haven't upgraded to Java 8 yet. If you use this, it should be fairly simple to refactor using the "real" Optional in the future.
import java.util.NoSuchElementException;
/**
* My own little implementation of optional. Lacks other Java 8 features such as filter on Predicate,
* eating the value up with a consumer, etc.
* <p>
* One possible benefit: If you try to get() the value, but haven't checked whether it isPresent(),
* this will throw an exception. You must always check if the value is present before getting it.
* <p>
* Otherwise, this API intends to follow the java.util.Optional<T> Java 8 API. It should be relatively
window['defineModule'] = function defMod(name, moduleDefiner) {
window[name] = window[name] || (Object.create ? Object.create(null) : {});
var args = Array.prototype.slice.call(arguments, 2);
var API = moduleDefiner.apply(window[name], args);
for(var b in API)
window[name][b] = API[b];
modules[name] = window[name];
}
@lebbe
lebbe / har.html
Last active August 29, 2015 14:04
Create a simple overview over network resources with a HAR-file from chrome debugger.
<!DOCTYPE html>
<html>
<head>
<script src="test.har"></script>
<script type="text/javascript">
// Insert "var test = " at the very beginning of the HAR-file
var entries = test.log.entries;
@lebbe
lebbe / includeJquery.js
Created August 8, 2014 12:09
A little snippet to make sure that jQuery is loaded before running code.
(function($, callback) {
if($) {
callback();
return;
}
var script = document.createElement('script');
script.src = '//code.jquery.com/jquery-1.11.1.min.js';
script.onload = callback;
document.getElementsByTagName('body')[0].appendChild(script);
})(window['jQuery'], function() {
/**
* Detects whether the PDF.js Chromium extension is installed in the browser.
* Example: isPDFJSExtensionInstalled(function(result) { alert('Is PDF.js installed? ' + result);});
*
* @param {function(boolean)} callback - Called when the detection completed.
*
* Author: Rob Wu <rob@robwu.nl> (https://robwu.nl)
*
* Made synchronous by a handwaiving Lebbe
*
@lebbe
lebbe / psyche.js
Created October 7, 2014 13:09
Transform any web-page into a psychedelic experience
window.setInterval(function() {
var levelColors = [], m = Math;
function getLevelColor(level) {
if(levelColors[level] === undefined) {
levelColors[level] = (m.PI * m.random()).toString(16).slice(-2);
}
return levelColors[level];
}
@lebbe
lebbe / weird.js
Created December 4, 2014 13:04
Recursive DOM node promises stuff
/**
*
*/
function promiseNode(name, attributes) {
/**
*
*/
return function (content, beforeInsideOrAfter) {
var start = '<' + name;
var end = '</' + name + '>';
@lebbe
lebbe / twitter-miner.js
Last active August 29, 2015 14:15
Poor mans twitter mining tool
/* Write out for months. */
var dates = [];
$('.js-stream-item ._timestamp').each(function() {
dates.push(new Date($(this).data('time-ms')));
});
var data = {};
@lebbe
lebbe / basicServer.js
Last active August 29, 2015 14:21
Creating a basic web-app in express+family
/*
This is the most basic setup you need in express 4 for listening on POST and GET.
*/
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
@lebbe
lebbe / testStorageAbilities.html
Last active December 13, 2015 19:28
Something I threw together to test storage abilities in different browsers.
<!--
Something I threw together to test storage abilities in different browsers.
This test two forms of session storages: html 5 session storages, and the window.name fallback option.
It also tests two forms of persistent storage: html 5 local storage and cookies.
Intended usage:
Upload the html file to your web-server.