Skip to content

Instantly share code, notes, and snippets.

View javanigus's full-sized avatar

Abdullah Yahya javanigus

View GitHub Profile
@javanigus
javanigus / lambda-cloudfront-s3-redirect.js
Created October 30, 2023 05:45
AWS Lambda Function to Redirect HTTP Requests by Reading JSON Data in S3
const REDIRECTS_DATA_REGION = 'us-east-1';
const REDIRECTS_DATA_BUCKET = 'pdfs';
const REDIRECTS_DATA_OBJECT = 'docs/pdf-redirects.json';
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/GetObjectCommand/
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
const client = new S3Client({
region: REDIRECTS_DATA_REGION
});
@javanigus
javanigus / aws-lambda-function-redirects.js
Created October 7, 2023 16:33
AWS Lambda Function to Redirect HTTP Requests
export const handler = async (event, context) => {
const request = event.request;
var newLocation = "";
var statusCode = 301;
var statusDescription = "Moved Permanently";
switch(request.uri) {
case "/test-pdf-1.pdf":
newLocation = "/test-pdf-2.pdf";
@javanigus
javanigus / aws-cloudfront-function-redirects.js
Created September 29, 2023 01:05
AWS CloudFront Function to Redirect URLs
function handler(event) {
// NOTE: This example function is for a viewer request event trigger.
// Choose viewer request for event trigger when you associate this function with a distribution.
var newLocation = "";
var statusCode = 301;
var statusDescription = "Moved Permanently";
switch(event.request.uri) {
case "/test-pdf-1.pdf":
@javanigus
javanigus / pass-UTM-parameters.js
Created September 27, 2023 17:00
Save UTM params in URL as session cookies; append UTM params to links with "appendUTM" class
/*
* save UTM parameters in URL as session cookies
* if a link contains the class "appendUTM",
* then append the UTM params to the link
*/
(function () {
"use strict";
// get url query string parameters
@javanigus
javanigus / convert-image-paths.js
Created September 24, 2023 17:18
Convert document-relative image paths to root-relative paths
/*
This NodeJS script will recursively read files in a folder
and execute a series of search and replace commands.
It will convert document-relative image paths to root-relative paths, e.g.
../assets/images/home_page_banner.png
to
/public/assets/images/home_page_banner.png
Set the name of the folder as the value of the "dir" variable.
Run the script as follows: node recursive-replace.js
*/
@javanigus
javanigus / recursive-regex-replace.js
Last active August 1, 2023 01:36
Convert handlebars to nunjucks
const fs = require('fs');
const path = require('path');
const walk = dir => {
try {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
file = path.join(dir, file);
const stat = fs.statSync(file);
@javanigus
javanigus / random-bg.js
Created August 19, 2021 15:01
Random background image with a fixed background image on a specific page
$=jQuery.noConflict();
(function () {
var backgrounds = [
"http://d3zntdne6n1qh.cloudfront.net/microsoft-rolling-hills.jpg",
"http://d3zntdne6n1qh.cloudfront.net/san-francisco-night.jpg",
"http://d3zntdne6n1qh.cloudfront.net/snow-mountain.jpg",
"http://d3zntdne6n1qh.cloudfront.net/turqoise-lake.jpg",
"http://d3zntdne6n1qh.cloudfront.net/desert.jpg",
"http://d3zntdne6n1qh.cloudfront.net/beach.jpg"
@javanigus
javanigus / functions.php
Created August 19, 2021 14:58
Wordpress minimum functions.php for child theme
<?php
function mychildtheme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'mychildtheme_enqueue_styles' );
@javanigus
javanigus / style.css
Created August 19, 2021 14:56
Wordpress minimum style.css for child theme
/*
Theme Name: [Your Theme Name]
Description: The custom theme [Your Theme Name] using the parent theme Twenty Fifteen.
Author: [You]
Author URI: [Your URL]
Template: twentyfifteen
Version: 1
*/
@javanigus
javanigus / promises9.js
Created May 17, 2019 18:15
Promise.all example
// first we need an asynchronous function that returns a promise
function getBurger () {
return new Promise(function (resolve, reject) {
// making burger ...
if (error) {
reject(error);
} else {
resolve(burger);
}
});