Skip to content

Instantly share code, notes, and snippets.

View DarrenSem's full-sized avatar

Darren Semotiuk DarrenSem

View GitHub Profile
@DarrenSem
DarrenSem / replaced.js
Last active February 10, 2024 18:01
replaced.js ( v, ...rules [ ArrayOf[ pattern, replaceWith, optionalRegExpFlags] ] ) -- flexible multiple string replacements (Flags default = "g")
View replaced.js
// replaced.js ( v: any, ...rules: Array of [pattern: any, replaceWith: string | function, optionalRegExpFlags: string | null | undefined] )
const replaced = (v, ...rules) => {
return rules.reduce( (acc, rule) => (
acc.replace(
RegExp( rule[0], rule[2] == null ? "g" : rule[2] ),
rule[1]
)
), String( v ?? "" ) );
};
@DarrenSem
DarrenSem / MHT Saver - minimal code for Chrome extension.md
Created February 2, 2024 15:22
MHT Saver - minimal code for Chrome extension to Save As MHT
View MHT Saver - minimal code for Chrome extension.md

I want to write a Chrome extension. I need the most minimal code for the V3 manifest and worker.js to trigger a download prompt of a data url generated by btoa() of the capture as mhtml function. Use only async await and arrow functions instead of promises and callbacks wherever possible. Use blob.text() instead of FileReader. Image128 .png and no popups or content scripts.

(02Feb2024 806am)

BOT:

To create a minimal Chrome extension that triggers a download prompt of a captured page in MHTML format using Manifest V3, you need to include the following files in your extension:

  1. manifest.json: The metadata file, which includes information about the extension like its name, version, permissions, and which script to run as the service worker.
