Skip to content

Instantly share code, notes, and snippets.

View superbiche's full-sized avatar
🕹️
Working from everywhere

Michel Tomas superbiche

🕹️
Working from everywhere
View GitHub Profile
@superbiche
superbiche / insert-jquery.js
Created May 20, 2015 13:24
Insert jQuery in a page with the DevTools
(function() {
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
window.setTimeout(function() {
jQuery.noConflict();
}, 5000);
})();
@superbiche
superbiche / click-all-buttons.js
Last active August 29, 2015 14:21
Trigger click on all buttons in a page.
(function() {
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
window.setTimeout(function() {
jQuery.noConflict();
}, 5000);
})();
@superbiche
superbiche / geolocation-with-fallback.js
Last active August 29, 2015 14:22 — forked from paulirish/gist:366184
HTML5 Geolocation with Google API fallback. Forked from https://gist.github.com/paulirish/366184
// geo-location shim
// currentely only serves lat/long
// depends on jQuery
// doublecheck the ClientLocation results because it may returning null results
;(function(geolocation){
if (geolocation) return;
// Converts from degrees to radians.
Math.toRadians = function(degrees) {
return degrees * Math.PI / 180;
};
 
// Converts from radians to degrees.
Math.toDegrees = function(radians) {
return radians * 180 / Math.PI;
};
@superbiche
superbiche / debounce.js
Last active August 29, 2015 14:23
http://davidwalsh.name/javascript-debounce-function The debounce function can be a game-changer when it comes to event-fueled performance. If you aren't using a debouncing function with a scroll, resize, key* event, you're probably doing it wrong. The debounce function will not allow a callback to be used more than once per given time frame. Thi…
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@superbiche
superbiche / poll.js
Last active August 29, 2015 14:23
http://davidwalsh.name/javascript-polling As I mentioned with the debounce function, sometimes you don't get to plug into an event to signify a desired state -- if the event doesn't exist, you need to check for your desired state at intervals.
function poll(fn, callback, errback, timeout, interval) {
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
(function p() {
// If the condition is met, we're done!
if(fn()) {
callback();
}
// If the condition isn't met but the timeout hasn't elapsed, go again
@superbiche
superbiche / once.js
Created June 30, 2015 08:00
From http://davidwalsh.name/javascript-once Every so often you have a function which you only want to run once. Oftentimes these functions are in the form of event listeners which may be difficult to manage. Of course if they were easy to manage, you'd just remove the listeners but that's a perfect world and sometimes you simply want the ability…
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
@superbiche
superbiche / matchesSelector.js
Created June 30, 2015 08:26
http://davidwalsh.name/element-matches-selector Oftentimes we validate input before moving forward; ensuring a truthy value, ensuring forms data is valid, etc. But how often do we ensure an element qualifies for moving forward? You can use a matchesSelector function to validate if an element is of a given selector match.
function matchesSelector(el, selector) {
var p = Element.prototype;
var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function(s) {
return [].indexOf.call(document.querySelectorAll(s), this) !== -1;
};
return f.call(el, selector);
}
// Usage
matchesSelector(document.getElementById('myDiv'), 'div.someSelector[some-attribute=true]')
@superbiche
superbiche / randomRange.js
Created June 30, 2015 08:31
randomRange provides a random value between min and max.
function rand(min, max, integer) {
var r = Math.random() * (max - min) + min;
return integer ? r|0 : r;
}
console.log(rand(2,5)); // float random between 2 and 5 inclusive
console.log(rand(1,100,true)); // integer random between 1 and 100
@superbiche
superbiche / stripTags.js
Created June 30, 2015 08:32
Strip tags
function stripTags(htmlstr) {
var div = document.createElement('div');
div.innerHTML = htmlstr;
return div.textContent;
}