Skip to content

Instantly share code, notes, and snippets.

View DarrenSem's full-sized avatar

Darren Semotiuk DarrenSem

View GitHub Profile
@DarrenSem
DarrenSem / REPL.js
Last active January 16, 2023 16:45
REPL.js AKA eval.js -- Bookmarklet to append to current webpage a text input field + global-scope [EVAL] button - ALSO evaluates console.[log|error|warn|info|assert]
// REPL.js AKA eval.js -- REPL.js AKA eval.js -- Bookmarklet to append to current webpage a text input field + global-scope [EVAL] button - ALSO evaluates console.[log|error|warn|info|assert]
// javascript:void function(){var a=document,b=(b,c)=>Object.assign(a.createElement(b||"button"),c),c=b("pre"),d=b("div",{contentEditable:!0}),e="innerText",f=b(0,{[e]:"EVAL",onclick:(a,b,c,f=new Date().toLocaleTimeString(),g=location,h,i=/\bconsole\s*\.(log|error|warn|info|assert)\s*\(([^)])/g)=>{try{a=d[e],b=(g._=[],(i.test(a)?"let _=location,_log=(...a)=>{_._.push(a.join(\" \"));"+"\nlet m=(String(a[0]).match(/^<([aefgilnorw_ts]{3,6})>$/)||[])[1]||\"log\";console[m](...(a.slice(m==\"assert\"?1:0)))"+"\n};\n":"")+a).replace(i,`_log('<$1>', $2`),c=(0,eval)(b)+"",g._&&(c=g._.concat([c]).join("\n"))}catch(a){c=a.stack||a.message,c+="\n"+b}d[e]+=/[\n\r]/.test(c)?`\n\n/\* ${f}:\n${c.replace(/\*\//g,"* /")}\n*\/`:`\n// ${f}: ${c}`,d.focus()}});[f,c].forEach(({style:a},b)=>{a.border=b?"solid":"ridge",a.background=b?"#0aa":"#
@DarrenSem
DarrenSem / xt.js
Last active December 28, 2022 07:10
xt.js
console.log("TOP");
exports.importedInfo = [, 3, 6, 9, exports && exports.length, "someGlobal:", typeof someGlobal !== "undefined" && someGlobal];
module.exports.xyz = 123;
alert('executing xt.js?' + location.href);
@DarrenSem
DarrenSem / HTML.js
Created December 1, 2022 01:15
HTML.js - display full webpage source (document.documentElement.outerHTML) into a new tab popup
// HTML.js - display full webpage source (document.documentElement.outerHTML) into a new tab popup
// RUN = 304 char: javascript:void function(){let a=open("about:blank").document,b=a.body;b.appendChild(a.createElement("b")).innerText=a.title=`Source code of ${location.href}`;let c=b.appendChild(a.createElement("pre"));c.innerText=document.documentElement.outerHTML,c.style.whiteSpace="pre-wrap",c.style.color="blue"}();
let doc = open('about:blank').document;
let body = doc.body;
let tagForTitle = "b";
body.appendChild(doc.createElement(tagForTitle)).innerText = doc.title = `Source code of ${location.href}`;
@DarrenSem
DarrenSem / selectedHtml.js
Last active November 30, 2022 20:00
selectedHtml.js -- returns [htmlString, htmlString.length] , args are ALL optional ( doc, selection = doc.getSelection(), range = selection.getRangeAt(0) )
// selectedHtml.js -- returns [htmlString, htmlString.length] , args are ALL optional ( doc, selection = doc.getSelection(), range = selection.getRangeAt(0) )
// const selectedHtml=(b=document,c=b.getSelection(),d=c&&c.getRangeAt(0),e=d&&a.createElement("div"),f=e?(e.appendChild(d.cloneContents()),e.innerHTML):"")=>[f,f.length];
const selectedHtml = (
_doc = document,
_sel = _doc.getSelection(), // "You can call Window.getSelection(), which works identically to Document.getSelection()." https://developer.mozilla.org/en-US/docs/Web/API/Document/getSelection
_range = _sel && _sel.getRangeAt(0),
_el = _range && doc.createElement("div"),
_result = !_el ? "" : (
@DarrenSem
DarrenSem / htmlDecode.js
Created November 29, 2022 18:28
htmlDecode.js htmlEncode.js - using modern browsers' built-in DOM manipulation (plus common escaping for htmlEncode)
// htmlDecode.js htmlEncode.js - using modern browsers' built-in DOM manipulation (plus common escaping for htmlEncode)
// let htmlDecode=a=>Object.assign(document.createElement("textarea"),{innerHTML:null==a?"":a.replace(/<\s*br\s*\/?\s*>/gi,"\n")}).value;
let htmlDecode = html => Object.assign(
document.createElement("textarea"), {
innerHTML: html == null ? "" : html.replace(/<\s*br\s*\/?\s*>/gi, "\n")
}
).value;
// let htmlEncode=a=>Object.assign(document.createElement("div"),{innerText:null==a?"":a}).innerHTML.replace(/<br>/g,"\n").replace(/['"\t\f\r\n\u00A0-\u2666]/g,a=>({"'":"&#039;",'"':"&quot;"})[a]||`&#${a.charCodeAt(0)};`);
@DarrenSem
DarrenSem / store.js
Created November 26, 2022 22:26
store.js ALL-IN-ONE function: () = dump Storage, (keyOrNS, value OTHER THAN undefined) = return value after setting it, (keyOrNS) = return value - UNDEFINED if missing, (null, keyOrNS) = removeItem(keyOrNS) then return keyOrNS
// store.js ALL-IN-ONE function: () = dump Storage, (keyOrNS, value OTHER THAN undefined) = return value after setting it, (keyOrNS) = return value - UNDEFINED if missing, (null, keyOrNS) = removeItem(keyOrNS) then return keyOrNS
// let store=(k,d,s=localStorage,z=s.getItem(k))=>null==k?null==d?s:(s.removeItem(d),d):void 0===d?null==z?void 0:JSON.parse(z):(s.setItem(k,JSON.stringify(d,0,"\t")),d);
let store = (
keyOrNS,
data,
storage = [localStorage, sessionStorage][0],
_z = storage.getItem(keyOrNS)
) => {
@DarrenSem
DarrenSem / Math.sum [ , Math.avg , Math.max , Math.sum ] .js
Created November 24, 2022 18:06
Math.sum.js and Math.avg (average) and Math.max and Math.sum -- ALL will automatically flatten (all, args, passed, [[even, [, Arrays]]])
// Math.sum.js and Math.avg and Math.max and Math.sum -- ALL will automatically flatten (all, args, passed, [[even, [, Arrays]]])
console.log(Math.sum);
// undefined
console.log(Math.avg);
// undefined
console.log(Math.max);
// ƒ max() { [native code] }
@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 March 5, 2024 22:47
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
// FEAT: 05Mar2024 prompt skipped if isLocalFileMHT (but NOT for every "file:" === location.protocol) because MHT files apparently disables prompt() (even when from F12 -- since they won't run Bookmarklets)
// 3458 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().split(/\W/),[,,g,d]=b.toString().split(" "),h=b.toLocaleString().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,c)=>{let d,f,g;c?(d=e(b?"div>div>span.chat-message-role-text":"div",a)[0],f=b?d?.parentElement?.parentElement?.parentElement:a,g=f&&e("div>textarea.text-input",f)[0]):(f=e(":scope>div>div>div+div>div>div>div",a)[0],d=f&&f.parentElement.parentE
@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.