Skip to content

Instantly share code, notes, and snippets.

View DarrenSem's full-sized avatar

Darren Semotiuk DarrenSem

View GitHub Profile
@DarrenSem
DarrenSem / VueJS-ChatGPT-Playground.html
Last active June 21, 2024 21:31
VueJS-ChatGPT-Playground (minimal, proof of concept direct API fetch) - Pinia for state management, CTRL+ENTER = send, CTRL + UP/DOWN = prompt history
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatGPT Playground (starting up)</title>
<style>
.byline {
font-family: Verdana;
font-size: 80%;
@DarrenSem
DarrenSem / chatgpt-lite.js.cs.md
Last active April 25, 2024 19:59
chatgpt-lite.js equivalent in Csharp is... not nearly as minimal (ChatGPT results, MarkDown) (1st comment = final C# version, 2nd comment = Java version)

SYSTEM:

You are an expert at JavaScript and C#

. You must format indents using 2 spaces, and you must wrap strings using doublequotes " instead of '.

Let's think step by step.

@DarrenSem
DarrenSem / ytSubs.js
Last active March 26, 2024 20:28
ytSubs.js - YouTube subtitles - English (auto-generated) CC (closed captions) - Usage: node ytSubs.js videoIdOrUrl, or Web browser BOOKMARKLET into a new window
// ytSubs.js (SEE DISCLAIMER) - via @DarrenSem https://gist.github.com/DarrenSem (22Mar2024)
// 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 = 4045 chars
javascript:void function(){"use strict";var a=String.fromCharCode;const b="",c=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}},d=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[g(b.simpleText||b.runs&&b.runs[0].text),a.baseUrl+"&fmt=json3"]});return d}catch(a){}},e=(a,b,c,d)=
@domenic
domenic / generate-urltestdata.js
Created December 30, 2022 09:53
urltestdata.json generator using jsdom/whatwg-url
"use strict";
const { URL } = require(".");
const inputs = [
"https://\u0000y",
"https://x/\u0000y",
"https://x/?\u0000y",
"https://x/?#\u0000y"
];
@DarrenSem
DarrenSem / randomWithSeed.js
Last active December 21, 2023 18:07
randomWithSeed.js - use rnd() after rnd = createRandomWithSeed( seed [0+] = Date.now() ) - Math.random is less than 2x faster than this 'good enough' SEED-able version
// randomWithSeed.js - use rnd() after rnd = createRandomWithSeed( seed [0+] = Date.now() ) - Math.random is less than 2x faster than this 'good enough' SEED-able version
let createRandomWithSeed = seed => {
seed = Math.abs(isNaN(seed) ? Date.now() : seed);
return () => {
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
};
};
// R=s=>(s=Math.abs(isNaN(s)?Date.now():s),_=>(s=(9301*s+49297)%233280,s/233280));
@DarrenSem
DarrenSem / innerText.js
Last active June 24, 2024 16:18
innerText.js bookmarklet for mobile (to show webpage text contents OR html) (including PARTIAL source of SELECTION)
// innerText.js bookmarklet for mobile (to show webpage text contents OR html) (including PARTIAL source of SELECTION)
// https://gist.github.com/DarrenSem/7e1216bda80e6269ad53b43d90603158
// BUGFIXED: 24Jun2024 regressive bug caused role to always be SYSTEM (it was only using the FIRST instance of div.interactive)
// 4322 char javascript:void function(){"use strict";const a=((a,b,c,e,f,g,d,h)=>(b=new Date(a||Date()),[c,e,,f]=b.toLocaleTimeString().replace(/(a|p)\.(m)\./i,"$1$2").split(/\W/),[,,g,d]=b.toString().split(" "),h=b.toLocaleString().replace(/(\d+)-(\d+)-(\d+)/,"$2/$1/$3").split("/")[0].padStart(2,0),`${c}${e}${(f||"").toLowerCase()} ${d}-${h}-${g}`))();let b=`${""} ${a}`;const c=top.document,d=location,e=(a,b)=>(b||c).querySelectorAll(a||null),f=(a,b)=>{let c=a?.innerText;return c==b?b:c.replace(/\n{3,}/g,"\n\n\n").replace(/\n\n/g,"\n")},g=a=>a.trim(),h=(a,b,c)=>{let d,h;c?(d=e("div.interactive>div",a?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement)[0],h
@DarrenSem
DarrenSem / contentsInNewWindow.js
Last active October 15, 2023 17:28
OpenInNewWindow(data, type) - F5-REFRESHABLE! - auto-UTF8 if type==="text"! - also ContentsInNewWindow(contents = '' or [], Title, type, delimToJoinArray = "\n\n")
// contentsInNewWindow.js - F5-REFRESHABLE! - with TITLE! - (contents = '' or [], title, type = 'octet-stream', array-joining-delim = '\n\n')
// const contentsInNewWindow=(c=[],T,t="octet-stream",d="\n\n")=>{const w=open(URL.createObjectURL(new Blob([[c].flat(1/0).join(d)],{type:"text"===t?t+"/plain;charset=utf-8":t})),"_blank");return w&&!w.closed&&(w.onload=()=>w.document.title=T),w};
const contentsInNewWindow = (contents = [], title, type = ["text", "octet-stream"][1], delim = "\n\n") => {
const win = open(
URL.createObjectURL(
new Blob([
[contents].flat(1/0).join(delim)
], {
// auto-UTF8 if type === 'text', to easily ensure visible emojis etc.
@DarrenSem
DarrenSem / dtString.js
Created November 6, 2022 20:02
dtString.js (dateArg, includeTime, defaultDate, defaultReturnValue) '1-2-2022 1:00' returns '02Jan2022 100am' or '02Jan2022'
// dtString.js (dateArg, includeTime, defaultDate, defaultReturnValue) '1-2-2022 1:00' returns '02Jan2022 100am' or '02Jan2022'
const assert=(...a)=>!a.reduce((b,c,d,e,f)=>(f=("function"==typeof c?!c():!c)&&[2>a.length?c:`<arg #${d+1}> ${a.map((a,b)=>`#${b+1} = [ ${a} ]`).join(" , ")}`],f&&console.assert(0,...b[b.push(f)-1]),b),a.length?[]:[a]).length;
const assertMessage = (test, ...messageAndSubstitutions) => assert(test) || console.error(...messageAndSubstitutions) || false;
console.clear();
// 183 chars: const dtString=(a,b,c,e,f=new Date(a),[,,,,g,h,,i]=(f=+f?f:new Date(c)).toLocaleString().toLowerCase().split(/\W/),[,j,k,d]=f.toString().split(" "))=>isNaN(f)?e:`${k}${j}${d}${b?` ${g}${h}${i}`:""}`
const dtString = (dateArg, includeTime, defaultDate, defaultReturnValue, z = new Date(dateArg), [, , , , h, n, , ampm] = (z = +z ? z : new Date(defaultDate)).toLocaleString().toLowerCase().split(/\W/), [, m, d, y] = z.toString().split(" ")) => isNaN(z) ? defaultReturnValue : `${d}${m}${y}${includeTime ? `
@DarrenSem
DarrenSem / download.js
Last active November 18, 2022 00:09
download.js: SAVE data TO file (aka 'export/download') plus openInNewWindow(data, type = "octet-stream")
/////// download.js: SAVE data TO file (aka 'export/download')...
//// const download_MIN=(d,f=`file-${+new Date}.txt`,t="octet-stream")=>{const h=URL.createObjectURL(new Blob([d].slice(void 0===d),{type:t})),s=null!==f;return Object.assign(document.createElement("a"),{href:h,[s&&"download"]:f,target:"_blank"}).click(),s&&URL.revokeObjectURL(h)};
const download = (
data
, filename = `file-${+new Date}.txt` // null means download: filename OMITTED = DISPLAY data in browser
, type = "octet-stream" // default is ~ "application/octet-binary"
) => {
const href = URL.createObjectURL(new Blob( // https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob
[data].slice(data === undefined)
@anuraghazra
anuraghazra / minimal-templating-engine.ts
Last active October 28, 2023 09:07
Minimal Templating Engine In Javascript
// magic!
function dom<Props extends Record<string, unknown>>(
str: TemplateStringsArray,
...args: string[] | ((props: Props) => string)[]
) {
const interleaved = args.flatMap((arg, index) => {
return [arg, str[index + 1]];
});
return (props?: Props) =>