Skip to content

Instantly share code, notes, and snippets.


Jeremy Keith adactio

View GitHub Profile
adactio / previewImageUploads.js
Created May 12, 2022
Show inline previews of images that are going to be uploaded.
View previewImageUploads.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
(function (win,doc) {
'use strict';
if (!win.FileReader || !win.addEventListener || !doc.querySelectorAll) {
// doesn't cut the mustard.
doc.querySelectorAll('input[type="file"][accept="image/*"]').forEach( function (fileInput) {
adactio / getBandcampArtist.php
Created Oct 21, 2021
Scraping Bandcamp for artist info.
View getBandcampArtist.php
function curlURL($url) {
$return = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
adactio / _polyfills.js
Created Nov 9, 2020
JavaScript polyfills for matches,closest, and forEach.
View _polyfills.js
var ElementPrototype = window.Element.prototype;
if (typeof ElementPrototype.matches !== 'function') {
ElementPrototype.matches = ElementPrototype.msMatchesSelector || ElementPrototype.mozMatchesSelector || ElementPrototype.webkitMatchesSelector || function matches(selector) {
var element = this;
var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
var index = 0;
while (elements[index] && elements[index] !== element) {
adactio / saveTextarea.js
Last active Jul 24, 2021
Put the contents of a textarea into localStorage if the user leaves the page before submitting the form.
View saveTextarea.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
(function (win, doc) {
// Cut the mustard.
if (!win.localStorage) return;
// You should probably use a more specific selector than this.
var textarea = doc.querySelector('textarea');
// The key for the key/value pair in localStorage is the current URL.
var key = win.location.href;
adactio / giveFeedback.js
Last active Dec 8, 2020
An unobtrusive animation effect for providing feedback to the user.
View giveFeedback.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
This function takes two arguments:
element: a reference to a DOM node in the document e.g. a button.
feedbackContent: a string of text or HTML.
An example of usage would be:
document.querySelector('button').addEventListener('click', function() {
adactio / sharebutton.js
Last active Mar 26, 2021
A polyfill for `button type="share"`
View sharebutton.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
/* Use <button type="share"> in your HTML.
Include this JavaScript in a <script> element on the same page or in an external script.
The script checks for three ways of sharing:
1. Native support for <button type="share">.
2. Support for the JavaScript Web Share API.
3. A mailto: link.
This will share the current URL and page title.
adactio / submitFormFromDatalist.js
Last active May 28, 2022
HTML's datalist element autocompletes. This script is my attempt to get it to autosubmit as well.
View submitFormFromDatalist.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
(function (win, doc) {
'use strict';
// Cut the mustard
if (!doc.querySelectorAll || !doc.querySelectorAll('input[list]') || !win.HTMLDataListElement || !win.addEventListener){
// Loop through each input element with a list attribute
adactio / getEmbedCode.php
Created Mar 24, 2020
A PHP function to return an oEmbed result from a URL.
View getEmbedCode.php
function getEmbedCode($url="") {
$return = '';
$providers = array(
'' => '',
'' => '',
'' => '',
adactio / indyMap.js
Created Oct 21, 2019
Scrape the current page for h-geo data and plot the result as a polyline on a Stamen watercolour map.
View indyMap.js
(function (win, doc) {
win.addEventListener('load', function() {
var latlons = [];
doc.querySelectorAll('.h-geo').forEach( function(geo) {
var lat = geo.querySelector('data.p-latitude').getAttribute('value');
var lon = geo.querySelector('data.p-longitude').getAttribute('value');
if (lat && lon) {
latlons.push([lat, lon]);
adactio / timestampComparison.js
Last active Jul 30, 2019
Compare server and client timestamps
View timestampComparison.js
// Generate a timestamp (in seconds) on the server. This won't change if the page is served from a cache.
var serverTimestamp = <?php echo time(); ?>;
// Create a new Date object from the local date and time on the client.
var localDate = new Date();
// Convert the local date and time to Universal Time (same as the server).
var localUTCString = localDate.toUTCString();
// Create a new Date object from the UTC date and time on the client.
var UTCDate = new Date(localUTCString);
// Generate a timestamp (in seconds) from the UTC date and time on the client.
var clientTimestamp = UTCDate.getTime() / 1000;