Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Accudio's full-sized avatar

Alistair Shepherd Accudio

View GitHub Profile
@Accudio
Accudio / api-auth.js
Created December 29, 2023 12:18
Basic authentication using Vercel edge functions
import bcrypt from 'bcrypt';
import { SignJWT } from 'jose';
import { serialize } from 'cookie';
export default async function (request, response) {
if (request.method !== "POST") {
return response.status(405).send('does not respond to GET requests');
}
// get all configured passwords
@Accudio
Accudio / middleware.js
Created April 10, 2023 09:07
Multiple currencies within cookies using Vercel Middleware
import { rewrite } from '@vercel/edge';
import { RequestCookies } from '@edge-runtime/cookies'
// only run middleware on home page
export const config = {
matcher: '/',
}
export default function middleware(req) {
const cookies = new RequestCookies(req.headers)
@Accudio
Accudio / add-to-diary.php
Created November 25, 2022 14:47
Class to generate Calendar URLs and files
<?php
/**
* Utility for adding an event to various calendars
*
* @package SMRC
*/
if (isset($_POST['smrc_add_to_diary'])) {
$event = new SMRC_Diary_Event($_POST);
@Accudio
Accudio / alpine-window-sync.html
Created June 10, 2022 11:55
Synchronising a property on `window` and an Alpine.js property
<div x-data="{
count: 4,
init() {
const self = this
Object.defineProperty(window, 'count', {
enumerable: true,
configurable: true,
get: function() {
return self.count
},
@Accudio
Accudio / colour-contrast.js
Created June 9, 2022 08:54
Basic JavaScript implementation of a WCAG 2.1 colour contrast algorithm that chooses between black or white depending on the colour provided
console.log(contrastingColour('#eee'))
function contrastingColour(colour) {
const rgb = hexToRGB(colour)
// luminance of inputted colour
const L = 0.2126 * colourMod(rgb.r) + 0.7152 * colourMod(rgb.g) + 0.0722 * colourMod(rgb.b)
// white has a luminance of 1
const whiteL = 1
@Accudio
Accudio / 1-usage.php
Last active January 26, 2022 15:25
Example PHP abstraction of an Image CDN. Built with CloudImage and an alias in mind, customise as your setup and provider.
<?php
echo image([
'image' => 'otter.jpg',
'alt' => 'an otter standing on a log looking majestic',
'srcset' => [300, 450, 600, 800, 1000, 1200],
'sizes' => '100vw',
'width' => 1200,
'height' => 800,
'loading' => 'lazy',
@Accudio
Accudio / 1-usage.php
Last active January 26, 2022 15:27
Example Advanced Custom Fields abstraction of an Image CDN. Built with CloudImage and an alias in mind, customise as your setup and provider.
<?php
echo image( [
'image' => get_field('image'),
'srcset' => [300, 450, 600, 800, 1000, 1200],
'sizes' => '100vw',
'loading' => 'lazy'
'class' => 'image-class'
] );
@Accudio
Accudio / wordle.js
Created January 14, 2022 09:46
Modify Wordle statistics. Modify object in `JSON.stringify` call and save as a bookmark, run on Wordle page.
javascript:(function(){ localStorage.setItem('statistics', JSON.stringify({ "currentStreak": 100, "maxStreak": 100, "guesses":{ "1": 99, "2": 0, "3": 1, "4": 0, "5": 0, "6": 0, "fail" :0 }, "winPercentage": 100, "gamesPlayed": 100, "gamesWon": 100, "averageGuesses": 1 }))})()
@Accudio
Accudio / 1-plugin.js
Created November 18, 2021 18:11
Custom Property colour plugin for Tailwind. Used in this case where `.focus-pink` would set `--focus-colour` but could be anything
const plugin = require('tailwindcss/plugin')
const baseClass = '.focus'
const property = '--focus-colour'
const getColours = (colours, prefix, e) => {
let utils = {}
for (const key in colours) {
if (typeof colours[key] === 'object') {
@Accudio
Accudio / bookmarklet
Last active May 31, 2022 16:21
"Check for WCAG 2.0 parsing compliance" by [Steve Faulkner](https://twitter.com/SteveFaulkner) with added support for Alpine and Vue attributes
javascript:(function(){var a,b,c,d,e,f,g=0;if(a=["tag seen","Stray end tag","Bad start tag","violates nesting rules","Duplicate ID","first occurrence of ID","Unclosed element","not allowed as child of element","unclosed elements","not allowed on element","unquoted attribute value","Duplicate attribute"].join("|"),b=document.getElementById("results"),!b)return void alert("No results container found.");for(c=b.getElementsByTagName("li"),f=0;f<c.length;f++)d=c[f],""!==d.className%26%26(e=(void 0===d.innerText%3Fd.textContent:d.innerText)+"",resultHtml=d.innerHTML,null===e.match(a)%3F(d.style.display="none",d.className+=" steveNoLike",g++):-1!==e.indexOf("not allowed on element")%26%26(-1!==resultHtml.indexOf("<code>ng-")||-1!==resultHtml.indexOf("<code>x-")||-1!==resultHtml.indexOf("<code>ax-")||-1!==resultHtml.indexOf("<code>v-")||-1!==resultHtml.indexOf("<code>:")||-1!==resultHtml.indexOf("<code>%40"))%26%26(d.style.display="none",d.className+=" steveNoLike",g++));alert("Complete. "+g+" items removed.")})();