Skip to content

Instantly share code, notes, and snippets.

@herdianf
herdianf / domready.js
Created February 4, 2024 12:36
dom ready
var domReady = (function() {
var readyCallbacks = [];
function whenReady(callback) {
setTimeout(function() {
callback.call(document)
});
}
function completed() {
@herdianf
herdianf / xmldom.js
Created December 7, 2023 10:11
DOM XML Implementation
// Directly pass this as string
let xmlString = `<ern:NewReleaseMessage xmlns:ern="http://ddex.net/xml/ern/382"
MessageSchemaVersionId="ern/382"
LanguageAndScriptCode="en"
xs:schemaLocation="http://ddex.net/xml/ern/382 http://ddex.net/xml/ern/382/release-notification.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"></ern:NewReleaseMessage>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
@herdianf
herdianf / relative.sh
Created December 4, 2023 15:34
Bash Relative to Source
#!/bin/bash
BASE=$(dirname $(realpath $0))/..
echo $BASE;
@herdianf
herdianf / route_match.js
Created December 3, 2023 03:22
Simple express router pattern matching
/**
* Match request path with route style path.
*
* @param {string} reqpath The request path
* @param {string} routepath The route pattern path
* @return {Object|boolean} matches object if match or false if not match
*
*/
function match(reqpath, routepath) {
if (reqpath === routepath) return {}
@herdianf
herdianf / base64url.php
Last active October 29, 2023 07:36
BASE64 URL RFC4648
<?php
function base64url_encode($data) {
return $data ? rtrim(strtr(base64_encode($data), '+/', '-_'), '=') : '';
}
function base64url_decode($data) {
return $data ? base64_decode(strtr((string)$data, '-_', '+/'), TRUE) : '';
}
@herdianf
herdianf / curl.php
Created October 10, 2023 21:19
PHP curl functions
<?php
/**
* Run a single curl request
*
* @param string $url
* The url for request.
* @param array $options
* Map of options, expected key:
* - method string GET|POST|HEAD
@herdianf
herdianf / innerhtml.js
Created July 11, 2023 03:23
innerHTML and execute script
function setInnerHTML(elm, html) {
elm.innerHTML = html;
elm.querySelectorAll("script").forEach(function(oldScriptEl) {
let newScriptEl = document.createElement("script");
[].slice.call(oldScriptEl.attributes).forEach(function(attr) {
newScriptEl.setAttribute(attr.name, attr.value);
});
newScriptEl.appendChild(document.createTextNode(oldScriptEl.innerHTML));
oldScriptEl.parentNode.replaceChild(newScriptEl, oldScriptEl);
});
@herdianf
herdianf / ffmpeg.txt
Created June 6, 2023 06:23
ffmpeg scripts
# cut video in middle
# In order to keep <start-15s> and <45s-end>, you need to
# keep all the frames which are "not between 15s and 45s":
ffmpeg -i input.mp4 \
-vf "select='not(between(t,15,45))', setpts=N/FRAME_RATE/TB" \
-af "aselect='not(between(t,15,45))', asetpts=N/SR/TB" \
output.mp4
#source https://stackoverflow.com/questions/64866231/remove-a-section-from-the-middle-of-a-video-without-concat
@herdianf
herdianf / preact.html
Created October 7, 2022 04:09
Preact No Build UMD
<!DOCTYPE html>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1, minimal-ui">
<title>Preact</title>
<div id=app>
<h1>Hello World!</h1>
<!-- comments -->
<p style="background-color: lime;">Preloading text...</p>
<p>Count = 0</p>
@herdianf
herdianf / printrr.php
Last active July 17, 2022 10:19
PHP print_r without circular issues and limited by depth
<?php
/**
* Function to dump variables limited by depth and without circular issues.
*
* @param $thing The object.
* @param int $maxdepth Maximum depth to recurse.
* @param array $stack Stack to store the previous object. Ignore this.
* @param int $depth The current depth. Ignore this.