Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / index.html
Last active June 19, 2019 00:01
Facebook Login for Web with Using Go Server. Token returned from FB Login button on client. Client makes POST request. FB API called with GET request. Token Validated. Reponsd to client with error / JSON.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Facebook Login Example</title>
<meta name="description" content="Facebook Login Example">
<meta name="author" content="Matt Lockyer">
</head>
@mattlockyer
mattlockyer / index.html
Last active June 18, 2019 23:47
Facebook Login for Web with Using Go Server.Validate token from web client POST request. FB API called with GET request. Reponse to client in JSON
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Facebook Login Example</title>
<meta name="description" content="Facebook Login Example">
<meta name="author" content="Matt Lockyer">
</head>
@mattlockyer
mattlockyer / email-or-phone-validation.js
Created June 11, 2019 15:23
Validate input as email or phone, with errors
let contact = 'test', data, error;
const first = contact.substring(0, 1)
if (first === '+' || first === '(' || !isNaN(first)) {
//phone
console.log('testing phone')
const phone = contact.replace(/[+|-|(|)|.| ]/g, "").toLowerCase()
if (phone === "" || isNaN(phone)) {
error = "Please enter a proper phone number.";
} else if (phone.length < 10) {
error = "Your phone number is not long enough. Please include the Area Code.";
//helper function
const assignFuncs = (state, funcs) => funcs
.map((func) => ({[func.name]: (...args) => func(state, ...args)}))
.reduce((acc, func) => ({...acc, ...func}))
//example
const bark = (state, howManyTimes) => {
state.barks += howManyTimes
const canBark = (state) => ({
bark:(howManyTimes) => {
state.barks += howManyTimes
}
})
const canEat = (state, howMuch) => {
state.food -= howMuch
}
const canBark = (state, howManyTimes) => {
state.barks += howManyTimes
}
const canEat = (state, howMuch) => {
state.food -= howMuch
}
const dog = (name) => {
let state = {
@mattlockyer
mattlockyer / composition-example-1.js
Created June 11, 2019 13:38
Object Composition Example
const canBark = (state) => ({
bark:(howManyTimes) => {
state.barks += howManyTimes
}
})
const canEat = (state) => ({
eat:(howMuch) => {
state.food -= howMuch
}
@mattlockyer
mattlockyer / unfollow.js
Last active June 1, 2019 16:45
Unfollow everyone on Twitter
/*
Go to mobile.twitter.com
Click following and you should see the followers / following tabs at the top, make sure you're on "following".
Open the developer console and paste this bad boy in.
You can watch it working away... And stop it anytime by closing the tab.
It will start with your most recently followed first and work backwards chronological as it scrolls the accounts you follow.
*/
@mattlockyer
mattlockyer / post.js
Last active April 24, 2019 17:55
JSON POST from console
fetch('http://localhost:8080/test', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({name:'matt', number:34}),
// optional
// mode: "cors", // no-cors, cors, *same-origin
// cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
@mattlockyer
mattlockyer / ecdh-example.js
Created April 20, 2019 14:47
WebCrypto ECDH Encryption / Decryption Basic Example (without validation, authentication)
//functions
const qs = (sel) => document.querySelector(sel)
const dhParams = (public) => ({ name: "ECDH", namedCurve: "P-256", public })
const aesParams = (length, counter) => ({ name: "AES-CTR", length, counter })
const keyInt = async(key) => new Uint8Array(await crypto.subtle.exportKey('raw', key));
const derive = async() => await window.crypto.subtle.generateKey(dhParams(), true, ["deriveKey", "deriveBits"])
const deriveAES = async(publicKey, privateKey) => await window.crypto.subtle.deriveKey(
dhParams(publicKey), privateKey, aesParams(256), true, ["encrypt", "decrypt"]
)
const encrypt = async (key, iv, data) => await window.crypto.subtle.encrypt(aesParams(128, iv), key, data)