Skip to content

Instantly share code, notes, and snippets.

@marcojetson
marcojetson / jquery.scrolldirection.js
Created January 16, 2014 15:59
jQuery - Adds scroll direction to scroll event
(function ($) {
"use strict";
var wnd = $(window),
lastScrollTop = wnd.scrollTop(),
lastScrollLeft = wnd.scrollLeft();
wnd.on("scroll", function (event) {
var scrollTop = wnd.scrollTop(),
scrollLeft = wnd.scrollLeft(),
@marcojetson
marcojetson / service-status.js
Last active August 29, 2015 13:56
Detect if visitor is logged into Google, Google Plus, Taringa! or Twitter
// check<Service>Status([ onSuccess, onFailure ]);
// i.e checkTaringaStatus(function () { alert('+10'); });
(function () {
var checkServiceStatus = function (image, success, error) {
return function (success, error) {
var img = new Image();
img.onload = success || function () {};
img.onerror = error || function () {};
img.src = image;
};
@marcojetson
marcojetson / brainfuck.c
Last active August 29, 2015 13:56
brainfuck interpreter
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *p = malloc(1024);
int l[10], j = 0, i = 0;
while (argv[1][i]) {
switch (argv[1][i++]) {
@marcojetson
marcojetson / quine.php
Created June 15, 2015 14:12
quine.php
<? $a='<? $a=\'%s\';printf($a,addslashes($a));';printf($a,addslashes($a));
@marcojetson
marcojetson / jquery-ajaxforms.js
Created June 16, 2015 08:56
convert forms to ajax forms
$('body').on('submit', 'form.ajax', function () {
var form = $(this);
$.ajax({
type: form.attr('method') || 'GET',
url: form.attr('action') || location.href,
data: form.serialize(),
success: function (data, textStatus, jqXHR) {
form.trigger('success', [data, textStatus, jqXHR]);
},
@marcojetson
marcojetson / fix-api.php
Created June 19, 2015 09:31
PHP API is weird
<?php
/**
* Checks if a value exists in an array
*
* @param array $haystack
* @param mixed $needle
* @param bool $strict
* @return bool
*/
@marcojetson
marcojetson / lilidns-updater.sh
Last active August 29, 2015 14:25
lilidns.com cli host updater
#!/bin/bash
if [ -z "$1" ]
then
echo "Usage $0 <token> [ip]"
exit
fi
if [ -n "$2" ]
then
@marcojetson
marcojetson / IDateTime.php
Last active August 29, 2015 14:25
DateTime supporting microseconds
<?php
class IDateTime extends \DateTime
{
public function __construct($time = 'now', $timezone = null)
{
if ($time === null || $time === 'now') {
$ts = microtime(true);
$micro = sprintf('%06d',($ts - floor($ts)) * 1000000);
$time = date('Y-m-d H:i:s.' . $micro, $ts);
@marcojetson
marcojetson / ObjectId.php
Created July 24, 2015 15:06
Mongo inspired unique and sortable id generation
<?php
class ObjectId
{
/** @var string */
private $value;
/**
* @param string $value
*/
@marcojetson
marcojetson / duration.php
Created July 30, 2015 09:29
Take a number of seconds and return it in weeks/days/hours/minutes/seconds format. (like mIRC script's $duration identifier)
<?php
/**
* @param int $seconds
* @return string
*/
function duration($seconds)
{
foreach (['wk' => 604800, 'day' => 86400, 'hr' => 3600, 'min' => 60, 'sec' => 1] as $symbol => $subunit) {
$value = (int) floor($seconds / $subunit);