Skip to content

Instantly share code, notes, and snippets.

View sampotts's full-sized avatar

Sam Potts sampotts

View GitHub Profile
@sampotts
sampotts / reddit-dark-mode-system.js
Last active June 1, 2023 00:27
A user script to toggle Reddit's dark mode automatically based on your system preferences
// Get the initial state
const query = window.matchMedia('(prefers-color-scheme: dark)');
function toggleDarkMode(toggle = query.matches) {
// Open the menu
document.querySelector('#USER_DROPDOWN_ID[aria-expanded="false"]')?.click();
// Get the wrapper item
const wrapper = Array.from(document.querySelectorAll('button')).find(({ innerText }) => innerText.toLowerCase() === 'dark mode');
const checkbox = wrapper?.querySelector(`[aria-checked="${!toggle}"]`);
@sampotts
sampotts / markdown.css
Created June 2, 2021 05:07
Custom Markdown Theme
@import "https://cdn.potts.es/fonts/fakt-pro.css";
@import "https://cdn.potts.es/fonts/basier-circle-mono.css";
html {
font-size: 100%;
}
/* Basic reset */
h1,
h2,
<div data-embed="cart">
<script type="text/props">
{
"storeId": 13,
"colors": {
"buttons": {
"background": "#f9b642",
"text": "#ffffff"
},
"checkout": {
@sampotts
sampotts / array-closest.js
Last active July 3, 2018 03:20
Find the closest number in an array of numbers, with ceil and floor rounding options
// Rounding methods for .closest()
const closestRounding = {
none: 0,
ceil: 1,
floor: 2,
};
export { closestRounding };
/**
* Get the closest value in an array
// ==========================================================================
// Simple color manipulation library
// ==========================================================================
const clamp = (input = 0, min = 0, max = 255) => {
return Math.min(Math.max(input, min), max);
};
const regex = new RegExp(/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/, 'i');
@sampotts
sampotts / previous_shutdown_cause
Created April 7, 2018 08:00 — forked from smashism/previous_shutdown_cause
searching for shutdown causes
log show --predicate 'eventMessage contains "Previous shutdown cause"' --last 24h
<html lang="fr">
<head>
<title>Isto é um teste</title>
</head>
<body>
<p>Aqui está um texto em francês</p>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
<path d="M12,0C5.383,0,0,5.383,0,12s5.383,12,12,12s12-5.383,12-12S18.617,0,12,0z M12,2c5.176,0,9.446,3.954,9.949,9h-4.567
L15,15.764l-6-12L5.382,11H2.051C2.554,5.954,6.824,2,12,2z M12,22c-5.176,0-9.446-3.954-9.949-9h4.567L9,8.236l6,12L18.618,13
// ==========================================================================
// tab-focus.js
// Detect keyboard tabbing
// ==========================================================================
(function() {
var className = "tab-focus";
// Remove class on blur
document.addEventListener("focusout", function(event) {
// Async load a script with a callback
(function () {
function async(u, c, a) {
var d = document;
var t = 'script';
var o = d.createElement(t);
var s = d.getElementsByTagName(t)[0];
o.src = u;
if (typeof a === 'object') {
@sampotts
sampotts / sprite.js
Last active May 14, 2017 05:47
This will fetch a remote SVG sprite and then store in local storage meaning next time it is pulled from local storage while the XHR occurs, preventing the flash of no icons. It could obviously be extended to pull from local storage all the time based on a version of the SVG sprite.
// ==========================================================================
// SVG sprite loading and caching
// This file should be at the top of the body to avoid a flash
// Usage: loadSprite('https://cdn.com/path/to/sprite.svg', 'sprite-id');
// The second argument is optional but prevents loading twice
// ==========================================================================
(function() {
window.loadSprite = function(url, id) {
if (typeof url !== "string") {