@DarrenSem
DarrenSem / range(size, optionalEachFunction, optionalMapFunction).js
Last active February 7, 2024 16:13
range.js (arraySize, optionalMapFunction, optionalEachFunction)
View range(size, optionalEachFunction, optionalMapFunction).js
// range(arraySize, optionalMapFunction, optionalEachFunction).js
// https://gist.github.com/DarrenSem/bc0403bcdad09912eb298e6d3b4824fe
const range = ( arraySize, fnMapIndex, fnEachIndexFirst, _indexes ) => (
_indexes = Array.from( Array(arraySize | 0).keys() ), // because [...Array(arraySize | 0).keys()] triggers TypeScript/VSC error: Type 'IterableIterator' is not an array type or a string type.
fnEachIndexFirst && _indexes.forEach( index => fnEachIndexFirst(index) ),
fnMapIndex && ( _indexes = _indexes.map( index => fnMapIndex(index) ) ),
_indexes
);
@DarrenSem
DarrenSem / fizzbuzz expanded - wordmod.js
Last active November 9, 2023 20:38
fizzbuzz expanded - wordmod( limit = 100, words = { 3: "Fizz", 5: "Buzz" }, sep = "\n" )
View fizzbuzz expanded - wordmod.js
// FizzBuzz expanded - wordmod.js - because why not
// https://gist.github.com/DarrenSem/3a63b31542487bb1fd38e08976d8d215
let wordmod = ( limit = 100, words = { 3: "Fizz", 5: "Buzz" }, sep = "\n" ) => {
let results = [];
for (let i = 1; i <= limit; i ++) {
let result = "";
for ( let [divisor, word] of Object.entries(words) ) {
@DarrenSem
DarrenSem / mongodb-getDocumentArray.js
Last active November 2, 2023 22:12
MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
View mongodb-getDocumentArray.js
// MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
// Purpose: returns cloned Array of passed documents (after running function on each)
// Usage: const docArray = getDocumentArray( fnEachDocument, ...documents ); await coll.insertMany(docArray);
// see the docs for https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany
// esp. "_id Field" https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#_id-field
// ^ because DURING THE .insert[Many|One]() call, "_id" field is ADDED to the original document! (if missing)
const getDocumentArray = ( fnEachDocument, ...documents ) => (
fnEachDocument ||= ( document => document ),
@DarrenSem
DarrenSem / ytSubs.js
Last active November 27, 2023 18:17
ytSubs.js - YouTube subtitles - English (auto-generated) CC (closed captions) - Usage: node ytSubs.js videoIdOrUrl, or Web browser BOOKMARKLET into a new window
View ytSubs.js
// ytSubs.js (SEE DISCLAIMER) - https://gist.github.com/DarrenSem (27Nov2023)
// YouTube subtitles - English (auto-generated) CC (closed captions)
// Usage: node ytSubs.js videoIdOrUrl
// or Web browser BOOKMARKLET (contents open in a new window)
// ES6 bookmarklet = 4037 chars
javascript:void function(){"use strict";var a=String.fromCharCode;const b=async a=>{try{if(globalThis.fetch){const b=await fetch(a),c=await b.text();return c}return new Promise((b,c)=>{const d=require(/^https/.test(a)?"https":"http").get(a,d=>{if(200>d.statusCode||299<d.statusCode)return c(Error(`${d.statusCode} ${d.statusMessage} ${a}`));const e=[];d.on("data",a=>e.push(a)),d.on("end",()=>b(e.join("")))});d.on("error",a=>c(a))})}catch(a){throw a}},c=a=>{try{a=(a||"")+"";const b=a.match(/"captionTracks":.*"isTranslatable"\:.*?}]/),c=JSON.parse(`{${(b||[""])[0]}}`).captionTracks||[],d=c.map(a=>{const b=a.name;return[f(b.simpleText||b.runs&&b.runs[0].text),a.baseUrl+"&fmt=json3"]});return d}catch(a){}},d=(a,b,c,d)=>{a=new Date(a),[,b,
@DarrenSem
DarrenSem / hostnameRoot.js
Created September 25, 2023 11:22
hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
View hostnameRoot.js
// hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
const hostnameRoot = hostname => String(hostname ?? "")
.toLowerCase()
.replace(
/^(m(ob(i(le)?)?)?|ww(w?\d|w))\.(.+?\..+)/,
"$6"
);
console.log([
@DarrenSem
DarrenSem / process.js-for-HackerRank.js.md
Last active June 15, 2023 19:49
process.js-for-HackerRank
View process.js-for-HackerRank.js.md

process.js-for-HackerRank

#nodejs #polyfill #browser #hackerrank #node-js #client-side #hackerrank-solutions #hackerrank-javascript #hackerrank-30dayschallange #hackerrank-challenges

https://github.com/DarrenSem/process.js-for-HackerRank (might have more recent updates/improvements)

Minimal polyfill (324 characters minified) for client-side JS to add missing NodeJS functions for completing HackerRank.com challenges -- process.stdin/stdout/env.OUTPUT_PATH and require("fs").createWriteStream

(So I can complete HackerRank.com challenges on my cell using my custom REPL bookmarklet.)

@DarrenSem
DarrenSem / UMD-javascript-module-template.js
Last active September 3, 2023 16:32
Universal Module Definition (UMD) template for JavaScript modules
View UMD-javascript-module-template.js
// UMD-javascript-module-template.js
// Universal Module Definition (UMD) template for JavaScript modules
(function(root, factory) {
// var req1 = foo, req2 = bar; // or even exports
if(typeof exports === "object" && typeof module !== "undefined") {
module.exports = factory(req1, req2);
} else if(typeof define === "function" && define.amd) {
define([req1, req2], factory)
} else {
root["MOD_NAME"] = factory(req1, req2);
@DarrenSem
DarrenSem / Array.build.js
Last active December 1, 2023 20:53
Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) ) - REPLACED BY Array.range.js
View Array.build.js
// REPLACED BY: Array.range.js => new Array( length, optionalEachFunction, optionalMapFunction )
// https://gist.github.com/DarrenSem/bc0403bcdad09912eb298e6d3b4824fe
// Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) )
Array.build ||= (length, callbackFn) => (
Array(length|0)
.fill(0)