Skip to content

Instantly share code, notes, and snippets.

View celsobessa's full-sized avatar

Celso Bessa celsobessa

View GitHub Profile
@celsobessa
celsobessa / bandwidth.js
Last active June 25, 2021 14:58 — forked from debloper/bandwidth.js
Determine client's connection speed with JavaScript
// Let's initialize the primitives
var startTime, endTime, fileSize, result, unit = {kbps:1,kBps:8};
// Set up the AJAX to perform
var xhr = new XMLHttpRequest();
// Rig the call-back... THE important part
xhr.onreadystatechange = function () {
// we only need to know when the request has completed
@celsobessa
celsobessa / Links
Created June 5, 2021 20:41 — forked from lukecav/Links
Speed Up Your WordPress Site with These 3 Advanced Techniques Workshop - WordSesh 2021
@celsobessa
celsobessa / trap_focus.js
Created June 5, 2021 20:39 — forked from myogeshchavan97/trap_focus.js
Code for trapping focus inside modal
// add all the elements inside modal which you want to make focusable
const focusableElements =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const modal = document.querySelector('#exampleModal'); // select the modal by it's id
const firstFocusableElement = modal.querySelectorAll(focusableElements)[0]; // get first element to be focused inside modal
const focusableContent = modal.querySelectorAll(focusableElements);
const lastFocusableElement = focusableContent[focusableContent.length - 1]; // get last element to be focused inside modal
@celsobessa
celsobessa / wp_add_preload_resource_hint.php
Created December 30, 2020 17:31
Writes preload resource hints to HTML element generated by wp_head function on WordPress.
// Adds preload resource hint to wp_head hook
add_action( 'wp_head', 'add_preload_resource_hint', -1);
/**
* Writes preload resource hints.
*
* Writes preload resource hints.
*/
function add_preload_resource_hint() {
@celsobessa
celsobessa / byte-to-units.js
Created November 8, 2020 03:15
Convert byte units to reasonable units
// https://medium.com/javascript-in-plain-english/7-javascript-utility-functions-to-improve-your-efficiency-79d27132f186
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
var i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}
@celsobessa
celsobessa / camelize.js
Created November 8, 2020 03:12
Convert hyphens to camelCase
function camelize(str) {
const camelizeRE = /-(\w)/g;
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
}
@celsobessa
celsobessa / wowp-batch-image-optimization.sh
Created September 11, 2020 20:32
Shell script for batch image optimization with Imageoptim from command line as used by WoWPerations team (www.wowperations.com.br)
# Batch image optimization with Imageoptim from command line as used by WoWPerations team (www.wowperations.com.br
mdfind -onlyin . "kMDItemPixelWidth<480 && kMDItemContentType=public.jpg" > imagesLess480JPG.txt
mdfind -onlyin . "kMDItemPixelWidth>479 && kMDItemPixelWidth<760 && kMDItemContentType=public.jpg" > imagesLess760JPG.txt
mdfind -onlyin . "kMDItemPixelWidth>759 && kMDItemPixelWidth<1024 && kMDItemContentType=public.jpg" > imagesLess1024JPG.txt
mdfind -onlyin . "kMDItemPixelWidth>1023 && kMDItemPixelWidth<1280 && kMDItemContentType=public.jpg" > imagesLess1280JPG.txt
@celsobessa
celsobessa / String.replaceAll.js
Created September 7, 2020 19:55
A more elegant implementation of a polyfill for replaceAll method on strings by Chris Ferdinandi (source https://vanillajstoolkit.com/polyfills/stringreplaceall/ )
/**
* String.prototype.replaceAll() polyfill
* https://vanillajstoolkit.com/polyfills/stringreplaceall/
* @author Chris Ferdinandi
* @license MIT
*/
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
@celsobessa
celsobessa / git-track-fetch-pull-all-remote-branches.sh
Created July 27, 2020 18:56
Track, fetch and pull all remote branches on a remote GIT repository
# Track, fetch and pull all remote branches on a remote GIT repository
# Source: https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches/10312587#10312587
git branch -r | grep -v '\->' | while read remote;
do git branch --track "${remote#origin/}" "$remote";
done
git fetch --all
git pull --all
@celsobessa
celsobessa / 1. Refactoring - Original.js
Last active February 23, 2020 17:34 — forked from Integralist/1. Refactoring - Original.js
The importance of refactoring... (by Mark McDonnell ( https://gist.github.com/Integralist )
var isProxyBased = (/S40[\w]{3,5}Browser|Opera\sMini\//i).test(navigator.userAgent);
if (('querySelector' in document && 'localStorage' in window && 'addEventListener' in window && !isProxyBased) || (isIE > 6 && document.getElementById('js-holepunched'))) {
// do stuff
}