Skip to content

Instantly share code, notes, and snippets.

View KevinBatdorf's full-sized avatar
⚔️
I am undefined

Kevin Batdorf KevinBatdorf

⚔️
I am undefined
View GitHub Profile
@KevinBatdorf
KevinBatdorf / make-mdn-great-again.js
Created July 16, 2022 02:46
Various tweaks to MDN to improve readability
// ==UserScript==
// @name MDN fixes
// @namespace kevinbatdorf
// @version 0.1
// @description Make MDN great again
// @author You
// @match https://developer.mozilla.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=mozilla.org
// @grant none
// ==/UserScript==
@KevinBatdorf
KevinBatdorf / BlockReplacer.js
Last active June 7, 2022 16:01
Replace your custom Gutenberg block with another block immediately
import { store as blockEditorStore } from '@wordpress/block-editor'
import { createBlock } from '@wordpress/blocks'
import { useSelect, useDispatch } from '@wordpress/data'
import { useEffect } from '@wordpress/element'
export const BlockReplacer = ({ clientId, blockType }) => {
const block = useSelect(
(select) => select(blockEditorStore).getBlock(clientId ?? ''),
[clientId],
)
@KevinBatdorf
KevinBatdorf / wpApi.js
Last active June 1, 2022 17:57
WordPress REST API install and activate by slug
// You need to set up `window.myPlugin.nonce` and `window.myPlugin.wpRoot` in PHP
/*
\wp_add_inline_script('script-name',
'window.myPlugin = ' . wp_json_encode([
'wpRoot' => \esc_url_raw(\rest_url()),
'nonce' => \wp_create_nonce('wp_rest')
]);
);
*/
@KevinBatdorf
KevinBatdorf / download-alpine-code-from-taliwind-ui.js
Last active February 27, 2023 16:33
This Gist lets you download the Alpine version of TailwindUI components
// ==UserScript==
// @name Download AlpineJs version from Tailwind UI
// @namespace kevinbatdorf
// @version 1.0
// @description When you press copy, it will download an html file containing the script code and alpine HTML
// @author https://twitter.com/kevinbatdorf
// @match https://tailwindui.com/components/*
// @grant none
// ==/UserScript==
@KevinBatdorf
KevinBatdorf / uploadImage.ts
Last active May 1, 2022 07:06
Upload an image to the WordPress media library using the ImageData JavaScript object type
import apiFetch from '@wordpress/api-fetch'
// This is also set up to clone fields from an existing image.
// Just remove imageToClone and supply the missing fields on your own.
export const uploadImage = async (image: ImageData, imageToClone: WpImage): Promise<WpImage | undefined> => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
if (!ctx) return
@KevinBatdorf
KevinBatdorf / deleteRedditComments.js
Last active March 18, 2024 18:50
While on the comments page, put this in the console. May need to refresh a few times depending on how many you have
// Go to the old.reddit.com site
for (const dltbtn of document.querySelectorAll('[data-event-action="delete"]')) {
dltbtn.click();
await new Promise(requestAnimationFrame);
dltbtn.closest('form')?.querySelector('.yes')?.click()
await new Promise(r => setTimeout(r, 300));
}
// No longer works
// for (const dotdotdot of document.querySelectorAll('[aria-label*=more ]')) {
export const isValidJson = (str) => {
try {
JSON.parse(str)
return true
} catch (e) {
return false
}
}
@KevinBatdorf
KevinBatdorf / disable-accordian-scripts.php
Created March 28, 2022 03:00
Basic headless support for Accordion-Blocks WordPress plugin
<?php
// add this to functions.php or as an mu-plugin (if needed)
add_action('wp_enqueue_scripts', function () {
wp_dequeue_script('pb-accordion-blocks-frontend-script');
wp_dequeue_style('pb-accordion-blocks-style');
});
@KevinBatdorf
KevinBatdorf / disable-welcome-guide.php
Last active March 8, 2022 03:17
Disable the Gutenberg Welcome Guide from showing
<?php
add_action('admin_footer', function () { ?>
<script>
window.addEventListener('load', () => {
window.setTimeout(() => {
if (wp.data && wp.data.select('core/edit-post').isFeatureActive('welcomeGuide')) {
wp.data.dispatch('core/edit-post').toggleFeature('welcomeGuide');
}
})
@KevinBatdorf
KevinBatdorf / keybindings.json
Created February 8, 2022 21:27
VS Code settings and bindings Feb 2022
[
/**
* Panels
**/
{
"key": "cmd+k cmd+e",
"command": "workbench.view.explorer"
},
{
"key": "cmd+k cmd+g",