Skip to content

Instantly share code, notes, and snippets.

@ckarnell
Created August 17, 2025 22:34
Show Gist options
  • Select an option

  • Save ckarnell/c03bb0333bfd54e84f647f0d4f63e3d1 to your computer and use it in GitHub Desktop.

Select an option

Save ckarnell/c03bb0333bfd54e84f647f0d4f63e3d1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name PickerWheel: set "water world" weight to 1% of total weights
// @namespace pw-weight
// @match https://pickerwheel.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
const TARGET = "water world";
const norm = s => (s ?? "").toString().toLowerCase().replace(/\s+/g, "");
const isNum = n => Number.isFinite(n) && !Number.isNaN(n);
function rewritePersistPrimary(jsonStr) {
let outer;
try { outer = JSON.parse(jsonStr); } catch {
console.log("[PW] Could not parse outer JSON:", jsonStr);
return null;
}
if (!outer || typeof outer !== "object" || typeof outer.choices !== "string") {
console.log("[PW] No choices field in outer JSON");
return null;
}
let choices;
try { choices = JSON.parse(outer.choices); } catch {
console.log("[PW] Could not parse inner choices:", outer.choices);
return null;
}
if (!Array.isArray(choices)) {
console.log("[PW] choices not array", choices);
return null;
}
console.log("[PW] Current choices:", choices);
const targetIdx = choices.findIndex(c => norm(c?.name) === norm(TARGET));
if (targetIdx < 0) {
console.log("[PW] No match for target:", TARGET);
return null;
}
const sumOther = choices.reduce((sum, c, i) => {
if (i === targetIdx) return sum;
const w = +c?.weight;
return sum + (isNum(w) ? w : 1);
}, 0);
const waterWorldPercent = 1;
const newWeight = Number((sumOther / (100 - waterWorldPercent)).toFixed(6));
console.log(`[PW] sumOther=${sumOther}, newWeight=${newWeight}`);
if (!isNum(newWeight) || newWeight <= 0) {
console.log("[PW] Computed invalid newWeight");
return null;
}
choices[targetIdx].weight = newWeight;
console.log("[PW] Updated target choice:", choices[targetIdx]);
outer.choices = JSON.stringify(choices);
return JSON.stringify(outer);
}
// Patch existing value at start
try {
const k = "persist:primary";
const v = localStorage.getItem(k);
if (v) {
console.log("[PW] Initial persist:primary found");
const newV = rewritePersistPrimary(v);
if (newV && newV !== v) {
console.log("[PW] Rewriting initial persist:primary");
Storage.prototype.setItem.call(localStorage, k, newV);
//
window.dispatchEvent(new StorageEvent("storage", {
key: "persist:primary",
oldValue: null,
newValue: localStorage.getItem("persist:primary"),
storageArea: localStorage
}));
//
}
} else {
console.log("[PW] No initial persist:primary in localStorage");
}
} catch (e) {
console.error("[PW] Error during initial patch", e);
}
// Intercept future writes
const origSetItem = Storage.prototype.setItem;
Storage.prototype.setItem = function (k, v) {
console.log("[PW] setItem called:", k);
try {
if (k === "persist:primary" && typeof v === "string") {
console.log("[PW] Intercepting persist:primary write");
const newV = rewritePersistPrimary(v);
if (newV) {
console.log("[PW] Rewriting persist:primary with new weights");
return origSetItem.call(this, k, newV);
}
}
} catch (e) {
console.error("[PW] Error in setItem hook", e);
}
return origSetItem.apply(this, arguments);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment