Skip to content

Instantly share code, notes, and snippets.

View ghostwriter's full-sized avatar
🐘
while(!succeed=try())

Nathanael Esayeas ghostwriter

🐘
while(!succeed=try())
View GitHub Profile
@ghostwriter
ghostwriter / 408721.js
Created December 10, 2014 12:44
On Firefox, offsetWidth and offsetHeight are undefined on a SVG element.
getSize : function () {
if (isBody(this))
return this.getWindow().getSize();
return {
x : this.offsetWidth || styleNumber(this, 'width'),
y : this.offsetHeight || styleNumber(this, 'height')
};
}
@ghostwriter
ghostwriter / gist:bcaba56977f20b14dc5b
Last active July 1, 2022 07:28
BattleTale Bug #29 (reproducible bug)
/*
1. post a link in chat. (ex. https://github.com/)
2. post anything with 3 or more "w"s in a row. (ex. www, wowww,wwwwwwwwww)
3. use {/geturl} and you'll get the word with the 3 "w" in it and not the url.
Fix: Validate url.
*/
var url:String = "http://www.google.com";
var regex:RegExp = /^http(s)?:\/\/((\d+\.\d+\.\d+\.\d+)|(([\w-]+\.)+([a-z,A-Z][\w-]*)))(:[1-9][0-9]*)?(\/([\w-.\/:%+@&=]+[\w- .\/?:%+@&=]*)?)?(#(.*))?$/i;
trace(regex.test(url)); // returns true if valid url is found
@ghostwriter
ghostwriter / js_addevent.md
Created January 21, 2016 07:56
Allows you to assign an event handler to an element.

addEvent ()

Allows you to assign an event handler to an element.

Code:

function addEvent(obj, eventName, callback){
	if (obj.addEventListener){
		obj.addEventListener(eventName, callback, false);
	} else if (obj.attachEvent){
@ghostwriter
ghostwriter / OrderByRandomTrait.php
Created February 4, 2016 19:14
Laravel Eloquent OrderByRandom
<?php
trait OrderByRandomTrait
{
/**
* Scope to order by random.
*
* @param \Illuminate\Database\Query\Builder $query
* @return \Illuminate\Database\Query\Builder
*/
public function scopeOrderByRandom($query)
@ghostwriter
ghostwriter / User.php
Created February 4, 2016 19:41
Laravel Eloquent Query Scopes - OrderByRandom
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Scope a query to return a random users Model.
@ghostwriter
ghostwriter / dom_insertion.js
Created February 4, 2016 20:05
Quick 'n Dirty DOM insertion Function when there is no JS library
// More elaborate code can be found here:
// https://github.com/jquery/jquery/blob/master/src/manipulation.js
function prepend(target, elem) {
target.insertBefore(elem, target.firstChild);
}
function append(target, elem) {
target.appendChild(elem);
}
@ghostwriter
ghostwriter / relative-size.js
Created February 4, 2016 20:07
Increase or decrease CSS values with jQuery
/*
cleaner than hard-coding the desired font size
refs: http://api.jquery.com/css/
some use case:
all these websites that use *huge* font sizes,
painful to read even on a 1920x1080 screen.
*/
$('.article-content').css('font-size', '-=2');
@ghostwriter
ghostwriter / cookies.js
Created February 4, 2016 20:15
Simple, no-dependency JavaScript to GET, SET and DELETE cookies
function getCookie(name) {
var cname = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1);
if (c.indexOf(cname) == 0)
return c.substring(cname.length, c.length);
}
@ghostwriter
ghostwriter / TCP_port_proxy.js
Created February 12, 2016 05:40
A basic TCP port proxy can be used for nearly all TCP traffic.
/*
It was just an idea I had to help mitigate some ddos by having many servers and hiding the real ip of the server using this then we could monitor each one and filter or turn them off as needed etc.
*/
var net = require('net');
function ProxyServer(localport, remoteip, remoteport) {
if (remoteport === undefined)
remoteport = localport;
var server = net.createServer({
allowHalfOpen : false,
@ghostwriter
ghostwriter / animateDecorator.js
Created April 29, 2016 22:17
custom ngAnimate decorator
'use strict';
var forEach = angular.forEach;
angular.module('ngTouchNav')
.config(function($animateProvider) {
$animateProvider.register('', ['$window','$sniffer', '$timeout', function($window, $sniffer, $timeout) {
var noop = angular.noop;