Add userscript build step

This commit is contained in:
Idrees Hassan
2025-08-14 17:00:15 -04:00
parent e6435f81b3
commit cf3680e7dc
4 changed files with 1944 additions and 75 deletions

63
birb.js
View File

@@ -1,15 +1,3 @@
// ==UserScript==
// @name birb
// @namespace https://idreesinc.com
// @version 2025-01-09
// @description birb
// @author Idrees
// @match *://*/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// ==/UserScript==
// @ts-check
// @ts-ignore
@@ -58,8 +46,8 @@ const styles = `
--neg-border-size: calc(var(--border-size) * -1);
--double-border-size: calc(var(--border-size) * 2);
--neg-double-border-size: calc(var(--neg-border-size) * 2);
--border-color: #000000;
--highlight: #ffa3cb;
--border-color: var(--highlight);
}
#birb {
@@ -108,6 +96,7 @@ const styles = `
flex-direction: column;
animation: pop-in 0.08s;
transition-timing-function: ease-in;
drop-shadow(0 0 0 var(--border-color));
}
#${MENU_ID} {
@@ -479,7 +468,7 @@ const HEART_BORDER = "heart-border";
const HEART_SHINE = "heart-shine";
const FEATHER_SPINE = "feather-spine";
const SPRITESHEET_COLOR_MAP = {
const SPRITE_SHEET_COLOR_MAP = {
"transparent": TRANSPARENT,
"#ffffff": BORDER,
"#000000": OUTLINE,
@@ -609,7 +598,7 @@ const species = {
[WING_EDGE]: "#282065",
}),
redAvadavat: new BirdType("Red Avadavat",
"Native to India and southeast Asia, these birds are also known as Strawberry Finches due to their speckled plummage.", {
"Native to India and southeast Asia, these birds are also known as Strawberry Finches due to their speckled plumage.", {
[BEAK]: "#f71919",
[FOOT]: "#af7575",
[FACE]: "#cb092b",
@@ -628,7 +617,7 @@ const species = {
[WING_EDGE]: "#ebebeb",
}),
americanRobin: new BirdType("American Robin",
"While not a true robin, this social North American bird is so named due to its orange colouring. It seems unbothered by nearby humans.", {
"While not a true robin, this social North American bird is so named due to its orange coloring. It seems unbothered by nearby humans.", {
[BEAK]: "#e89f30",
[FOOT]: "#9f8075",
[FACE]: "#2d2d2d",
@@ -662,17 +651,17 @@ const SPRITE_WIDTH = 32;
const DECORATIONS_SPRITE_WIDTH = 48;
const FEATHER_SPRITE_WIDTH = 32;
const SPRITE_SHEET_URI = "__SPRITE_SHEET__";
const DECORATIONS_SPRITE_SHEET_URI = "__DECORATIONS_SPRITE_SHEET__";
const FEATHER_SPRITE_SHEET_URI = "__FEATHER_SPRITE_SHEET__";
const SPRITE_SHEET = "__SPRITE_SHEET__";
const DECORATIONS_SPRITE_SHEET = "__DECORATIONS_SPRITE_SHEET__";
const FEATHER_SPRITE_SHEET = "__FEATHER_SPRITE_SHEET__";
/**
* Load the spritesheet and return the pixelmap template
* Load the sprite sheet and return the pixel-map template
* @param {string} dataUri
* @param {boolean} [templateColors]
* @returns {Promise<string[][]>}
*/
function loadSpritesheetPixels(dataUri, templateColors = true) {
function loadSpriteSheetPixels(dataUri, templateColors = true) {
return new Promise((resolve, reject) => {
const img = new Image();
img.src = dataUri;
@@ -706,11 +695,11 @@ function loadSpritesheetPixels(dataUri, templateColors = true) {
row.push(hex);
continue;
}
if (SPRITESHEET_COLOR_MAP[hex] === undefined) {
if (SPRITE_SHEET_COLOR_MAP[hex] === undefined) {
error(`Unknown color: ${hex}`);
row.push(TRANSPARENT);
}
row.push(SPRITESHEET_COLOR_MAP[hex]);
row.push(SPRITE_SHEET_COLOR_MAP[hex]);
}
hexArray.push(row);
}
@@ -722,7 +711,7 @@ function loadSpritesheetPixels(dataUri, templateColors = true) {
});
}
Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECORATIONS_SPRITE_SHEET_URI, false), loadSpritesheetPixels(FEATHER_SPRITE_SHEET_URI)]).then(([birbPixels, decorationPixels, featherPixels ]) => {
Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).then(([birbPixels, decorationPixels, featherPixels ]) => {
const SPRITE_SHEET = birbPixels;
const DECORATIONS_SPRITE_SHEET = decorationPixels;
const FEATHER_SPRITE_SHEET = featherPixels;
@@ -844,8 +833,8 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
* @param {string} text
* @param {() => void} action
*/
constructor(text, action) {
super(text, action, undefined, true);
constructor(text, action, removeMenu = true) {
super(text, action, removeMenu, true);
}
}
@@ -859,7 +848,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
new MenuItem("Pet Birb", pet),
new MenuItem("Field Guide", insertFieldGuide),
// new MenuItem("Decorations", insertDecoration),
new MenuItem("Applications", () => switchMenuItems(otherItems), false),
new DebugMenuItem("Applications", () => switchMenuItems(otherItems), false),
new MenuItem("Hide Birb", hideBirb),
new DebugMenuItem("Reset Data", resetSaveData),
new DebugMenuItem("Unlock All", () => {
@@ -942,7 +931,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
/**
* @returns {boolean} Whether the script is running in a userscript extension context
*/
function isUserscript() {
function isUserScript() {
// @ts-ignore
return typeof GM_getValue === "function";
}
@@ -953,15 +942,15 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
function load() {
let saveData = {};
if (isUserscript()) {
log("Loading save data from userscript storage");
if (isUserScript()) {
log("Loading save data from UserScript storage");
// @ts-ignore
saveData = GM_getValue("birbSaveData", {}) ?? {};
} else if (isTestEnvironment()) {
log("Test environment detected, loading save data from localStorage");
saveData = JSON.parse(localStorage.getItem("birbSaveData") ?? "{}");
} else {
log("Not a userscript");
log("Not a UserScript");
}
log("Loaded data: " + JSON.stringify(saveData));
unlockedSpecies = saveData.unlockedSpecies ?? [DEFAULT_BIRD];
@@ -974,28 +963,28 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
unlockedSpecies: unlockedSpecies,
currentSpecies: currentSpecies,
};
if (isUserscript()) {
log("Saving data to userscript storage");
if (isUserScript()) {
log("Saving data to UserScript storage");
// @ts-ignore
GM_setValue("birbSaveData", saveData);
} else if (isTestEnvironment()) {
log("Test environment detected, saving data to localStorage");
localStorage.setItem("birbSaveData", JSON.stringify(saveData));
} else {
log("Not a userscript");
log("Not a UserScript");
}
}
function resetSaveData() {
if (isUserscript()) {
log("Resetting save data in userscript storage");
if (isUserScript()) {
log("Resetting save data in UserScript storage");
// @ts-ignore
GM_deleteValue("birbSaveData");
} else if (isTestEnvironment()) {
log("Test environment detected, resetting save data in localStorage");
localStorage.removeItem("birbSaveData");
} else {
log("Not a userscript");
log("Not a UserScript");
}
load();
}