Skip to content

Instantly share code, notes, and snippets.

View erikyo's full-sized avatar

Erik Golinelli erikyo

View GitHub Profile
@erikyo
erikyo / sprintf.d.ts
Last active May 8, 2024 20:00
Strongly typed sprintf (allows placeholders and named placeholders)
type Specifiers = {
's': string,
'd': number,
'b': boolean,
'D': Date
};
type S = keyof Specifiers;
type ExtractNamedPlaceholders<T extends string> =
T extends `${infer Start}%(${infer Key})${infer Spec}${infer Rest}`
@erikyo
erikyo / lazy-background.php
Created March 25, 2024 06:34
Wordpress background and video lazy load
<?php
/**
* Lazy load background images and videos in Gutenberg blocks
* @param $block - the block
* @param $parsed - the parsed block data
* @return array|mixed|string|string[]|null
*/
function vsge_lazy_cover( $block, $parsed ) {
// Check if the 'url' attribute is set
if ( isset( $parsed['attrs']['url'] ) ) {
@erikyo
erikyo / script.js
Last active March 6, 2024 20:12
Select all the emojis in the page and wrap them into a span. Fix for this wordpress dark mode block plugin
function wrapEmojisWithDiv() {
const emojiRegex = /\p{Emoji_Presentation}/gu;
// Select all elements containing text
const elementsWithText = document.querySelectorAll('*:not(script):not(style)');
elementsWithText.forEach(function(element) {
// node 3 is "text"
if (element.childNodes[0]?.nodeType === 3) {
// Wrap emojis with a div with the class "no-dark" that disables the "invert" filter
@erikyo
erikyo / wpmm-schema.json
Created December 11, 2023 09:27
The schema json for WPMM
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"wordpress": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
@erikyo
erikyo / wp-package.json
Created December 9, 2023 14:01
wpmm wp-package.json template
{
"name": "wordpress",
"wordpress": {
"version": null,
"language": "it_IT",
"config": {
"DB_NAME": "my_db_name",
"DB_USER": "my_db_username",
"DB_PASSWORD": "my_db_password",
"DB_HOST": "localhost",
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browser Security Test</title>
<!-- Google Fonts -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
@erikyo
erikyo / checkCookieConsentAndShowPopup.js
Created October 17, 2023 13:17
Show a message after the cookie Consent Popup - This gist contains JavaScript code for displaying a customizable banner on a website after the user has accepted cookie consent. The code checks for the presence of a specific cookie and shows the banner with configurable content once the user has provided consent. This code can be integrated into …
// the name of the cookie to check before showing the popup
const COOKIE_NAME = "cookieyes-consent";
const COOKIE_POPUP = "ctaBannerShown";
const COOKIE_POPUP_CLASS = "popup-cta";
const COOKIE_POPUP_LINK = "//google.com";
/**
* The content of the banner
*
* @type {{reject: string, text: string, title: string, accept: string}}
@erikyo
erikyo / simple_toast_notification.js
Created October 13, 2023 13:21
This code defines a JavaScript function named makeToast designed for displaying customizable toast notifications on a web page. Toast notifications are often used to provide brief, non-intrusive feedback or information to the user.
function makeToast( message = 'OK!', timeout = 3000 ) {
// Create the toast notification element
const toast = document.createElement('div');
toast.textContent = message;
toast.style = 'display:block;max-width:calc(100% - 20px);position:fixed;bottom:20px;left:50%;transform:translateX(-50%);background-color:#333;color:#fff;padding:10px 20px;border-radius:5px;';
// display the toast notification
document.body.appendChild(toast);
// Hide and remove the toast after a while
@erikyo
erikyo / Readme.md
Last active May 24, 2023 18:03
CSS file parser for removing duplicates among multiple css files

By using this script, you can easily remove duplicated CSS properties and prettify the resulting CSS files, making your stylesheets more organized and optimized.

Description:

This Node.js script helps parse multiple CSS files, remove duplicated CSS properties, and save the processed files with the "new-" prefix. The script utilizes the css package for parsing and manipulating CSS, as well as the prettier package for prettifying the resulting CSS output.

Key Features:

Accepts an array of CSS file names as input. Parses each CSS file using the css package, building an Abstract Syntax Tree (AST) representation. Removes duplicate CSS properties by comparing rules across all previous files.