Skip to content

Instantly share code, notes, and snippets.

View michaelhalstead's full-sized avatar

Michael Halstead michaelhalstead

View GitHub Profile
@michaelhalstead
michaelhalstead / sanitizeHTML.js
Last active September 19, 2023 23:30
Sanitize HTML #html #javascript #typescript #security
function sanitizeHTML(html, whitelistedTagNames = []) {
/* List of event attributes to remove */
const eventAttributes = [
'onabort',
'onblur',
'onclick',
'ondblclick',
'onerror',
'onfocus',
'onkeydown',
@michaelhalstead
michaelhalstead / gmaps-autocomplete.js
Last active August 30, 2023 23:28
Fetch for Google Maps Autocomplete
async function getLocationSuggestions({ query, apiKey, boundaryCountry = 'US', layers = 'locality' }) {
const autocompleteData = await fetch(`https://api.geocode.earth/v1/autocomplete?api_key=${apiKey}&text=${encodeURIComponent(query)}&boundary.country=${boundaryCountry}&layers=locality`, {
"body": null,
"method": "GET",
"credentials": "omit"
})
.then(async res => res.json());
const autocompleteSuggestions = autocompleteData
.features
@michaelhalstead
michaelhalstead / js-cookie-helpers.js
Last active August 30, 2023 20:54
JS Cookie Helpers #JavaScript #cookies
/**
* Receives a form element and validates the
* fields with a required tag.
*
* @param {HTMLElement} formEl - The HTMLElement of the for to be validated
* @param {Function} callback - The form submission handler to run if validation passes.
* @param {String} invalidClass - OPTIONAL. A class appended to fields that are invalid.
* @returns {null} - Nothing is returned. The function calls the callback if validation passes.
*/
@michaelhalstead
michaelhalstead / index.js
Last active January 21, 2022 22:19
Node.JS script for parsing page in Puppeteer
const puppeteer = require("puppeteer");
let browser;
module.exports = async function (context, req) {
var startTime = Date.now();
if(!browser){
browser = await puppeteer.launch({headless: true});
}
@michaelhalstead
michaelhalstead / FormatPhoneNumber.js
Last active October 10, 2023 16:22
Format Phone Number
const formatPhoneNumber = (num, format = 'default') => {
/* Filter only numbers from the input */
const cleaned = ('' + num).replace(/\D/g, '');
/* Check if the input is of correct */
const match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/);
if (match) {
/* Remove the matched extension code. Change this to format for any country code.*/
const intlCode = (match[1] ? '' : '');
const viewportLessThan = (px = 969) => window.innerWidth < px;