Skip to content

Instantly share code, notes, and snippets.

@asafh
asafh / pairs.js
Last active October 5, 2021 23:49
Ranked Pair voting
/**
* Runs electSingle until at least *wanted* winners are elected, returns the first *wanted* winners.
* @param candidates see electSingle
* @param getScore see electSingle
* @param ballots see electSingle
* @param wanted the number of wanted winners
* @returns [candidate] Array of candidates of size *wanted* (given that there are enough candidates)
*/
function elect(candidates, getScore, ballots, wanted) {
if(typeof wanted !== "number") {
@asafh
asafh / inbox_someday.js
Last active January 25, 2019 08:43
Restore Google Inbox Snooze to Someday
/*
Updated to support when "This Weekend" option doesn't exist.
Put the following link in a bookmark in your bookmark bar and when selecting snooze duration
(after clikcing the snooze icon) you may click this bookmark to modify the first snooze option to be the "Someday" option.
You need to click the bookmark everytime you want to snooze Someday (after clicking the snooze icon to show the menu).
*/
javascript:(function() { const l = document.querySelector("[data-jsaction*='global.snooze_']"); l.dataset.jsaction = 'global.snooze_someday'; l.querySelector('span.do').innerText = 'Someday';})(); void 0;
//Unfortunately this no longer works as the "snooze_someday" action has been removed internally from the JS API (rather than just from the UI)
@asafh
asafh / viewtrade.js
Last active September 24, 2018 16:04
Viewtrade.com sort Portfolio table.
//Paste in console or create a javascript: bookmark
//Dual number cells are sorted by the first number (e.g. absolute value rather than percentage).
//Every refresh of the table disables the sort functionility and needs to be reapplied.
//To sort by a column click its header.
(() => {
const tbl = document.querySelector("#portfolioDiv table.datatable");
const header = tbl.querySelector("#portfolioHeader");
const totals = tbl.querySelector("tr.totalsRow");
const dataRows = Array.from(tbl.querySelectorAll("tr.data"));