scraps
things that weren't interesting enough to blog about but are still kind of cool. mostly just code snippets, thoughts, half-ideas, etc. mostly javascript. source: my snippets.js on github
things that weren't interesting enough to blog about but are still kind of cool. mostly just code snippets, thoughts, half-ideas, etc. mostly javascript. source: my snippets.js on github
get current NIST pulse information
const getBeaconPulse = () => {
const url =
"https://cors-anywhere.herokuapp.com/https://beacon.nist.gov/beacon/2.0/pulse/last";
const go = () => {
let resp;
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => (resp = xhr.responseText);
xhr.open("GET", url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onload = resp => resolve(resp);
xhr.onerror = err => reject(new Error(err));
xhr.send();
});
};
const returnPulse = async () => {
let pulse = await go();
let json = JSON.parse(pulse.target.responseText);
pulse = json.pulse;
console.log(pulse);
return pulse;
};
return returnPulse();
};
call getBeaconPulse() - no arguments.
convert hexadecimal (hex) color code to rgba or rgb color in javascript. source: my snippets.js on github
var hexToRGBA=function(b){if(!b||"string"!=typeof b||3>b.length)throw Error(b,"is not a valid argument.");if(6==b.length){var d=parseInt(b,16),c=d>>16&255;b=d>>8&255;d&=255;if(isNaN(a)||isNaN(c)||isNaN(b)||isNaN(d))throw Error("please enter
rgb(a) values only between 0 and F.");return{r:c,g:b,b:d}}if(3==b.length){b.split("");b=[b[0],b[0],b[1],b[1],b[2],b[2]];b="0x"+b.join("");d=parseInt(b,16);c=d>>16&255;b=d>>8&255;d&=255;if(isNaN(a)||isNaN(c)||isNaN(b)||isNaN(d))throw
Error("please enter rgb(a) values only between 0 and F.");
return{r:c,g:b,b:d}}if(8==b.length){c=b.substring(6,8);b=b.substring(0,6);var e=parseInt(b,16);b=e>>16&255;d=e>>8&255;e&=255;c=parseInt("0x"+c);if(isNaN(c)||isNaN(b)||isNaN(d)||isNaN(e))throw Error("please enter rgb(a) values only between 0
and
F.");return{r:b,g:d,b:e,a:c}}if(4==b.length){c=b.substring(3,4).split("");b=b.substring(0,3).split("");b=[b[0],b[0],b[1],b[1],b[2],b[2]];b="0x"+b.join("");c=[c[0],c[0]];c="0x"+c.join("");c=parseInt(c);e=parseInt(b,16);b=e>>16&255;d=e>>8&255;e&=255;if(isNaN(c)||
isNaN(b)||isNaN(d)||isNaN(e))throw Error("please enter rgb(a) values only between 0 and F.");return{r:b,g:d,b:e,a:c}}throw Error(b,"is not valid, or you entered some weird format that I forgot about.");};
get a time-based one-time password. usage:
getTOTP(secret, length)
source: my snippets.js on github
var getTOTP=function(h,k){for(var d=Math.round((new Date).getTime()/1E3),d=function(a,b,c){return
b+1>=a.length?Array(b+1-a.length).join(c)+a:a}(function(a){return(15.5>a?"0":"")+Math.round(a).toString(16)}(Math.floor(d/30)),16,"0"),l=function(a){var b,c="";for(i=0;i< a.toString().length;i++)b=a.toString().charCodeAt(i).toString(16),c+=b;return
c.replace(/\D/g,"")}(h),f=[],e=1;e < k+1;e++){var g=parseInt(d,16)*e*parseInt(l.toString().substring(0,8));f.push(g.toString().charAt(g.toString().length-3))}return f.join("")};
get a random youtube video (wip) usage: randomYoutubeVideo()
source: my snippets.js on github
const randomYoutubeVideo = () => {
const reqUrl = "https://youtube.com/watch?v=";
const baseUrl =
"https://cors-anywhere.herokuapp.com/https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=";
const charList =
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
const rUrl = () => {
let output = "";
for (var i = 0; i < 11; i++) { output +=Array.from(charList)[(Math.random() * charList.length) | 0]; } return { request: `${baseUrl + output}`, set: `${reqUrl + output}` }; }; const checkVid=a=> {
return new Promise((resolve, reject) => {
fetch(rUrl().request, {
method: "HEAD",
headers: {
origin: "https://example.com"
}
})
.then(resp => {
console.log(resp);
if (resp.status != 200) {
setTimeout(function() {
return randomYoutubeVideo();
}, 2000);
} else resolve(resp);
})
.catch(err => {
return new Error(err);
});
});
};
checkVid(rUrl()).then(resp => {
let tab = window.open(rUrl().set, "_blank");
tab.focus();
return resp.url;
});
};
how to figure out when a google chrome bookmark was created. chrome's bookmarks use a different time format than the usual javascript format. it uses time since jan 1st, 1970 in microseconds. usage: chromeDtToDate(date)
source: my snippets.js on github
const chromeDtToDate = st_dt => {
const microseconds = parseInt(st_dt, 10);
const millis = microseconds / 1000;
const past = new Date(1601, 0, 1).getTime();
return new Date(past + millis);
};