Skip to content

Instantly share code, notes, and snippets.

View segdeha's full-sized avatar
🏠
Working from home

Andrew Hedges segdeha

🏠
Working from home
View GitHub Profile
@segdeha
segdeha / romans.js
Created July 27, 2023 23:53
Bookmarklet to quickly return the decimal version of any Roman numeral
javascript:(function romans(){const romans={I:1,V:5,X:10,L:50,C:100,D:500,M:1000};function getDecimalFromRomanNumeral(romanNumeral){let decimal;decimal=0;for (let i=0,l=romanNumeral.length;i<l;++i) {if(romanNumeral[i+1]&&romans[romanNumeral[i]]<romans[romanNumeral[i+1]]){decimal+=romans[romanNumeral[i+1]]-romans[romanNumeral[i]];++i;}else{decimal+=romans[romanNumeral[i]];}}return decimal;}const input=prompt('Roman numerals');const result=getDecimalFromRomanNumeral(input.toUpperCase());alert(result);})();
@segdeha
segdeha / rando-passwd.js
Created October 6, 2022 16:40
Bookmarklet: Create a random password and copy it to the pasteboard
javascript:(function()%7Bfunction Types()%7Bthis.characterTypes%3D%5B%27uppercase%27,%27lowercase%27,%27number%27,%27dash%27%5D%3Bthis.limits%3D%7Buppercase:1,lowercase:13,number:1,dash:1%7D%7DTypes.prototype.getValidType%3Dfunction()%7Bconst types%3D%5B%5D%3Bif(this.limits.uppercase>0)%7Btypes.push(%27uppercase%27)%7Dif(this.limits.lowercase>0)%7Btypes.push(%27lowercase%27)%7Dif(this.limits.number>0)%7Btypes.push(%27number%27)%7Dif(this.limits.dash>0)%7Btypes.push(%27dash%27)%7Dconst type%3Dtypes%5BgetRandomNumber(0,types.length-1)%5D%3Breturn type%7D%3BTypes.prototype.decrement%3Dfunction(type)%7Bthis.limits%5Btype%5D-%3D1%3Bif(this.limits%5Btype%5D<0)%7Bthis.limits%5Btype%5D%3D0%7D%7D%3Bfunction getRandomNumber(min,max)%7Breturn Math.floor(Math.random()*(max-min%2B1))%2Bmin%7Dfunction getValidType()%7Breturn(new Types()).getValidType()%7Dfunction getCharacterCode(types,type)%7Blet number%3Bswitch(type)%7Bcase %27uppercase%27:number%3DgetRandomNumber(65,90)%3Btypes.decrement(%27uppercase%27)%3Bbreak%3Bcase
@segdeha
segdeha / bookmarklet: noqs
Created October 6, 2022 16:40
Quickly remove the query string from an URL
javascript:location.href=location.href.split('?%27)%5B0%5D;
@segdeha
segdeha / get-week-number.js
Created April 15, 2021 16:55
Return the number of the week (e.g., 16 out of 52 weeks)
function getWeekNumber() {
const now = new Date()
const oneJan = new Date(now.getFullYear(), 0, 1)
const numDays = Math.floor((now - oneJan) / (24 * 60 * 60 * 1000))
return Math.ceil(( now.getDay() + 1 + numDays) / 7)
}
@segdeha
segdeha / nearby.php
Last active August 4, 2021 17:47
Pass-through proxy for Wikipedia’s “nearby” API
<?php
// pass through proxy for wikipedia’s “nearby” api
function isValidOrigin() {
$host = $_SERVER['HTTP_HOST'];
$origin = $_SERVER['HTTP_ORIGIN'];
$allowed_hosts = array(
'segdeha.com',
);
function Counter(count, displaySelector, buttonSelector) {
this.count = count
this.displayElement = document.querySelector(displaySelector)
this.buttonElement = document.querySelector(buttonSelector)
this.setCount()
}
Counter.prototype = {
/**
* Calculate a weighted estimate for the interval until the next purchase
* Current purchase a tiny bit less weight than all previous purchases
* @param {Number} lastEstimate The last stored purchase interval estimate
* @param {Number} latestInterval The interval between the most recent and previous purchases
* @param {Number} numberOfPurchases Total number of purchases for the item
*/
const calculateEstimate = (lastEstimate, latestInterval, numberOfPurchases) => {
if (isNaN(lastEstimate)) {
lastEstimate = 14;
const words = [ "aaa", "aaaa", "aaron", "aba", "ababa", "aback", "abase", "abash", "abate", "abbas", "abbe", "abbey", "abbot", "abbott", "abc", "abe", "abed", "abel", "abet", "abide", "abject", "ablaze", "able", "abner", "abo", "abode", "abort", "about", "above", "abrade", "abram", "absorb", "abuse", "abut", "abyss", "acadia", "accra", "accrue", "ace", "acetic", "ache", "acid", "acidic", "acm", "acme", "acorn", "acre", "acrid", "act", "acton", "actor", "acts", "acuity", "acute", "ada", "adage", "adagio", "adair", "adam", "adams", "adapt", "add", "added", "addict", "addis", "addle", "adele", "aden", "adept", "adieu", "adjust", "adler", "admit", "admix", "ado", "adobe", "adonis", "adopt", "adore", "adorn", "adult", "advent", "advert", "advise", "aegis", "aeneid", "afar", "affair", "affine", "affix", "afire", "afoot", "afraid", "africa", "afro", "aft", "again", "agate", "agave", "age", "agee", "agenda", "agent", "agile", "aging", "agnes", "agnew", "ago", "agone", "agony", "agree", "ague", "agway", "ahead", "ahem
const capitalise = name => name.slice(0, 1).toUpperCase() +
name.slice(1, name.length).toLowerCase();
let name = 'diane';
alert(`Hi ${capitalise(name)}. You are pretty cool!`);
let name = 'diane';
let name_capitalised = name.slice(0, 1).toUpperCase() +
name.slice(1, name.length).toLowerCase();
alert(`Hi ${name_capitalised}. You are pretty cool!`);