Skip to content

Instantly share code, notes, and snippets.

@leereamsnyder
leereamsnyder / og-image-generator.js
Last active December 13, 2021 14:09
Netlify on-demand builder for taking a screen capture with puppeteer v10
/*
this is the Netlify on-demand builder described here:
https://www.leereamsnyder.com/blog/dynamic-social-media-images-with-puppeteer-and-netlify
but updated to work with puppeteer 10
See that article for setup on netlify, including redirects and meta tags
The builder code in linked article was written for puppeteer 8
and things didn't work after I updated to version 10
@leereamsnyder
leereamsnyder / wantsJSON.js
Last active October 20, 2017 17:39
Detect if a request in Express is for HTML or JSON
/*
Usage:
var jsoncheck = require('./wantsJSON') // path to file with this middleware function
app.use(jsoncheck)
app.get('/', function(req,res,next){
if (req.wantsJSON) {
// serve JSON
}
if (req.wantsHTML) {
@leereamsnyder
leereamsnyder / jquery.restoreIOSscrollTo.js
Last active May 27, 2016 19:38
Restore window.scrollTo in iOS Safari after tapping the status bar
/**
**UPDATE**: This was fixed in iOS 8. But this could still be valid for iOS 7 and below.
I believe this was first noticed here: http://blog.b123400.net/window-scrollto-and-ios-status-bar/
> When the status bar is tapped, the page scrolls to the top and url bar is shown,
> then window.scrollTo doesn't work anymore.
Yep!
@leereamsnyder
leereamsnyder / wordpress_listing_excerpt_or_more.php
Last active August 29, 2015 14:05
In WordPress loops, show the excerpt, the content before <!--MORE-->, or something else, but avoid showing the full post content.
<?php
/**
* Let's say you want to support BOTH post excerpts and use of the <!--more--> tag
* to truncate post content in The Loop.
*
* And let's also say that you definitely do not want to show the full content of the post
* if either of those are absent. Maybe you're displaying posts in little Pinterest-y
* cards so the text/excerpt can't be too long.
*
* Let's say this is your priority:
@leereamsnyder
leereamsnyder / wp_get_current_page_url.php
Last active March 26, 2024 15:59
In WordPress, get current URL including query string
<?php
/**
Re-use some existing WordPress functions so you don't have to write a bunch of raw PHP to check for SSL, port numbers, etc
Place in your functions.php (or re-use in a plugin)
If you absolutely don't need or want any query string, use home_url(add_query_arg(array(),$wp->request));
Hat tip to:
+ http://kovshenin.com/2012/current-url-in-wordpress/
@leereamsnyder
leereamsnyder / jquery.attemptfocus.js
Last active September 10, 2015 20:52
A little jQuery plugin that I've found helps a bunch when managing focus. Adds ':focusable' and ':tabbable' selectors (from jQuery UI) and adds chainable methods to set and manage focus amongst sets of elements
;(function($, window, document, undefined){
/* :focusable and :tabbable selectors from
https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.core.js */
function visible(element) {
return $.expr.filters.visible(element) && !$(element).parents().addBack().filter(function () {
return $.css(this, "visibility") === "hidden";
}).length;
}
@leereamsnyder
leereamsnyder / jquery.ontransitionend.js
Last active March 22, 2017 20:33
Quick jQuery plugin to fire the correct browser-prefixed 'transitionend' event with a callback
/***********************************
Little utility function for handling single transitionEnd events.
Particularly handy for when you need to remove something from
the DOM after transforming it with an animation
WHY NOT $el.one('transitionend', callback) ????
=============================================