From a94cc92c520c636c172400e827e3318ed6278533 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Tue, 16 Sep 2025 17:10:08 -0400 Subject: [PATCH 01/11] Add extension manifest --- birb.js | 6 ++++-- build.js | 14 +++++++++++++- dist/birb.js | 6 ++++-- dist/birb.user.js | 8 +++++--- manifest.json | 29 +++++++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 manifest.json diff --git a/birb.js b/birb.js index 4a416e9..c204cc6 100644 --- a/birb.js +++ b/birb.js @@ -704,7 +704,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function isTestEnvironment() { - return window.location.hostname === "127.0.0.1"; + return window.location.hostname === "127.0.0.1" + || window.location.hostname === "localhost" + || window.location.hostname.startsWith("192.168."); } function load() { @@ -958,7 +960,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastActionTimestamp = Date.now(); // Can't keep up with scrolling on mobile devices so fly down instead if (isMobile()) { - focusOnGround(); + // focusOnGround(); } }); diff --git a/build.js b/build.js index e255782..57688c4 100644 --- a/build.js +++ b/build.js @@ -20,11 +20,23 @@ const spriteSheets = [ const STYLESHEET_PATH = "./stylesheet.css"; const STYLESHEET_KEY = "___STYLESHEET___"; +let version = "0.0.0"; +// Try to read version from manifest.json +try { + const manifest = JSON.parse(readFileSync('manifest.json', 'utf8')); + if (manifest.version) { + version = manifest.version; + } +} catch (e) { + console.error("Could not read version from manifest.json"); + throw e; +} + const userScriptHeader = `// ==UserScript== // @name Browser Bird // @namespace https://idreesinc.com -// @version 2025-09-15-01 +// @version ${version} // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Browser-Bird/raw/refs/heads/main/dist/birb.user.js diff --git a/dist/birb.js b/dist/birb.js index 9c192d2..0023209 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -1043,7 +1043,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function isTestEnvironment() { - return window.location.hostname === "127.0.0.1"; + return window.location.hostname === "127.0.0.1" + || window.location.hostname === "localhost" + || window.location.hostname.startsWith("192.168."); } function load() { @@ -1297,7 +1299,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastActionTimestamp = Date.now(); // Can't keep up with scrolling on mobile devices so fly down instead if (isMobile()) { - focusOnGround(); + // focusOnGround(); } }); diff --git a/dist/birb.user.js b/dist/birb.user.js index 37cd231..96e37a8 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Browser Bird // @namespace https://idreesinc.com -// @version 2025-09-15-01 +// @version 2025.9.16.1 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Browser-Bird/raw/refs/heads/main/dist/birb.user.js @@ -1057,7 +1057,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function isTestEnvironment() { - return window.location.hostname === "127.0.0.1"; + return window.location.hostname === "127.0.0.1" + || window.location.hostname === "localhost" + || window.location.hostname.startsWith("192.168."); } function load() { @@ -1311,7 +1313,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastActionTimestamp = Date.now(); // Can't keep up with scrolling on mobile devices so fly down instead if (isMobile()) { - focusOnGround(); + // focusOnGround(); } }); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..9399648 --- /dev/null +++ b/manifest.json @@ -0,0 +1,29 @@ +{ + "manifest_version": 3, + "name": "Browser Bird", + "description": "It's a bird, in your browser. What more could you want?", + "version": "2025.9.16.1", + "homepage_url": "https://idreesinc.com", + "content_scripts": [ + { + "matches": [""], + "js": ["birb.js"] + } + ], + "permissions": [ + "storage", + "activeTab" + ], + "web_accessible_resources": [ + { + "resources": ["images/*"], + "matches": [""] + } + ], + "browser_specific_settings": { + "gecko": { + "id": "birb@idreesinc.com" + } + } +} + \ No newline at end of file diff --git a/package.json b/package.json index 4b4f18e..2e7ed7a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "type": "module", "scripts": { "build": "node build.js", - "dev": "nodemon --watch birb.js --watch stylesheet.css --watch build.js --exec \"npm run build\"" + "dev": "nodemon --watch birb.js --watch stylesheet.css --watch build.js --watch manifest.json --exec \"npm run build\"" }, "devDependencies": { "nodemon": "^3.1.10" From bf1bb74219103d09c2b911471bbe9adcbd880ee1 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 15:41:49 -0400 Subject: [PATCH 02/11] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c2de630..b73fe0d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Browser Bird (Work in Progress!) +# Pocket Bird (Work in Progress!) This project is still being worked on, but if you wish to help me beta test it, join the [Discord](https://discord.gg/6yxE9prcNc) and follow the installation instructions below! 1. Install [Tampermonkey](https://www.tampermonkey.net/) on your web browser 2. Enable the Tampermonkey extension and give it the permissions requested -3. Install my Browser Bird script by going to this link and clicking install: [https://github.com/IdreesInc/Browser-Bird/raw/refs/heads/main/dist/birb.user.js](https://github.com/IdreesInc/Browser-Bird/raw/refs/heads/main/dist/birb.user.js) +3. Install my Pocket Bird script by going to this link and clicking install: [https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js](https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js) 4. Now any websites you visit will have a little bird hopping around! \ No newline at end of file From f73e29a723dd7a76fbd8a6e0e3c5eb9914bd87d7 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 18:29:41 -0400 Subject: [PATCH 03/11] Correct padding on field guide --- birb.js | 2 +- dist/birb.js | 12 ++++++------ dist/birb.user.js | 12 ++++++------ stylesheet.css | 10 +++++----- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/birb.js b/birb.js index 64c706b..7d622f7 100644 --- a/birb.js +++ b/birb.js @@ -11,7 +11,7 @@ const SHARED_CONFIG = { const DESKTOP_CONFIG = { - flySpeed: 0.2, + flySpeed: 0.25 }; const MOBILE_CONFIG = { diff --git a/dist/birb.js b/dist/birb.js index d6825b6..71a3f0d 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -11,7 +11,7 @@ const SHARED_CONFIG = { const DESKTOP_CONFIG = { - flySpeed: 0.2, + flySpeed: 0.25 }; const MOBILE_CONFIG = { @@ -294,8 +294,8 @@ const STYLESHEET = `:root { flex-direction: row; padding-top: 4px; padding-bottom: 4px; - padding-left: 15px; - padding-right: 15px; + padding-left: 10px; + padding-right: 10px; box-sizing: border-box; } @@ -342,12 +342,12 @@ const STYLESHEET = `:root { } .birb-field-guide-description { - width: calc(100% - 16px); - margin-top: 10px; + width: calc(100% - 20px); + margin-top: 5px; padding: 8px; padding-top: 4px; padding-bottom: 4px; - margin-bottom: 6px; + margin-bottom: 10px; font-size: 14px; box-sizing: border-box; color: rgb(124, 108, 75); diff --git a/dist/birb.user.js b/dist/birb.user.js index 51bcd82..5484341 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -25,7 +25,7 @@ const SHARED_CONFIG = { const DESKTOP_CONFIG = { - flySpeed: 0.2, + flySpeed: 0.25 }; const MOBILE_CONFIG = { @@ -308,8 +308,8 @@ const STYLESHEET = `:root { flex-direction: row; padding-top: 4px; padding-bottom: 4px; - padding-left: 15px; - padding-right: 15px; + padding-left: 10px; + padding-right: 10px; box-sizing: border-box; } @@ -356,12 +356,12 @@ const STYLESHEET = `:root { } .birb-field-guide-description { - width: calc(100% - 16px); - margin-top: 10px; + width: calc(100% - 20px); + margin-top: 5px; padding: 8px; padding-top: 4px; padding-bottom: 4px; - margin-bottom: 6px; + margin-bottom: 10px; font-size: 14px; box-sizing: border-box; color: rgb(124, 108, 75); diff --git a/stylesheet.css b/stylesheet.css index 504506c..5025e81 100644 --- a/stylesheet.css +++ b/stylesheet.css @@ -241,8 +241,8 @@ flex-direction: row; padding-top: 4px; padding-bottom: 4px; - padding-left: 15px; - padding-right: 15px; + padding-left: 10px; + padding-right: 10px; box-sizing: border-box; } @@ -289,12 +289,12 @@ } .birb-field-guide-description { - width: calc(100% - 16px); - margin-top: 10px; + width: calc(100% - 20px); + margin-top: 5px; padding: 8px; padding-top: 4px; padding-bottom: 4px; - margin-bottom: 6px; + margin-bottom: 10px; font-size: 14px; box-sizing: border-box; color: rgb(124, 108, 75); From ff98390e1087dfbaf4080ea2e31662e90db57b3e Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 19:01:04 -0400 Subject: [PATCH 04/11] Fix manifest --- birb.js | 6 ++++++ dist/birb.js | 6 ++++++ dist/birb.user.js | 6 ++++++ manifest.json | 4 ++-- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/birb.js b/birb.js index 7d622f7..f0360c1 100644 --- a/birb.js +++ b/birb.js @@ -443,6 +443,8 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { }); } +log("Loading sprite sheets..."); + // @ts-ignore Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).then(([birbPixels, decorationPixels, featherPixels]) => { const SPRITE_SHEET = birbPixels; @@ -924,8 +926,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function init() { if (window !== window.top) { // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); return; } + log("Sprite sheets loaded successfully, initializing bird..."); // Preload font const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; @@ -1834,6 +1838,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } +}).catch((e) => { + error("Error while loading sprite sheets: ", e); }); /** diff --git a/dist/birb.js b/dist/birb.js index 71a3f0d..911be04 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -786,6 +786,8 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { }); } +log("Loading sprite sheets..."); + // @ts-ignore Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).then(([birbPixels, decorationPixels, featherPixels]) => { const SPRITE_SHEET = birbPixels; @@ -1267,8 +1269,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function init() { if (window !== window.top) { // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); return; } + log("Sprite sheets loaded successfully, initializing bird..."); // Preload font const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; @@ -2177,6 +2181,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } +}).catch((e) => { + error("Error while loading sprite sheets: ", e); }); /** diff --git a/dist/birb.user.js b/dist/birb.user.js index 5484341..3736584 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -800,6 +800,8 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { }); } +log("Loading sprite sheets..."); + // @ts-ignore Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).then(([birbPixels, decorationPixels, featherPixels]) => { const SPRITE_SHEET = birbPixels; @@ -1281,8 +1283,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function init() { if (window !== window.top) { // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); return; } + log("Sprite sheets loaded successfully, initializing bird..."); // Preload font const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; @@ -2191,6 +2195,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } +}).catch((e) => { + error("Error while loading sprite sheets: ", e); }); /** diff --git a/manifest.json b/manifest.json index 9399648..cd3e701 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,13 @@ { "manifest_version": 3, - "name": "Browser Bird", + "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", "version": "2025.9.16.1", "homepage_url": "https://idreesinc.com", "content_scripts": [ { "matches": [""], - "js": ["birb.js"] + "js": ["./dist/birb.js"] } ], "permissions": [ From e55f0e74127a837920eb99a45cec71c33395d37f Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 19:03:14 -0400 Subject: [PATCH 05/11] Remove PICO-8 and Spotify windows --- birb.js | 80 ----------------------------------------------- dist/birb.js | 80 ----------------------------------------------- dist/birb.user.js | 80 ----------------------------------------------- 3 files changed, 240 deletions(-) diff --git a/birb.js b/birb.js index f0360c1..3e5de3b 100644 --- a/birb.js +++ b/birb.js @@ -599,8 +599,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const menuItems = [ new MenuItem(`Pet ${birdBirb()}`, pet), new MenuItem("Field Guide", insertFieldGuide), - // new MenuItem("Decorations", insertDecoration), - new DebugMenuItem("Applications", () => switchMenuItems(otherItems), false), new MenuItem("Sticky Note", newStickyNote), new MenuItem(`Hide ${birdBirb()}`, hideBirb), new DebugMenuItem("Freeze/Unfreeze", () => { @@ -629,34 +627,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }) ]; - const otherItems = [ - new MenuItem("Go Back", () => switchMenuItems(menuItems), false), - new Separator(), - new MenuItem("Video Games", () => switchMenuItems(gameItems), false), - new MenuItem("Utilities", () => switchMenuItems(utilityItems), false), - new MenuItem("Music Player", () => insertMusicPlayer(), false), - ]; - - const gameItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pinball", () => insertPico8("Terra Nova Pinball", "terra_nova_pinball")), - new MenuItem("Pico Dino", () => insertPico8("Pico Dino", "picodino")), - new MenuItem("Woodworm", () => insertPico8("Woodworm", "woodworm")), - new MenuItem("Pico and Chill", () => insertPico8("Pico and Chill", "picochill")), - new MenuItem("Lani's Trek", () => insertPico8("Celeste 2 Lani's Trek", "celeste_classic_2")), - new MenuItem("Tetraminis", () => insertPico8("Tetraminis", "tetraminisdeffect")), - // new MenuItem("Pool", () => insertPico8("Pool", "mot_pool")), - ]; - - const utilityItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pomodoro Timer", () => insertPico8("Pomodoro", "pomodorotimerv1")), - new MenuItem("Ohm Calculator", () => insertPico8("Resistor Calculator", "picoohm")), - new MenuItem("Wobblepaint ", () => insertPico8("Wobblepaint", "wobblepaint")), - ]; - const styleElement = document.createElement("style"); const canvas = document.createElement("canvas"); @@ -1361,60 +1331,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - // insertPico8(); - function isSafari() { return /^((?!chrome|android).)*safari/i.test(navigator.userAgent); } - /** - * @param {string} name - * @param {string} pid - */ - function insertPico8(name, pid) { - let html = ` -
-
${name}
-
x
-
-
- -
` - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - - function insertMusicPlayer() { - let html = ` -
-
Music Player
-
x
-
-
- -
`; - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - /** * @param {string} type */ diff --git a/dist/birb.js b/dist/birb.js index 911be04..4c2d2ed 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -942,8 +942,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const menuItems = [ new MenuItem(`Pet ${birdBirb()}`, pet), new MenuItem("Field Guide", insertFieldGuide), - // new MenuItem("Decorations", insertDecoration), - new DebugMenuItem("Applications", () => switchMenuItems(otherItems), false), new MenuItem("Sticky Note", newStickyNote), new MenuItem(`Hide ${birdBirb()}`, hideBirb), new DebugMenuItem("Freeze/Unfreeze", () => { @@ -972,34 +970,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }) ]; - const otherItems = [ - new MenuItem("Go Back", () => switchMenuItems(menuItems), false), - new Separator(), - new MenuItem("Video Games", () => switchMenuItems(gameItems), false), - new MenuItem("Utilities", () => switchMenuItems(utilityItems), false), - new MenuItem("Music Player", () => insertMusicPlayer(), false), - ]; - - const gameItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pinball", () => insertPico8("Terra Nova Pinball", "terra_nova_pinball")), - new MenuItem("Pico Dino", () => insertPico8("Pico Dino", "picodino")), - new MenuItem("Woodworm", () => insertPico8("Woodworm", "woodworm")), - new MenuItem("Pico and Chill", () => insertPico8("Pico and Chill", "picochill")), - new MenuItem("Lani's Trek", () => insertPico8("Celeste 2 Lani's Trek", "celeste_classic_2")), - new MenuItem("Tetraminis", () => insertPico8("Tetraminis", "tetraminisdeffect")), - // new MenuItem("Pool", () => insertPico8("Pool", "mot_pool")), - ]; - - const utilityItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pomodoro Timer", () => insertPico8("Pomodoro", "pomodorotimerv1")), - new MenuItem("Ohm Calculator", () => insertPico8("Resistor Calculator", "picoohm")), - new MenuItem("Wobblepaint ", () => insertPico8("Wobblepaint", "wobblepaint")), - ]; - const styleElement = document.createElement("style"); const canvas = document.createElement("canvas"); @@ -1704,60 +1674,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - // insertPico8(); - function isSafari() { return /^((?!chrome|android).)*safari/i.test(navigator.userAgent); } - /** - * @param {string} name - * @param {string} pid - */ - function insertPico8(name, pid) { - let html = ` -
-
${name}
-
x
-
-
- -
` - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - - function insertMusicPlayer() { - let html = ` -
-
Music Player
-
x
-
-
- -
`; - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - /** * @param {string} type */ diff --git a/dist/birb.user.js b/dist/birb.user.js index 3736584..8666b1c 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -956,8 +956,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const menuItems = [ new MenuItem(`Pet ${birdBirb()}`, pet), new MenuItem("Field Guide", insertFieldGuide), - // new MenuItem("Decorations", insertDecoration), - new DebugMenuItem("Applications", () => switchMenuItems(otherItems), false), new MenuItem("Sticky Note", newStickyNote), new MenuItem(`Hide ${birdBirb()}`, hideBirb), new DebugMenuItem("Freeze/Unfreeze", () => { @@ -986,34 +984,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }) ]; - const otherItems = [ - new MenuItem("Go Back", () => switchMenuItems(menuItems), false), - new Separator(), - new MenuItem("Video Games", () => switchMenuItems(gameItems), false), - new MenuItem("Utilities", () => switchMenuItems(utilityItems), false), - new MenuItem("Music Player", () => insertMusicPlayer(), false), - ]; - - const gameItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pinball", () => insertPico8("Terra Nova Pinball", "terra_nova_pinball")), - new MenuItem("Pico Dino", () => insertPico8("Pico Dino", "picodino")), - new MenuItem("Woodworm", () => insertPico8("Woodworm", "woodworm")), - new MenuItem("Pico and Chill", () => insertPico8("Pico and Chill", "picochill")), - new MenuItem("Lani's Trek", () => insertPico8("Celeste 2 Lani's Trek", "celeste_classic_2")), - new MenuItem("Tetraminis", () => insertPico8("Tetraminis", "tetraminisdeffect")), - // new MenuItem("Pool", () => insertPico8("Pool", "mot_pool")), - ]; - - const utilityItems = [ - new MenuItem("Go Back", () => switchMenuItems(otherItems), false), - new Separator(), - new MenuItem("Pomodoro Timer", () => insertPico8("Pomodoro", "pomodorotimerv1")), - new MenuItem("Ohm Calculator", () => insertPico8("Resistor Calculator", "picoohm")), - new MenuItem("Wobblepaint ", () => insertPico8("Wobblepaint", "wobblepaint")), - ]; - const styleElement = document.createElement("style"); const canvas = document.createElement("canvas"); @@ -1718,60 +1688,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - // insertPico8(); - function isSafari() { return /^((?!chrome|android).)*safari/i.test(navigator.userAgent); } - /** - * @param {string} name - * @param {string} pid - */ - function insertPico8(name, pid) { - let html = ` -
-
${name}
-
x
-
-
- -
` - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - - function insertMusicPlayer() { - let html = ` -
-
Music Player
-
x
-
-
- -
`; - const pico8 = makeElement("birb-window"); - pico8.innerHTML = html; - document.body.appendChild(pico8); - makeDraggable(pico8.querySelector(".birb-window-header")); - const close = pico8.querySelector(".birb-window-close"); - if (close) { - makeClosable(() => { - pico8.remove(); - }, close); - } - centerElement(pico8); - } - /** * @param {string} type */ From f45eb0ce61cdca280ced6a0c25344507ced8db9b Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 20:40:25 -0400 Subject: [PATCH 06/11] Fix petting on mobile --- birb.js | 32 +++++++++++++++++++++--- build.js | 28 ++++++++++++++++++--- dist/birb.js | 32 +++++++++++++++++++++--- dist/birb.user.js | 34 +++++++++++++++++++++---- manifest.json | 63 ++++++++++++++++++++++++++--------------------- package.json | 2 +- 6 files changed, 146 insertions(+), 45 deletions(-) diff --git a/birb.js b/birb.js index 3e5de3b..831bf0a 100644 --- a/birb.js +++ b/birb.js @@ -943,6 +943,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { + if (currentAnimation === Animations.HEART) { + // Currently being pet, don't open menu + return; + } insertMenu(); }); @@ -955,13 +959,17 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } const pets = petStack.filter((time) => Date.now() - time < 1000).length; if (pets >= 3) { - setAnimation(Animations.HEART); + pet(); // Clear the stack petStack = []; } } }); + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + drawStickyNotes(); let lastUrl = (window.location.href ?? "").split("?")[0]; @@ -1448,7 +1456,23 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI */ function onClick(element, action) { element.addEventListener("click", (e) => action(e)); - element.addEventListener("touchstart", (e) => action(e)); + element.addEventListener("touchend", (e) => { + if (e instanceof TouchEvent === false) { + return; + } else if (element instanceof HTMLElement === false) { + return; + } + const touch = e.changedTouches[0]; + const rect = element.getBoundingClientRect(); + if ( + touch.clientX >= rect.left && + touch.clientX <= rect.right && + touch.clientY >= rect.top && + touch.clientY <= rect.bottom + ) { + action(e); + } + }); } /** @@ -1608,7 +1632,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function getFullWindowHeight() { return document.documentElement.clientHeight; } - + function focusOnGround() { console.log("Focusing on ground"); focusedElement = null; @@ -1678,7 +1702,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function pet() { - if (currentState === States.IDLE) { + if (currentState === States.IDLE && currentAnimation !== Animations.HEART) { setAnimation(Animations.HEART); lastPetTimestamp = Date.now(); } diff --git a/build.js b/build.js index d40d8b5..3b95188 100644 --- a/build.js +++ b/build.js @@ -20,18 +20,40 @@ const spriteSheets = [ const STYLESHEET_PATH = "./stylesheet.css"; const STYLESHEET_KEY = "___STYLESHEET___"; -let version = "0.0.0"; -// Try to read version from manifest.json +const now = new Date(); +const versionDate = `${now.getFullYear()}.${now.getMonth() + 1}.${now.getDate()}`; +let buildNumber = 1; + +// Get build number from manifest.json try { const manifest = JSON.parse(readFileSync('manifest.json', 'utf8')); if (manifest.version) { - version = manifest.version; + if (manifest.version.startsWith(versionDate)) { + // Same day, increment build number + const parts = manifest.version.split('.'); + if (parts.length === 4) { + buildNumber = parseInt(parts[3], 10) + 1; + } + } } } catch (e) { console.error("Could not read version from manifest.json"); throw e; } +const version = `${versionDate}.${buildNumber}`; + +// Update manifest.json with new version +try { + const manifest = JSON.parse(readFileSync('manifest.json', 'utf8')); + manifest.version = version; + writeFileSync('manifest.json', JSON.stringify(manifest, null, 4), 'utf8'); +} catch (e) { + console.error("Could not update version in manifest.json"); + throw e; +} + + const userScriptHeader = `// ==UserScript== // @name Pocket Bird diff --git a/dist/birb.js b/dist/birb.js index 4c2d2ed..f38dca4 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -1286,6 +1286,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { + if (currentAnimation === Animations.HEART) { + // Currently being pet, don't open menu + return; + } insertMenu(); }); @@ -1298,13 +1302,17 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } const pets = petStack.filter((time) => Date.now() - time < 1000).length; if (pets >= 3) { - setAnimation(Animations.HEART); + pet(); // Clear the stack petStack = []; } } }); + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + drawStickyNotes(); let lastUrl = (window.location.href ?? "").split("?")[0]; @@ -1791,7 +1799,23 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI */ function onClick(element, action) { element.addEventListener("click", (e) => action(e)); - element.addEventListener("touchstart", (e) => action(e)); + element.addEventListener("touchend", (e) => { + if (e instanceof TouchEvent === false) { + return; + } else if (element instanceof HTMLElement === false) { + return; + } + const touch = e.changedTouches[0]; + const rect = element.getBoundingClientRect(); + if ( + touch.clientX >= rect.left && + touch.clientX <= rect.right && + touch.clientY >= rect.top && + touch.clientY <= rect.bottom + ) { + action(e); + } + }); } /** @@ -1951,7 +1975,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function getFullWindowHeight() { return document.documentElement.clientHeight; } - + function focusOnGround() { console.log("Focusing on ground"); focusedElement = null; @@ -2021,7 +2045,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function pet() { - if (currentState === States.IDLE) { + if (currentState === States.IDLE && currentAnimation !== Animations.HEART) { setAnimation(Animations.HEART); lastPetTimestamp = Date.now(); } diff --git a/dist/birb.user.js b/dist/birb.user.js index 8666b1c..7eb5a87 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.9.16.1 +// @version 2025.10.25.1 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js @@ -1300,6 +1300,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { + if (currentAnimation === Animations.HEART) { + // Currently being pet, don't open menu + return; + } insertMenu(); }); @@ -1312,13 +1316,17 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } const pets = petStack.filter((time) => Date.now() - time < 1000).length; if (pets >= 3) { - setAnimation(Animations.HEART); + pet(); // Clear the stack petStack = []; } } }); + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + drawStickyNotes(); let lastUrl = (window.location.href ?? "").split("?")[0]; @@ -1805,7 +1813,23 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI */ function onClick(element, action) { element.addEventListener("click", (e) => action(e)); - element.addEventListener("touchstart", (e) => action(e)); + element.addEventListener("touchend", (e) => { + if (e instanceof TouchEvent === false) { + return; + } else if (element instanceof HTMLElement === false) { + return; + } + const touch = e.changedTouches[0]; + const rect = element.getBoundingClientRect(); + if ( + touch.clientX >= rect.left && + touch.clientX <= rect.right && + touch.clientY >= rect.top && + touch.clientY <= rect.bottom + ) { + action(e); + } + }); } /** @@ -1965,7 +1989,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function getFullWindowHeight() { return document.documentElement.clientHeight; } - + function focusOnGround() { console.log("Focusing on ground"); focusedElement = null; @@ -2035,7 +2059,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } function pet() { - if (currentState === States.IDLE) { + if (currentState === States.IDLE && currentAnimation !== Animations.HEART) { setAnimation(Animations.HEART); lastPetTimestamp = Date.now(); } diff --git a/manifest.json b/manifest.json index cd3e701..4383718 100644 --- a/manifest.json +++ b/manifest.json @@ -1,29 +1,36 @@ { - "manifest_version": 3, - "name": "Pocket Bird", - "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.9.16.1", - "homepage_url": "https://idreesinc.com", - "content_scripts": [ - { - "matches": [""], - "js": ["./dist/birb.js"] - } - ], - "permissions": [ - "storage", - "activeTab" - ], - "web_accessible_resources": [ - { - "resources": ["images/*"], - "matches": [""] - } - ], - "browser_specific_settings": { - "gecko": { - "id": "birb@idreesinc.com" - } - } -} - \ No newline at end of file + "manifest_version": 3, + "name": "Pocket Bird", + "description": "It's a bird, in your browser. What more could you want?", + "version": "2025.10.25.1", + "homepage_url": "https://idreesinc.com", + "content_scripts": [ + { + "matches": [ + "" + ], + "js": [ + "./dist/birb.js" + ] + } + ], + "permissions": [ + "storage", + "activeTab" + ], + "web_accessible_resources": [ + { + "resources": [ + "images/*" + ], + "matches": [ + "" + ] + } + ], + "browser_specific_settings": { + "gecko": { + "id": "birb@idreesinc.com" + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 2e7ed7a..4b4f18e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "type": "module", "scripts": { "build": "node build.js", - "dev": "nodemon --watch birb.js --watch stylesheet.css --watch build.js --watch manifest.json --exec \"npm run build\"" + "dev": "nodemon --watch birb.js --watch stylesheet.css --watch build.js --exec \"npm run build\"" }, "devDependencies": { "nodemon": "^3.1.10" From 3aa0308746104d80a80adfd2167b63d6b175ab12 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 20:41:10 -0400 Subject: [PATCH 07/11] Move comments around --- build.js | 7 +++---- dist/birb.user.js | 2 +- manifest.json | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/build.js b/build.js index 3b95188..238ae8c 100644 --- a/build.js +++ b/build.js @@ -22,9 +22,9 @@ const STYLESHEET_KEY = "___STYLESHEET___"; const now = new Date(); const versionDate = `${now.getFullYear()}.${now.getMonth() + 1}.${now.getDate()}`; -let buildNumber = 1; -// Get build number from manifest.json +// Get current build number from manifest.json +let buildNumber = 0; try { const manifest = JSON.parse(readFileSync('manifest.json', 'utf8')); if (manifest.version) { @@ -41,9 +41,8 @@ try { throw e; } -const version = `${versionDate}.${buildNumber}`; - // Update manifest.json with new version +const version = `${versionDate}.${buildNumber}`; try { const manifest = JSON.parse(readFileSync('manifest.json', 'utf8')); manifest.version = version; diff --git a/dist/birb.user.js b/dist/birb.user.js index 7eb5a87..7a51b2f 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.10.25.1 +// @version 2025.10.25.8 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js diff --git a/manifest.json b/manifest.json index 4383718..c9ea6e4 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.10.25.1", + "version": "2025.10.25.8", "homepage_url": "https://idreesinc.com", "content_scripts": [ { From 7f9c3888539649fb6e1e2cc78be619b28f2eb56f Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 22:55:55 -0400 Subject: [PATCH 08/11] Allow clicking after short delay following petting --- birb.js | 2 +- dist/birb.js | 2 +- dist/birb.user.js | 4 ++-- manifest.json | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/birb.js b/birb.js index 831bf0a..4f1e789 100644 --- a/birb.js +++ b/birb.js @@ -943,7 +943,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { // Currently being pet, don't open menu return; } diff --git a/dist/birb.js b/dist/birb.js index f38dca4..c5cb424 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -1286,7 +1286,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { // Currently being pet, don't open menu return; } diff --git a/dist/birb.user.js b/dist/birb.user.js index 7a51b2f..9ce4980 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.10.25.8 +// @version 2025.10.25.15 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js @@ -1300,7 +1300,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { // Currently being pet, don't open menu return; } diff --git a/manifest.json b/manifest.json index c9ea6e4..b611f46 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.10.25.8", + "version": "2025.10.25.15", "homepage_url": "https://idreesinc.com", "content_scripts": [ { From b9304fe11b8dea7935f5f6bd5f426a32c682d229 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 23:46:50 -0400 Subject: [PATCH 09/11] Clean up code --- birb.js | 278 ++++++++++++++++++++++++++------------------- dist/birb.js | 278 ++++++++++++++++++++++++++------------------- dist/birb.user.js | 280 +++++++++++++++++++++++++++------------------- manifest.json | 2 +- 4 files changed, 485 insertions(+), 353 deletions(-) diff --git a/birb.js b/birb.js index 4f1e789..795f1cb 100644 --- a/birb.js +++ b/birb.js @@ -1,6 +1,5 @@ // @ts-check -// @ts-ignore const SHARED_CONFIG = { birbCssScale: 1, uiCssScale: 1, @@ -9,7 +8,6 @@ const SHARED_CONFIG = { hopDistance: 45, }; - const DESKTOP_CONFIG = { flySpeed: 0.25 }; @@ -28,17 +26,6 @@ const BIRB_CSS_SCALE = CONFIG.birbCssScale; const UI_CSS_SCALE = CONFIG.uiCssScale; const CANVAS_PIXEL_SIZE = CONFIG.canvasPixelSize; const WINDOW_PIXEL_SIZE = CANVAS_PIXEL_SIZE * BIRB_CSS_SCALE; -const HOP_SPEED = CONFIG.hopSpeed; -const FLY_SPEED = CONFIG.flySpeed; -const HOP_DISTANCE = CONFIG.hopDistance; -// Time in milliseconds until the user is considered AFK -const AFK_TIME = debugMode ? 0 : 1000 * 30; -const SPRITE_HEIGHT = 32; -const MENU_ID = "birb-menu"; -const MENU_EXIT_ID = "birb-menu-exit"; -const FIELD_GUIDE_ID = "birb-field-guide"; -const FEATHER_ID = "birb-feather"; - const DEFAULT_SETTINGS = { birbMode: false @@ -48,6 +35,23 @@ const DEFAULT_SETTINGS = { * @typedef {typeof DEFAULT_SETTINGS} Settings */ +/** + * @typedef {Object} SavedStickyNote + * @property {string} id + * @property {string} site + * @property {string} content + * @property {number} top + * @property {number} left + */ + +/** + * @typedef {Object} BirbSaveData + * @property {string[]} unlockedSpecies + * @property {string} currentSpecies + * @property {Partial} settings + * @property {SavedStickyNote[]} [stickyNotes] + */ + /** @type {Partial} */ let userSettings = {}; @@ -380,6 +384,7 @@ const Directions = { }; const SPRITE_WIDTH = 32; +const SPRITE_HEIGHT = 32; const DECORATIONS_SPRITE_WIDTH = 48; const FEATHER_SPRITE_WIDTH = 32; @@ -387,6 +392,34 @@ const SPRITE_SHEET = "__SPRITE_SHEET__"; const DECORATIONS_SPRITE_SHEET = "__DECORATIONS_SPRITE_SHEET__"; const FEATHER_SPRITE_SHEET = "__FEATHER_SPRITE_SHEET__"; +const MENU_ID = "birb-menu"; +const MENU_EXIT_ID = "birb-menu-exit"; +const FIELD_GUIDE_ID = "birb-field-guide"; +const FEATHER_ID = "birb-feather"; + +const HOP_SPEED = CONFIG.hopSpeed; +const FLY_SPEED = CONFIG.flySpeed; +const HOP_DISTANCE = CONFIG.hopDistance; +/** Speed at which the feather falls per tick */ +const FEATHER_FALL_SPEED = 1; +/** Time in milliseconds until the user is considered AFK */ +const AFK_TIME = debugMode ? 0 : 1000 * 30; +const UPDATE_INTERVAL = 1000 / 60; // 60 FPS +// Per-frame chances +const HOP_CHANCE = 1 / (60 * 3); // 3 seconds +const FOCUS_SWITCH_CHANCE = 1 / (60 * 20); // 20 seconds +const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 2 hours +/** Multiplier after petting that increases the feather drop chance */ +const PET_FEATHER_BOOST = 2; +/** How long the pet boost lasts in milliseconds */ +const PET_BOOST_DURATION = 1000 * 60 * 5; +const MIN_FOCUS_ELEMENT_WIDTH = 100; +const MIN_FOCUS_ELEMENT_TOP = 80; +/** Time between checking whether the URL has changed */ +const URL_CHECK_INTERVAL = 500; +/** Time after petting before the menu can be opened */ +const PET_MENU_COOLDOWN = 1000; + /** * Load the sprite sheet and return the pixel-map template * @param {string} dataUri @@ -445,7 +478,7 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-ignore +// @ts-expect-error 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; @@ -631,7 +664,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const canvas = document.createElement("canvas"); /** @type {CanvasRenderingContext2D} */ - // @ts-ignore + // @ts-expect-error const ctx = canvas.getContext("2d"); const States = { @@ -672,7 +705,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI * @returns {boolean} Whether the script is running in a userscript extension context */ function isUserScript() { - // @ts-ignore + // @ts-expect-error return typeof GM_getValue === "function"; } @@ -685,9 +718,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function load() { /** @type {Record} */ let saveData = {}; + if (isUserScript()) { log("Loading save data from UserScript storage"); - // @ts-ignore + // @ts-expect-error saveData = GM_getValue("birbSaveData", {}) ?? {}; } else if (isTestEnvironment()) { log("Test environment detected, loading save data from localStorage"); @@ -695,14 +729,18 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } else { log("Not a UserScript"); } + debug("Loaded data: " + JSON.stringify(saveData)); + if (!saveData.settings) { log("No user settings found in save data, starting fresh"); } + userSettings = saveData.settings ?? {}; unlockedSpecies = saveData.unlockedSpecies ?? [DEFAULT_BIRD]; currentSpecies = saveData.currentSpecies ?? DEFAULT_BIRD; stickyNotes = []; + if (saveData.stickyNotes) { for (let note of saveData.stickyNotes) { if (note.id) { @@ -710,17 +748,19 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } } + log(stickyNotes.length + " sticky notes loaded"); switchSpecies(currentSpecies); } function save() { - /** @type {Record} */ - let saveData = { - unlockedSpecies: unlockedSpecies, - currentSpecies: currentSpecies, + /** @type {BirbSaveData} */ + const saveData = { + unlockedSpecies, + currentSpecies, settings: userSettings }; + if (stickyNotes.length > 0) { saveData.stickyNotes = stickyNotes.map(note => ({ id: note.id, @@ -730,9 +770,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI left: note.left })); } + if (isUserScript()) { log("Saving data to UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_setValue("birbSaveData", saveData); } else if (isTestEnvironment()) { log("Test environment detected, saving data to localStorage"); @@ -745,7 +786,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function resetSaveData() { if (isUserScript()) { log("Resetting save data in UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_deleteValue("birbSaveData"); } else if (isTestEnvironment()) { log("Test environment detected, resetting save data in localStorage"); @@ -871,17 +912,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return false; } - /** @type {Record} */ - const stickyNoteParams = stickyNoteUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); - - /** @type {Record} */ - const currentParams = currentUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); + const stickyNoteParams = parseUrlParams(stickyNoteUrl); + const currentParams = parseUrlParams(currentUrl); debug("Comparing params: ", stickyNoteParams, currentParams); @@ -943,7 +975,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { // Currently being pet, don't open menu return; } @@ -980,9 +1012,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastUrl = currentUrl; drawStickyNotes(); } - }, 500); + }, URL_CHECK_INTERVAL); - setInterval(update, 1000 / 60); + setInterval(update, UPDATE_INTERVAL); } function drawStickyNotes() { @@ -1007,7 +1039,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < 1 / (60 * 3) && currentAnimation !== Animations.HEART) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { hop(); } else if (Date.now() - lastActionTimestamp > AFK_TIME) { // Idle for a while, do something @@ -1015,7 +1047,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI // Fly to an element focusOnElement(); lastActionTimestamp = Date.now(); - } else if (Math.random() < 1 / (60 * 20)) { + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { // Fly to another element if idle for a longer while focusOnElement(); lastActionTimestamp = Date.now(); @@ -1026,9 +1058,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI setState(States.IDLE); } } - const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 1 every 2 hours (ticks * seconds * minutes * hours) + // Double the chance of a feather if recently pet - let petMod = Date.now() - lastPetTimestamp < 1000 * 60 * 5 ? 2 : 1; + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; if (visible && Math.random() < FEATHER_CHANCE * petMod) { lastPetTimestamp = 0; activateFeather(); @@ -1099,6 +1131,42 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return element; } + /** + * Create a window element with header and content + * @param {string} id + * @param {string} title + * @param {string} contentHtml + * @param {() => void} [onClose] + * @returns {HTMLElement} + */ + function createWindow(id, title, contentHtml, onClose) { + const window = makeElement("birb-window", undefined, id); + window.innerHTML = ` +
+
${title}
+
x
+
+
+ ${contentHtml} +
+ `; + + document.body.appendChild(window); + makeDraggable(window.querySelector(".birb-window-header")); + + const closeButton = window.querySelector(".birb-window-close"); + if (closeButton) { + makeClosable(() => { + if (onClose) { + onClose(); + } + window.remove(); + }, closeButton); + } + + return window; + } + function insertDecoration() { // Create a canvas element for the decoration const decorationCanvas = document.createElement("canvas"); @@ -1178,21 +1246,16 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function updateFeather() { const feather = document.querySelector("#birb-feather"); - const featherGravity = 1; if (!feather || !(feather instanceof HTMLElement)) { return; } - const y = parseInt(feather.style.top || "0") + featherGravity; + const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; if (y < window.innerHeight - feather.offsetHeight) { feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; } } - - // insertDecoration(); - // insertFieldGuide(); - /** * @param {HTMLElement} element */ @@ -1209,28 +1272,14 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI if (document.querySelector("#" + FIELD_GUIDE_ID)) { return; } - let html = ` -
-
${title}
-
x
-
-
-
- ${message} -
-
` - const modal = makeElement("birb-window"); - modal.style.width = "270px"; - modal.innerHTML = html; - document.body.appendChild(modal); - makeDraggable(modal.querySelector(".birb-window-header")); - const closeButton = modal.querySelector(".birb-window-close"); - if (closeButton) { - makeClosable(() => { - modal.remove(); - }, closeButton); - } + const modal = createWindow("birb-modal", title, ` +
+ ${message} +
+ `); + + modal.style.width = "270px"; centerElement(modal); } @@ -1647,12 +1696,11 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const elements = document.querySelectorAll("img, video"); const inWindow = Array.from(elements).filter((img) => { const rect = img.getBoundingClientRect(); - return rect.left >= 0 && rect.top >= 80 && rect.right <= window.innerWidth && rect.top <= window.innerHeight; + return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; }); - const MIN_WIDTH = 100; /** @type {HTMLElement[]} */ - // @ts-ignore - const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_WIDTH); + // @ts-expect-error + const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_FOCUS_ELEMENT_WIDTH); if (largeElements.length === 0) { return; } @@ -1669,12 +1717,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; return; } - const rect = focusedElement.getBoundingClientRect(); - focusedBounds = { - left: rect.left, - right: rect.right, - top: rect.top - }; + const { left, right, top } = focusedElement.getBoundingClientRect(); + focusedBounds = { left, right, top }; } function getCanvasWidth() { @@ -1782,49 +1826,49 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } + + // Helper functions + + /** + * @param {number} startX + * @param {number} startY + * @param {number} endX + * @param {number} endY + * @param {number} amount + * @param {number} [intensity] + * @returns {{x: number, y: number}} + */ + function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { + const dx = endX - startX; + const dy = endY - startY; + const distance = Math.sqrt(dx * dx + dy * dy); + const angle = Math.atan2(dy, dx); + const midX = startX + Math.cos(angle) * distance / 2; + const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; + const t = amount; + const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; + const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; + return { x, y }; + } + + /** + * Parse URL parameters into a key-value map + * @param {string} url + * @returns {Record} + */ + function parseUrlParams(url) { + const queryString = url.split("?")[1]; + if (!queryString) return {}; + + return queryString.split("&").reduce((params, param) => { + const [key, value] = param.split("="); + return { ...params, [key]: value }; + }, {}); + } }).catch((e) => { error("Error while loading sprite sheets: ", e); }); -/** - * @param {number} start - * @param {number} end - * @param {number} amount - * @returns {number} - */ -function linearLerp(start, end, amount) { - return start + (end - start) * amount; -} - -/** - * @param {number} startX - * @param {number} startY - * @param {number} endX - * @param {number} endY - * @param {number} amount - * @param {number} [intensity] - * @returns {{x: number, y: number}} - */ -function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { - const dx = endX - startX; - const dy = endY - startY; - const distance = Math.sqrt(dx * dx + dy * dy); - const angle = Math.atan2(dy, dx); - const midX = startX + Math.cos(angle) * distance / 2; - const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; - const t = amount; - const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; - const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; - return { x, y }; -} - -/** - * @param {number} value - */ -function roundToPixel(value) { - return Math.round(value / WINDOW_PIXEL_SIZE) * WINDOW_PIXEL_SIZE; -} - /** * @returns {boolean} Whether the user is on a mobile device */ diff --git a/dist/birb.js b/dist/birb.js index c5cb424..d525656 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -1,6 +1,5 @@ // @ts-check -// @ts-ignore const SHARED_CONFIG = { birbCssScale: 1, uiCssScale: 1, @@ -9,7 +8,6 @@ const SHARED_CONFIG = { hopDistance: 45, }; - const DESKTOP_CONFIG = { flySpeed: 0.25 }; @@ -28,17 +26,6 @@ const BIRB_CSS_SCALE = CONFIG.birbCssScale; const UI_CSS_SCALE = CONFIG.uiCssScale; const CANVAS_PIXEL_SIZE = CONFIG.canvasPixelSize; const WINDOW_PIXEL_SIZE = CANVAS_PIXEL_SIZE * BIRB_CSS_SCALE; -const HOP_SPEED = CONFIG.hopSpeed; -const FLY_SPEED = CONFIG.flySpeed; -const HOP_DISTANCE = CONFIG.hopDistance; -// Time in milliseconds until the user is considered AFK -const AFK_TIME = debugMode ? 0 : 1000 * 30; -const SPRITE_HEIGHT = 32; -const MENU_ID = "birb-menu"; -const MENU_EXIT_ID = "birb-menu-exit"; -const FIELD_GUIDE_ID = "birb-field-guide"; -const FEATHER_ID = "birb-feather"; - const DEFAULT_SETTINGS = { birbMode: false @@ -48,6 +35,23 @@ const DEFAULT_SETTINGS = { * @typedef {typeof DEFAULT_SETTINGS} Settings */ +/** + * @typedef {Object} SavedStickyNote + * @property {string} id + * @property {string} site + * @property {string} content + * @property {number} top + * @property {number} left + */ + +/** + * @typedef {Object} BirbSaveData + * @property {string[]} unlockedSpecies + * @property {string} currentSpecies + * @property {Partial} settings + * @property {SavedStickyNote[]} [stickyNotes] + */ + /** @type {Partial} */ let userSettings = {}; @@ -723,6 +727,7 @@ const Directions = { }; const SPRITE_WIDTH = 32; +const SPRITE_HEIGHT = 32; const DECORATIONS_SPRITE_WIDTH = 48; const FEATHER_SPRITE_WIDTH = 32; @@ -730,6 +735,34 @@ const SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAAgCAYA const DECORATIONS_SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAPNJREFUaIHtmTESgzAMBHWZDC+gp0vP/x9Bn44+L6BRmrhJA4csM05uGzfY1s1JxggzIYQQQgghxEnATnB3zwikAICKiXq4BE/uwaxvn/UPb3BnNwFg27Ky0w6vzRp8S4mkIbQD3wzzFJofdTMkYJgn89czFADGKSSiSgphfFBjTaoIKC4cHWvSxIFMmjiQSYoDLUlxoCVywOwHHWjpROop1IL/vsxty2oYO77M1QggSvcpJAFXE66BPfa+2C4v4j2yi7z7FJKAq6FrwN3TO3MMlAAAKO3F2sVZTiu2N9p9CnUv4FR7PbMG2BQ69SJL/kVA8QauAnHUj36BVwAAAABJRU5ErkJggg=="; const FEATHER_SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAARhJREFUWIXtlbENwjAQRf8hSiZIRQ+9WQNRUFIAKzACBSsAA1Ag1mAABqCCBomG3hQQ9OMEx4ZDNH5SikSJ3/fZ5wCJRCKRSPwZ0RzMWmtLAhGvQyUAi9mXP/aFaGjJRQQiguHihMvcFMJUVUYlAMuHixPGy4en1WmVQqgHYHkuZjiEj6a2/LjtYzTY0eiZbgC37Mxh1UN3sn/dr6cCz/LHB/DJj9s+2oMdbtdz6TtfFwQHcMvOInfmQNjsgchNWLXmdfK6gyioAu/6uKrsm1kWLAciKuCuey5nYuXAh234bdmZ6INIUw4E/Ix49xtjCmXfzLL8nY/ktdgnAKwxxgIoXIyqmAOwvIqfiN0ALNd21HYBO9XXGMAdnZTYyHWzWjQAAAAASUVORK5CYII="; +const MENU_ID = "birb-menu"; +const MENU_EXIT_ID = "birb-menu-exit"; +const FIELD_GUIDE_ID = "birb-field-guide"; +const FEATHER_ID = "birb-feather"; + +const HOP_SPEED = CONFIG.hopSpeed; +const FLY_SPEED = CONFIG.flySpeed; +const HOP_DISTANCE = CONFIG.hopDistance; +/** Speed at which the feather falls per tick */ +const FEATHER_FALL_SPEED = 1; +/** Time in milliseconds until the user is considered AFK */ +const AFK_TIME = debugMode ? 0 : 1000 * 30; +const UPDATE_INTERVAL = 1000 / 60; // 60 FPS +// Per-frame chances +const HOP_CHANCE = 1 / (60 * 3); // 3 seconds +const FOCUS_SWITCH_CHANCE = 1 / (60 * 20); // 20 seconds +const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 2 hours +/** Multiplier after petting that increases the feather drop chance */ +const PET_FEATHER_BOOST = 2; +/** How long the pet boost lasts in milliseconds */ +const PET_BOOST_DURATION = 1000 * 60 * 5; +const MIN_FOCUS_ELEMENT_WIDTH = 100; +const MIN_FOCUS_ELEMENT_TOP = 80; +/** Time between checking whether the URL has changed */ +const URL_CHECK_INTERVAL = 500; +/** Time after petting before the menu can be opened */ +const PET_MENU_COOLDOWN = 1000; + /** * Load the sprite sheet and return the pixel-map template * @param {string} dataUri @@ -788,7 +821,7 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-ignore +// @ts-expect-error 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; @@ -974,7 +1007,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const canvas = document.createElement("canvas"); /** @type {CanvasRenderingContext2D} */ - // @ts-ignore + // @ts-expect-error const ctx = canvas.getContext("2d"); const States = { @@ -1015,7 +1048,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI * @returns {boolean} Whether the script is running in a userscript extension context */ function isUserScript() { - // @ts-ignore + // @ts-expect-error return typeof GM_getValue === "function"; } @@ -1028,9 +1061,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function load() { /** @type {Record} */ let saveData = {}; + if (isUserScript()) { log("Loading save data from UserScript storage"); - // @ts-ignore + // @ts-expect-error saveData = GM_getValue("birbSaveData", {}) ?? {}; } else if (isTestEnvironment()) { log("Test environment detected, loading save data from localStorage"); @@ -1038,14 +1072,18 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } else { log("Not a UserScript"); } + debug("Loaded data: " + JSON.stringify(saveData)); + if (!saveData.settings) { log("No user settings found in save data, starting fresh"); } + userSettings = saveData.settings ?? {}; unlockedSpecies = saveData.unlockedSpecies ?? [DEFAULT_BIRD]; currentSpecies = saveData.currentSpecies ?? DEFAULT_BIRD; stickyNotes = []; + if (saveData.stickyNotes) { for (let note of saveData.stickyNotes) { if (note.id) { @@ -1053,17 +1091,19 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } } + log(stickyNotes.length + " sticky notes loaded"); switchSpecies(currentSpecies); } function save() { - /** @type {Record} */ - let saveData = { - unlockedSpecies: unlockedSpecies, - currentSpecies: currentSpecies, + /** @type {BirbSaveData} */ + const saveData = { + unlockedSpecies, + currentSpecies, settings: userSettings }; + if (stickyNotes.length > 0) { saveData.stickyNotes = stickyNotes.map(note => ({ id: note.id, @@ -1073,9 +1113,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI left: note.left })); } + if (isUserScript()) { log("Saving data to UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_setValue("birbSaveData", saveData); } else if (isTestEnvironment()) { log("Test environment detected, saving data to localStorage"); @@ -1088,7 +1129,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function resetSaveData() { if (isUserScript()) { log("Resetting save data in UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_deleteValue("birbSaveData"); } else if (isTestEnvironment()) { log("Test environment detected, resetting save data in localStorage"); @@ -1214,17 +1255,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return false; } - /** @type {Record} */ - const stickyNoteParams = stickyNoteUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); - - /** @type {Record} */ - const currentParams = currentUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); + const stickyNoteParams = parseUrlParams(stickyNoteUrl); + const currentParams = parseUrlParams(currentUrl); debug("Comparing params: ", stickyNoteParams, currentParams); @@ -1286,7 +1318,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { // Currently being pet, don't open menu return; } @@ -1323,9 +1355,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastUrl = currentUrl; drawStickyNotes(); } - }, 500); + }, URL_CHECK_INTERVAL); - setInterval(update, 1000 / 60); + setInterval(update, UPDATE_INTERVAL); } function drawStickyNotes() { @@ -1350,7 +1382,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < 1 / (60 * 3) && currentAnimation !== Animations.HEART) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { hop(); } else if (Date.now() - lastActionTimestamp > AFK_TIME) { // Idle for a while, do something @@ -1358,7 +1390,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI // Fly to an element focusOnElement(); lastActionTimestamp = Date.now(); - } else if (Math.random() < 1 / (60 * 20)) { + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { // Fly to another element if idle for a longer while focusOnElement(); lastActionTimestamp = Date.now(); @@ -1369,9 +1401,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI setState(States.IDLE); } } - const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 1 every 2 hours (ticks * seconds * minutes * hours) + // Double the chance of a feather if recently pet - let petMod = Date.now() - lastPetTimestamp < 1000 * 60 * 5 ? 2 : 1; + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; if (visible && Math.random() < FEATHER_CHANCE * petMod) { lastPetTimestamp = 0; activateFeather(); @@ -1442,6 +1474,42 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return element; } + /** + * Create a window element with header and content + * @param {string} id + * @param {string} title + * @param {string} contentHtml + * @param {() => void} [onClose] + * @returns {HTMLElement} + */ + function createWindow(id, title, contentHtml, onClose) { + const window = makeElement("birb-window", undefined, id); + window.innerHTML = ` +
+
${title}
+
x
+
+
+ ${contentHtml} +
+ `; + + document.body.appendChild(window); + makeDraggable(window.querySelector(".birb-window-header")); + + const closeButton = window.querySelector(".birb-window-close"); + if (closeButton) { + makeClosable(() => { + if (onClose) { + onClose(); + } + window.remove(); + }, closeButton); + } + + return window; + } + function insertDecoration() { // Create a canvas element for the decoration const decorationCanvas = document.createElement("canvas"); @@ -1521,21 +1589,16 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function updateFeather() { const feather = document.querySelector("#birb-feather"); - const featherGravity = 1; if (!feather || !(feather instanceof HTMLElement)) { return; } - const y = parseInt(feather.style.top || "0") + featherGravity; + const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; if (y < window.innerHeight - feather.offsetHeight) { feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; } } - - // insertDecoration(); - // insertFieldGuide(); - /** * @param {HTMLElement} element */ @@ -1552,28 +1615,14 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI if (document.querySelector("#" + FIELD_GUIDE_ID)) { return; } - let html = ` -
-
${title}
-
x
-
-
-
- ${message} -
-
` - const modal = makeElement("birb-window"); - modal.style.width = "270px"; - modal.innerHTML = html; - document.body.appendChild(modal); - makeDraggable(modal.querySelector(".birb-window-header")); - const closeButton = modal.querySelector(".birb-window-close"); - if (closeButton) { - makeClosable(() => { - modal.remove(); - }, closeButton); - } + const modal = createWindow("birb-modal", title, ` +
+ ${message} +
+ `); + + modal.style.width = "270px"; centerElement(modal); } @@ -1990,12 +2039,11 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const elements = document.querySelectorAll("img, video"); const inWindow = Array.from(elements).filter((img) => { const rect = img.getBoundingClientRect(); - return rect.left >= 0 && rect.top >= 80 && rect.right <= window.innerWidth && rect.top <= window.innerHeight; + return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; }); - const MIN_WIDTH = 100; /** @type {HTMLElement[]} */ - // @ts-ignore - const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_WIDTH); + // @ts-expect-error + const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_FOCUS_ELEMENT_WIDTH); if (largeElements.length === 0) { return; } @@ -2012,12 +2060,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; return; } - const rect = focusedElement.getBoundingClientRect(); - focusedBounds = { - left: rect.left, - right: rect.right, - top: rect.top - }; + const { left, right, top } = focusedElement.getBoundingClientRect(); + focusedBounds = { left, right, top }; } function getCanvasWidth() { @@ -2125,49 +2169,49 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } + + // Helper functions + + /** + * @param {number} startX + * @param {number} startY + * @param {number} endX + * @param {number} endY + * @param {number} amount + * @param {number} [intensity] + * @returns {{x: number, y: number}} + */ + function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { + const dx = endX - startX; + const dy = endY - startY; + const distance = Math.sqrt(dx * dx + dy * dy); + const angle = Math.atan2(dy, dx); + const midX = startX + Math.cos(angle) * distance / 2; + const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; + const t = amount; + const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; + const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; + return { x, y }; + } + + /** + * Parse URL parameters into a key-value map + * @param {string} url + * @returns {Record} + */ + function parseUrlParams(url) { + const queryString = url.split("?")[1]; + if (!queryString) return {}; + + return queryString.split("&").reduce((params, param) => { + const [key, value] = param.split("="); + return { ...params, [key]: value }; + }, {}); + } }).catch((e) => { error("Error while loading sprite sheets: ", e); }); -/** - * @param {number} start - * @param {number} end - * @param {number} amount - * @returns {number} - */ -function linearLerp(start, end, amount) { - return start + (end - start) * amount; -} - -/** - * @param {number} startX - * @param {number} startY - * @param {number} endX - * @param {number} endY - * @param {number} amount - * @param {number} [intensity] - * @returns {{x: number, y: number}} - */ -function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { - const dx = endX - startX; - const dy = endY - startY; - const distance = Math.sqrt(dx * dx + dy * dy); - const angle = Math.atan2(dy, dx); - const midX = startX + Math.cos(angle) * distance / 2; - const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; - const t = amount; - const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; - const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; - return { x, y }; -} - -/** - * @param {number} value - */ -function roundToPixel(value) { - return Math.round(value / WINDOW_PIXEL_SIZE) * WINDOW_PIXEL_SIZE; -} - /** * @returns {boolean} Whether the user is on a mobile device */ diff --git a/dist/birb.user.js b/dist/birb.user.js index 9ce4980..ab361a5 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.10.25.15 +// @version 2025.10.25.117 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js @@ -14,7 +14,6 @@ // @ts-check -// @ts-ignore const SHARED_CONFIG = { birbCssScale: 1, uiCssScale: 1, @@ -23,7 +22,6 @@ const SHARED_CONFIG = { hopDistance: 45, }; - const DESKTOP_CONFIG = { flySpeed: 0.25 }; @@ -42,17 +40,6 @@ const BIRB_CSS_SCALE = CONFIG.birbCssScale; const UI_CSS_SCALE = CONFIG.uiCssScale; const CANVAS_PIXEL_SIZE = CONFIG.canvasPixelSize; const WINDOW_PIXEL_SIZE = CANVAS_PIXEL_SIZE * BIRB_CSS_SCALE; -const HOP_SPEED = CONFIG.hopSpeed; -const FLY_SPEED = CONFIG.flySpeed; -const HOP_DISTANCE = CONFIG.hopDistance; -// Time in milliseconds until the user is considered AFK -const AFK_TIME = debugMode ? 0 : 1000 * 30; -const SPRITE_HEIGHT = 32; -const MENU_ID = "birb-menu"; -const MENU_EXIT_ID = "birb-menu-exit"; -const FIELD_GUIDE_ID = "birb-field-guide"; -const FEATHER_ID = "birb-feather"; - const DEFAULT_SETTINGS = { birbMode: false @@ -62,6 +49,23 @@ const DEFAULT_SETTINGS = { * @typedef {typeof DEFAULT_SETTINGS} Settings */ +/** + * @typedef {Object} SavedStickyNote + * @property {string} id + * @property {string} site + * @property {string} content + * @property {number} top + * @property {number} left + */ + +/** + * @typedef {Object} BirbSaveData + * @property {string[]} unlockedSpecies + * @property {string} currentSpecies + * @property {Partial} settings + * @property {SavedStickyNote[]} [stickyNotes] + */ + /** @type {Partial} */ let userSettings = {}; @@ -737,6 +741,7 @@ const Directions = { }; const SPRITE_WIDTH = 32; +const SPRITE_HEIGHT = 32; const DECORATIONS_SPRITE_WIDTH = 48; const FEATHER_SPRITE_WIDTH = 32; @@ -744,6 +749,34 @@ const SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAAgCAYA const DECORATIONS_SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAPNJREFUaIHtmTESgzAMBHWZDC+gp0vP/x9Bn44+L6BRmrhJA4csM05uGzfY1s1JxggzIYQQQgghxEnATnB3zwikAICKiXq4BE/uwaxvn/UPb3BnNwFg27Ky0w6vzRp8S4mkIbQD3wzzFJofdTMkYJgn89czFADGKSSiSgphfFBjTaoIKC4cHWvSxIFMmjiQSYoDLUlxoCVywOwHHWjpROop1IL/vsxty2oYO77M1QggSvcpJAFXE66BPfa+2C4v4j2yi7z7FJKAq6FrwN3TO3MMlAAAKO3F2sVZTiu2N9p9CnUv4FR7PbMG2BQ69SJL/kVA8QauAnHUj36BVwAAAABJRU5ErkJggg=="; const FEATHER_SPRITE_SHEET = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAARhJREFUWIXtlbENwjAQRf8hSiZIRQ+9WQNRUFIAKzACBSsAA1Ag1mAABqCCBomG3hQQ9OMEx4ZDNH5SikSJ3/fZ5wCJRCKRSPwZ0RzMWmtLAhGvQyUAi9mXP/aFaGjJRQQiguHihMvcFMJUVUYlAMuHixPGy4en1WmVQqgHYHkuZjiEj6a2/LjtYzTY0eiZbgC37Mxh1UN3sn/dr6cCz/LHB/DJj9s+2oMdbtdz6TtfFwQHcMvOInfmQNjsgchNWLXmdfK6gyioAu/6uKrsm1kWLAciKuCuey5nYuXAh234bdmZ6INIUw4E/Ix49xtjCmXfzLL8nY/ktdgnAKwxxgIoXIyqmAOwvIqfiN0ALNd21HYBO9XXGMAdnZTYyHWzWjQAAAAASUVORK5CYII="; +const MENU_ID = "birb-menu"; +const MENU_EXIT_ID = "birb-menu-exit"; +const FIELD_GUIDE_ID = "birb-field-guide"; +const FEATHER_ID = "birb-feather"; + +const HOP_SPEED = CONFIG.hopSpeed; +const FLY_SPEED = CONFIG.flySpeed; +const HOP_DISTANCE = CONFIG.hopDistance; +/** Speed at which the feather falls per tick */ +const FEATHER_FALL_SPEED = 1; +/** Time in milliseconds until the user is considered AFK */ +const AFK_TIME = debugMode ? 0 : 1000 * 30; +const UPDATE_INTERVAL = 1000 / 60; // 60 FPS +// Per-frame chances +const HOP_CHANCE = 1 / (60 * 3); // 3 seconds +const FOCUS_SWITCH_CHANCE = 1 / (60 * 20); // 20 seconds +const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 2 hours +/** Multiplier after petting that increases the feather drop chance */ +const PET_FEATHER_BOOST = 2; +/** How long the pet boost lasts in milliseconds */ +const PET_BOOST_DURATION = 1000 * 60 * 5; +const MIN_FOCUS_ELEMENT_WIDTH = 100; +const MIN_FOCUS_ELEMENT_TOP = 80; +/** Time between checking whether the URL has changed */ +const URL_CHECK_INTERVAL = 500; +/** Time after petting before the menu can be opened */ +const PET_MENU_COOLDOWN = 1000; + /** * Load the sprite sheet and return the pixel-map template * @param {string} dataUri @@ -802,7 +835,7 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-ignore +// @ts-expect-error 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; @@ -988,7 +1021,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const canvas = document.createElement("canvas"); /** @type {CanvasRenderingContext2D} */ - // @ts-ignore + // @ts-expect-error const ctx = canvas.getContext("2d"); const States = { @@ -1029,7 +1062,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI * @returns {boolean} Whether the script is running in a userscript extension context */ function isUserScript() { - // @ts-ignore + // @ts-expect-error return typeof GM_getValue === "function"; } @@ -1042,9 +1075,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function load() { /** @type {Record} */ let saveData = {}; + if (isUserScript()) { log("Loading save data from UserScript storage"); - // @ts-ignore + // @ts-expect-error saveData = GM_getValue("birbSaveData", {}) ?? {}; } else if (isTestEnvironment()) { log("Test environment detected, loading save data from localStorage"); @@ -1052,14 +1086,18 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } else { log("Not a UserScript"); } + debug("Loaded data: " + JSON.stringify(saveData)); + if (!saveData.settings) { log("No user settings found in save data, starting fresh"); } + userSettings = saveData.settings ?? {}; unlockedSpecies = saveData.unlockedSpecies ?? [DEFAULT_BIRD]; currentSpecies = saveData.currentSpecies ?? DEFAULT_BIRD; stickyNotes = []; + if (saveData.stickyNotes) { for (let note of saveData.stickyNotes) { if (note.id) { @@ -1067,17 +1105,19 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } } + log(stickyNotes.length + " sticky notes loaded"); switchSpecies(currentSpecies); } function save() { - /** @type {Record} */ - let saveData = { - unlockedSpecies: unlockedSpecies, - currentSpecies: currentSpecies, + /** @type {BirbSaveData} */ + const saveData = { + unlockedSpecies, + currentSpecies, settings: userSettings }; + if (stickyNotes.length > 0) { saveData.stickyNotes = stickyNotes.map(note => ({ id: note.id, @@ -1087,9 +1127,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI left: note.left })); } + if (isUserScript()) { log("Saving data to UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_setValue("birbSaveData", saveData); } else if (isTestEnvironment()) { log("Test environment detected, saving data to localStorage"); @@ -1102,7 +1143,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function resetSaveData() { if (isUserScript()) { log("Resetting save data in UserScript storage"); - // @ts-ignore + // @ts-expect-error GM_deleteValue("birbSaveData"); } else if (isTestEnvironment()) { log("Test environment detected, resetting save data in localStorage"); @@ -1228,17 +1269,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return false; } - /** @type {Record} */ - const stickyNoteParams = stickyNoteUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); - - /** @type {Record} */ - const currentParams = currentUrl.split("?")[1]?.split("&").reduce((params, param) => { - const [key, value] = param.split("="); - return { ...params, [key]: value }; - }, {}); + const stickyNoteParams = parseUrlParams(stickyNoteUrl); + const currentParams = parseUrlParams(currentUrl); debug("Comparing params: ", stickyNoteParams, currentParams); @@ -1300,7 +1332,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI }); onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < 1000)) { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { // Currently being pet, don't open menu return; } @@ -1337,9 +1369,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI lastUrl = currentUrl; drawStickyNotes(); } - }, 500); + }, URL_CHECK_INTERVAL); - setInterval(update, 1000 / 60); + setInterval(update, UPDATE_INTERVAL); } function drawStickyNotes() { @@ -1364,7 +1396,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < 1 / (60 * 3) && currentAnimation !== Animations.HEART) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { hop(); } else if (Date.now() - lastActionTimestamp > AFK_TIME) { // Idle for a while, do something @@ -1372,7 +1404,7 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI // Fly to an element focusOnElement(); lastActionTimestamp = Date.now(); - } else if (Math.random() < 1 / (60 * 20)) { + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { // Fly to another element if idle for a longer while focusOnElement(); lastActionTimestamp = Date.now(); @@ -1383,9 +1415,9 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI setState(States.IDLE); } } - const FEATHER_CHANCE = 1 / (60 * 60 * 60 * 2); // 1 every 2 hours (ticks * seconds * minutes * hours) + // Double the chance of a feather if recently pet - let petMod = Date.now() - lastPetTimestamp < 1000 * 60 * 5 ? 2 : 1; + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; if (visible && Math.random() < FEATHER_CHANCE * petMod) { lastPetTimestamp = 0; activateFeather(); @@ -1456,6 +1488,42 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return element; } + /** + * Create a window element with header and content + * @param {string} id + * @param {string} title + * @param {string} contentHtml + * @param {() => void} [onClose] + * @returns {HTMLElement} + */ + function createWindow(id, title, contentHtml, onClose) { + const window = makeElement("birb-window", undefined, id); + window.innerHTML = ` +
+
${title}
+
x
+
+
+ ${contentHtml} +
+ `; + + document.body.appendChild(window); + makeDraggable(window.querySelector(".birb-window-header")); + + const closeButton = window.querySelector(".birb-window-close"); + if (closeButton) { + makeClosable(() => { + if (onClose) { + onClose(); + } + window.remove(); + }, closeButton); + } + + return window; + } + function insertDecoration() { // Create a canvas element for the decoration const decorationCanvas = document.createElement("canvas"); @@ -1535,21 +1603,16 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI function updateFeather() { const feather = document.querySelector("#birb-feather"); - const featherGravity = 1; if (!feather || !(feather instanceof HTMLElement)) { return; } - const y = parseInt(feather.style.top || "0") + featherGravity; + const y = parseInt(feather.style.top || "0") + FEATHER_FALL_SPEED; feather.style.top = `${Math.min(y, window.innerHeight - feather.offsetHeight)}px`; if (y < window.innerHeight - feather.offsetHeight) { feather.style.left = `${Math.sin(3.14 * 2 * (ticks / 120)) * 25}px`; } } - - // insertDecoration(); - // insertFieldGuide(); - /** * @param {HTMLElement} element */ @@ -1566,28 +1629,14 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI if (document.querySelector("#" + FIELD_GUIDE_ID)) { return; } - let html = ` -
-
${title}
-
x
-
-
-
- ${message} -
-
` - const modal = makeElement("birb-window"); - modal.style.width = "270px"; - modal.innerHTML = html; - document.body.appendChild(modal); - makeDraggable(modal.querySelector(".birb-window-header")); - const closeButton = modal.querySelector(".birb-window-close"); - if (closeButton) { - makeClosable(() => { - modal.remove(); - }, closeButton); - } + const modal = createWindow("birb-modal", title, ` +
+ ${message} +
+ `); + + modal.style.width = "270px"; centerElement(modal); } @@ -2004,12 +2053,11 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI const elements = document.querySelectorAll("img, video"); const inWindow = Array.from(elements).filter((img) => { const rect = img.getBoundingClientRect(); - return rect.left >= 0 && rect.top >= 80 && rect.right <= window.innerWidth && rect.top <= window.innerHeight; + return rect.left >= 0 && rect.top >= MIN_FOCUS_ELEMENT_TOP && rect.right <= window.innerWidth && rect.top <= window.innerHeight; }); - const MIN_WIDTH = 100; /** @type {HTMLElement[]} */ - // @ts-ignore - const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_WIDTH); + // @ts-expect-error + const largeElements = Array.from(inWindow).filter((img) => img instanceof HTMLElement && img !== focusedElement && img.offsetWidth >= MIN_FOCUS_ELEMENT_WIDTH); if (largeElements.length === 0) { return; } @@ -2026,12 +2074,8 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI focusedBounds = { left: 0, right: window.innerWidth, top: getFullWindowHeight() }; return; } - const rect = focusedElement.getBoundingClientRect(); - focusedBounds = { - left: rect.left, - right: rect.right, - top: rect.top - }; + const { left, right, top } = focusedElement.getBoundingClientRect(); + focusedBounds = { left, right, top }; } function getCanvasWidth() { @@ -2139,49 +2183,49 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } canvas.style.bottom = `${bottom}px`; } + + // Helper functions + + /** + * @param {number} startX + * @param {number} startY + * @param {number} endX + * @param {number} endY + * @param {number} amount + * @param {number} [intensity] + * @returns {{x: number, y: number}} + */ + function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { + const dx = endX - startX; + const dy = endY - startY; + const distance = Math.sqrt(dx * dx + dy * dy); + const angle = Math.atan2(dy, dx); + const midX = startX + Math.cos(angle) * distance / 2; + const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; + const t = amount; + const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; + const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; + return { x, y }; + } + + /** + * Parse URL parameters into a key-value map + * @param {string} url + * @returns {Record} + */ + function parseUrlParams(url) { + const queryString = url.split("?")[1]; + if (!queryString) return {}; + + return queryString.split("&").reduce((params, param) => { + const [key, value] = param.split("="); + return { ...params, [key]: value }; + }, {}); + } }).catch((e) => { error("Error while loading sprite sheets: ", e); }); -/** - * @param {number} start - * @param {number} end - * @param {number} amount - * @returns {number} - */ -function linearLerp(start, end, amount) { - return start + (end - start) * amount; -} - -/** - * @param {number} startX - * @param {number} startY - * @param {number} endX - * @param {number} endY - * @param {number} amount - * @param {number} [intensity] - * @returns {{x: number, y: number}} - */ -function parabolicLerp(startX, startY, endX, endY, amount, intensity = 1.2) { - const dx = endX - startX; - const dy = endY - startY; - const distance = Math.sqrt(dx * dx + dy * dy); - const angle = Math.atan2(dy, dx); - const midX = startX + Math.cos(angle) * distance / 2; - const midY = startY + Math.sin(angle) * distance / 2 + distance / 4 * intensity; - const t = amount; - const x = (1 - t) ** 2 * startX + 2 * (1 - t) * t * midX + t ** 2 * endX; - const y = (1 - t) ** 2 * startY + 2 * (1 - t) * t * midY + t ** 2 * endY; - return { x, y }; -} - -/** - * @param {number} value - */ -function roundToPixel(value) { - return Math.round(value / WINDOW_PIXEL_SIZE) * WINDOW_PIXEL_SIZE; -} - /** * @returns {boolean} Whether the user is on a mobile device */ diff --git a/manifest.json b/manifest.json index b611f46..37f1cf4 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.10.25.15", + "version": "2025.10.25.117", "homepage_url": "https://idreesinc.com", "content_scripts": [ { From 6124fcd96988fe800574aed0c46f8c3b7cd5a8c4 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sat, 25 Oct 2025 23:49:55 -0400 Subject: [PATCH 10/11] Group sticky note functions together --- birb.js | 351 ++++++++++++++++++++++----------------------- dist/birb.js | 351 ++++++++++++++++++++++----------------------- dist/birb.user.js | 353 +++++++++++++++++++++++----------------------- manifest.json | 2 +- 4 files changed, 530 insertions(+), 527 deletions(-) diff --git a/birb.js b/birb.js index 795f1cb..72a8564 100644 --- a/birb.js +++ b/birb.js @@ -812,6 +812,178 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return settings().birbMode ? "Birb" : "Bird"; } + function init() { + if (window !== window.top) { + // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); + return; + } + log("Sprite sheets loaded successfully, initializing bird..."); + + // Preload font + const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; + const fontLink = document.createElement("link"); + fontLink.rel = "stylesheet"; + fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; + document.head.appendChild(fontLink); + + // Add stylesheet font-face + const fontFace = ` + @font-face { + font-family: 'Monocraft'; + src: url(${MONOCRAFT_SRC}) format('opentype'); + font-weight: normal; + font-style: normal; + } + `; + const fontStyle = document.createElement("style"); + fontStyle.innerHTML = fontFace; + document.head.appendChild(fontStyle); + + load(); + + styleElement.innerHTML = STYLESHEET; + document.head.appendChild(styleElement); + + canvas.id = "birb"; + canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; + canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; + document.body.appendChild(canvas); + + window.addEventListener("scroll", () => { + lastActionTimestamp = Date.now(); + }); + + onClick(document, (e) => { + lastActionTimestamp = Date.now(); + if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { + removeMenu(); + } + }); + + onClick(canvas, () => { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { + // Currently being pet, don't open menu + return; + } + insertMenu(); + }); + + canvas.addEventListener("mouseover", () => { + lastActionTimestamp = Date.now(); + if (currentState === States.IDLE) { + petStack.push(Date.now()); + if (petStack.length > 10) { + petStack.shift(); + } + const pets = petStack.filter((time) => Date.now() - time < 1000).length; + if (pets >= 3) { + pet(); + // Clear the stack + petStack = []; + } + } + }); + + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + + drawStickyNotes(); + + let lastUrl = (window.location.href ?? "").split("?")[0]; + setInterval(() => { + const currentUrl = (window.location.href ?? "").split("?")[0]; + if (currentUrl !== lastUrl) { + log("URL changed, updating sticky notes"); + lastUrl = currentUrl; + drawStickyNotes(); + } + }, URL_CHECK_INTERVAL); + + setInterval(update, UPDATE_INTERVAL); + } + + function update() { + ticks++; + + // Hide bird if the browser is fullscreen + if (document.fullscreenElement) { + hideBirb(); + // Won't be restored on fullscreen exit + } + + if (currentState === States.IDLE && !frozen && !isMenuOpen()) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { + hop(); + } else if (Date.now() - lastActionTimestamp > AFK_TIME) { + // Idle for a while, do something + if (focusedElement === null) { + // Fly to an element + focusOnElement(); + lastActionTimestamp = Date.now(); + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { + // Fly to another element if idle for a longer while + focusOnElement(); + lastActionTimestamp = Date.now(); + } + } + } else if (currentState === States.HOP) { + if (updateParabolicPath(HOP_SPEED)) { + setState(States.IDLE); + } + } + + // Double the chance of a feather if recently pet + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; + if (visible && Math.random() < FEATHER_CHANCE * petMod) { + lastPetTimestamp = 0; + activateFeather(); + } + updateFeather(); + } + + function draw() { + requestAnimationFrame(draw); + + if (!visible) { + return; + } + + updateFocusedElementBounds(); + + // Update the bird's position + if (currentState === States.IDLE) { + if (focusedElement && !isWithinHorizontalBounds()) { + focusOnGround(); + } + birdY = getFocusedY(); + } else if (currentState === States.FLYING) { + // Fly to target location (even if in the air) + if (updateParabolicPath(FLY_SPEED)) { + setState(States.IDLE); + } + } + + const oldTargetY = targetY; + targetY = getFocusedY(); + // Adjust startY to account for scrolling + startY += targetY - oldTargetY; + if (targetY < 0 || targetY > window.innerHeight) { + // Fly to ground if the focused element moves out of bounds + focusOnGround(); + } + + ctx.clearRect(0, 0, canvas.width, canvas.height); + if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { + setAnimation(Animations.STILL); + } + + // Update HTML element position + setX(birdX); + setY(birdY); + } + function newStickyNote() { const id = Date.now().toString(); const site = window.location.href; @@ -925,98 +1097,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return true; } - function init() { - if (window !== window.top) { - // Skip installation if within an iframe - log("In iframe, skipping Birb script initialization"); - return; - } - log("Sprite sheets loaded successfully, initializing bird..."); - - // Preload font - const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; - const fontLink = document.createElement("link"); - fontLink.rel = "stylesheet"; - fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; - document.head.appendChild(fontLink); - - // Add stylesheet font-face - const fontFace = ` - @font-face { - font-family: 'Monocraft'; - src: url(${MONOCRAFT_SRC}) format('opentype'); - font-weight: normal; - font-style: normal; - } - `; - const fontStyle = document.createElement("style"); - fontStyle.innerHTML = fontFace; - document.head.appendChild(fontStyle); - - load(); - - styleElement.innerHTML = STYLESHEET; - document.head.appendChild(styleElement); - - canvas.id = "birb"; - canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; - canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; - document.body.appendChild(canvas); - - window.addEventListener("scroll", () => { - lastActionTimestamp = Date.now(); - }); - - onClick(document, (e) => { - lastActionTimestamp = Date.now(); - if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { - removeMenu(); - } - }); - - onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { - // Currently being pet, don't open menu - return; - } - insertMenu(); - }); - - canvas.addEventListener("mouseover", () => { - lastActionTimestamp = Date.now(); - if (currentState === States.IDLE) { - petStack.push(Date.now()); - if (petStack.length > 10) { - petStack.shift(); - } - const pets = petStack.filter((time) => Date.now() - time < 1000).length; - if (pets >= 3) { - pet(); - // Clear the stack - petStack = []; - } - } - }); - - canvas.addEventListener("touchmove", (e) => { - pet(); - }); - - drawStickyNotes(); - - let lastUrl = (window.location.href ?? "").split("?")[0]; - setInterval(() => { - const currentUrl = (window.location.href ?? "").split("?")[0]; - if (currentUrl !== lastUrl) { - log("URL changed, updating sticky notes"); - lastUrl = currentUrl; - drawStickyNotes(); - } - }, URL_CHECK_INTERVAL); - - setInterval(update, UPDATE_INTERVAL); - } - function drawStickyNotes() { // Remove all existing sticky notes const existingNotes = document.querySelectorAll(".birb-sticky-note"); @@ -1029,89 +1109,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - function update() { - ticks++; - - // Hide bird if the browser is fullscreen - if (document.fullscreenElement) { - hideBirb(); - // Won't be restored on fullscreen exit - } - - if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { - hop(); - } else if (Date.now() - lastActionTimestamp > AFK_TIME) { - // Idle for a while, do something - if (focusedElement === null) { - // Fly to an element - focusOnElement(); - lastActionTimestamp = Date.now(); - } else if (Math.random() < FOCUS_SWITCH_CHANCE) { - // Fly to another element if idle for a longer while - focusOnElement(); - lastActionTimestamp = Date.now(); - } - } - } else if (currentState === States.HOP) { - if (updateParabolicPath(HOP_SPEED)) { - setState(States.IDLE); - } - } - - // Double the chance of a feather if recently pet - const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; - if (visible && Math.random() < FEATHER_CHANCE * petMod) { - lastPetTimestamp = 0; - activateFeather(); - } - updateFeather(); - } - - function draw() { - requestAnimationFrame(draw); - - if (!visible) { - return; - } - - updateFocusedElementBounds(); - - // Update the bird's position - if (currentState === States.IDLE) { - if (focusedElement && !isWithinHorizontalBounds()) { - focusOnGround(); - } - birdY = getFocusedY(); - } else if (currentState === States.FLYING) { - // Fly to target location (even if in the air) - if (updateParabolicPath(FLY_SPEED)) { - setState(States.IDLE); - } - } - - const oldTargetY = targetY; - targetY = getFocusedY(); - // Adjust startY to account for scrolling - startY += targetY - oldTargetY; - if (targetY < 0 || targetY > window.innerHeight) { - // Fly to ground if the focused element moves out of bounds - focusOnGround(); - } - - ctx.clearRect(0, 0, canvas.width, canvas.height); - if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { - setAnimation(Animations.STILL); - } - - // Update HTML element position - setX(birdX); - setY(birdY); - } - - init(); - draw(); - /** * Create an HTML element with the specified parameters * @param {string} className @@ -1865,6 +1862,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return { ...params, [key]: value }; }, {}); } + + // Run the birb + init(); + draw(); }).catch((e) => { error("Error while loading sprite sheets: ", e); }); diff --git a/dist/birb.js b/dist/birb.js index d525656..1565bca 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -1155,6 +1155,178 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return settings().birbMode ? "Birb" : "Bird"; } + function init() { + if (window !== window.top) { + // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); + return; + } + log("Sprite sheets loaded successfully, initializing bird..."); + + // Preload font + const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; + const fontLink = document.createElement("link"); + fontLink.rel = "stylesheet"; + fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; + document.head.appendChild(fontLink); + + // Add stylesheet font-face + const fontFace = ` + @font-face { + font-family: 'Monocraft'; + src: url(${MONOCRAFT_SRC}) format('opentype'); + font-weight: normal; + font-style: normal; + } + `; + const fontStyle = document.createElement("style"); + fontStyle.innerHTML = fontFace; + document.head.appendChild(fontStyle); + + load(); + + styleElement.innerHTML = STYLESHEET; + document.head.appendChild(styleElement); + + canvas.id = "birb"; + canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; + canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; + document.body.appendChild(canvas); + + window.addEventListener("scroll", () => { + lastActionTimestamp = Date.now(); + }); + + onClick(document, (e) => { + lastActionTimestamp = Date.now(); + if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { + removeMenu(); + } + }); + + onClick(canvas, () => { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { + // Currently being pet, don't open menu + return; + } + insertMenu(); + }); + + canvas.addEventListener("mouseover", () => { + lastActionTimestamp = Date.now(); + if (currentState === States.IDLE) { + petStack.push(Date.now()); + if (petStack.length > 10) { + petStack.shift(); + } + const pets = petStack.filter((time) => Date.now() - time < 1000).length; + if (pets >= 3) { + pet(); + // Clear the stack + petStack = []; + } + } + }); + + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + + drawStickyNotes(); + + let lastUrl = (window.location.href ?? "").split("?")[0]; + setInterval(() => { + const currentUrl = (window.location.href ?? "").split("?")[0]; + if (currentUrl !== lastUrl) { + log("URL changed, updating sticky notes"); + lastUrl = currentUrl; + drawStickyNotes(); + } + }, URL_CHECK_INTERVAL); + + setInterval(update, UPDATE_INTERVAL); + } + + function update() { + ticks++; + + // Hide bird if the browser is fullscreen + if (document.fullscreenElement) { + hideBirb(); + // Won't be restored on fullscreen exit + } + + if (currentState === States.IDLE && !frozen && !isMenuOpen()) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { + hop(); + } else if (Date.now() - lastActionTimestamp > AFK_TIME) { + // Idle for a while, do something + if (focusedElement === null) { + // Fly to an element + focusOnElement(); + lastActionTimestamp = Date.now(); + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { + // Fly to another element if idle for a longer while + focusOnElement(); + lastActionTimestamp = Date.now(); + } + } + } else if (currentState === States.HOP) { + if (updateParabolicPath(HOP_SPEED)) { + setState(States.IDLE); + } + } + + // Double the chance of a feather if recently pet + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; + if (visible && Math.random() < FEATHER_CHANCE * petMod) { + lastPetTimestamp = 0; + activateFeather(); + } + updateFeather(); + } + + function draw() { + requestAnimationFrame(draw); + + if (!visible) { + return; + } + + updateFocusedElementBounds(); + + // Update the bird's position + if (currentState === States.IDLE) { + if (focusedElement && !isWithinHorizontalBounds()) { + focusOnGround(); + } + birdY = getFocusedY(); + } else if (currentState === States.FLYING) { + // Fly to target location (even if in the air) + if (updateParabolicPath(FLY_SPEED)) { + setState(States.IDLE); + } + } + + const oldTargetY = targetY; + targetY = getFocusedY(); + // Adjust startY to account for scrolling + startY += targetY - oldTargetY; + if (targetY < 0 || targetY > window.innerHeight) { + // Fly to ground if the focused element moves out of bounds + focusOnGround(); + } + + ctx.clearRect(0, 0, canvas.width, canvas.height); + if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { + setAnimation(Animations.STILL); + } + + // Update HTML element position + setX(birdX); + setY(birdY); + } + function newStickyNote() { const id = Date.now().toString(); const site = window.location.href; @@ -1268,98 +1440,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return true; } - function init() { - if (window !== window.top) { - // Skip installation if within an iframe - log("In iframe, skipping Birb script initialization"); - return; - } - log("Sprite sheets loaded successfully, initializing bird..."); - - // Preload font - const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; - const fontLink = document.createElement("link"); - fontLink.rel = "stylesheet"; - fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; - document.head.appendChild(fontLink); - - // Add stylesheet font-face - const fontFace = ` - @font-face { - font-family: 'Monocraft'; - src: url(${MONOCRAFT_SRC}) format('opentype'); - font-weight: normal; - font-style: normal; - } - `; - const fontStyle = document.createElement("style"); - fontStyle.innerHTML = fontFace; - document.head.appendChild(fontStyle); - - load(); - - styleElement.innerHTML = STYLESHEET; - document.head.appendChild(styleElement); - - canvas.id = "birb"; - canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; - canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; - document.body.appendChild(canvas); - - window.addEventListener("scroll", () => { - lastActionTimestamp = Date.now(); - }); - - onClick(document, (e) => { - lastActionTimestamp = Date.now(); - if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { - removeMenu(); - } - }); - - onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { - // Currently being pet, don't open menu - return; - } - insertMenu(); - }); - - canvas.addEventListener("mouseover", () => { - lastActionTimestamp = Date.now(); - if (currentState === States.IDLE) { - petStack.push(Date.now()); - if (petStack.length > 10) { - petStack.shift(); - } - const pets = petStack.filter((time) => Date.now() - time < 1000).length; - if (pets >= 3) { - pet(); - // Clear the stack - petStack = []; - } - } - }); - - canvas.addEventListener("touchmove", (e) => { - pet(); - }); - - drawStickyNotes(); - - let lastUrl = (window.location.href ?? "").split("?")[0]; - setInterval(() => { - const currentUrl = (window.location.href ?? "").split("?")[0]; - if (currentUrl !== lastUrl) { - log("URL changed, updating sticky notes"); - lastUrl = currentUrl; - drawStickyNotes(); - } - }, URL_CHECK_INTERVAL); - - setInterval(update, UPDATE_INTERVAL); - } - function drawStickyNotes() { // Remove all existing sticky notes const existingNotes = document.querySelectorAll(".birb-sticky-note"); @@ -1372,89 +1452,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - function update() { - ticks++; - - // Hide bird if the browser is fullscreen - if (document.fullscreenElement) { - hideBirb(); - // Won't be restored on fullscreen exit - } - - if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { - hop(); - } else if (Date.now() - lastActionTimestamp > AFK_TIME) { - // Idle for a while, do something - if (focusedElement === null) { - // Fly to an element - focusOnElement(); - lastActionTimestamp = Date.now(); - } else if (Math.random() < FOCUS_SWITCH_CHANCE) { - // Fly to another element if idle for a longer while - focusOnElement(); - lastActionTimestamp = Date.now(); - } - } - } else if (currentState === States.HOP) { - if (updateParabolicPath(HOP_SPEED)) { - setState(States.IDLE); - } - } - - // Double the chance of a feather if recently pet - const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; - if (visible && Math.random() < FEATHER_CHANCE * petMod) { - lastPetTimestamp = 0; - activateFeather(); - } - updateFeather(); - } - - function draw() { - requestAnimationFrame(draw); - - if (!visible) { - return; - } - - updateFocusedElementBounds(); - - // Update the bird's position - if (currentState === States.IDLE) { - if (focusedElement && !isWithinHorizontalBounds()) { - focusOnGround(); - } - birdY = getFocusedY(); - } else if (currentState === States.FLYING) { - // Fly to target location (even if in the air) - if (updateParabolicPath(FLY_SPEED)) { - setState(States.IDLE); - } - } - - const oldTargetY = targetY; - targetY = getFocusedY(); - // Adjust startY to account for scrolling - startY += targetY - oldTargetY; - if (targetY < 0 || targetY > window.innerHeight) { - // Fly to ground if the focused element moves out of bounds - focusOnGround(); - } - - ctx.clearRect(0, 0, canvas.width, canvas.height); - if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { - setAnimation(Animations.STILL); - } - - // Update HTML element position - setX(birdX); - setY(birdY); - } - - init(); - draw(); - /** * Create an HTML element with the specified parameters * @param {string} className @@ -2208,6 +2205,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return { ...params, [key]: value }; }, {}); } + + // Run the birb + init(); + draw(); }).catch((e) => { error("Error while loading sprite sheets: ", e); }); diff --git a/dist/birb.user.js b/dist/birb.user.js index ab361a5..d450320 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.10.25.117 +// @version 2025.10.25.124 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js @@ -1169,6 +1169,178 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return settings().birbMode ? "Birb" : "Bird"; } + function init() { + if (window !== window.top) { + // Skip installation if within an iframe + log("In iframe, skipping Birb script initialization"); + return; + } + log("Sprite sheets loaded successfully, initializing bird..."); + + // Preload font + const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; + const fontLink = document.createElement("link"); + fontLink.rel = "stylesheet"; + fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; + document.head.appendChild(fontLink); + + // Add stylesheet font-face + const fontFace = ` + @font-face { + font-family: 'Monocraft'; + src: url(${MONOCRAFT_SRC}) format('opentype'); + font-weight: normal; + font-style: normal; + } + `; + const fontStyle = document.createElement("style"); + fontStyle.innerHTML = fontFace; + document.head.appendChild(fontStyle); + + load(); + + styleElement.innerHTML = STYLESHEET; + document.head.appendChild(styleElement); + + canvas.id = "birb"; + canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; + canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; + document.body.appendChild(canvas); + + window.addEventListener("scroll", () => { + lastActionTimestamp = Date.now(); + }); + + onClick(document, (e) => { + lastActionTimestamp = Date.now(); + if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { + removeMenu(); + } + }); + + onClick(canvas, () => { + if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { + // Currently being pet, don't open menu + return; + } + insertMenu(); + }); + + canvas.addEventListener("mouseover", () => { + lastActionTimestamp = Date.now(); + if (currentState === States.IDLE) { + petStack.push(Date.now()); + if (petStack.length > 10) { + petStack.shift(); + } + const pets = petStack.filter((time) => Date.now() - time < 1000).length; + if (pets >= 3) { + pet(); + // Clear the stack + petStack = []; + } + } + }); + + canvas.addEventListener("touchmove", (e) => { + pet(); + }); + + drawStickyNotes(); + + let lastUrl = (window.location.href ?? "").split("?")[0]; + setInterval(() => { + const currentUrl = (window.location.href ?? "").split("?")[0]; + if (currentUrl !== lastUrl) { + log("URL changed, updating sticky notes"); + lastUrl = currentUrl; + drawStickyNotes(); + } + }, URL_CHECK_INTERVAL); + + setInterval(update, UPDATE_INTERVAL); + } + + function update() { + ticks++; + + // Hide bird if the browser is fullscreen + if (document.fullscreenElement) { + hideBirb(); + // Won't be restored on fullscreen exit + } + + if (currentState === States.IDLE && !frozen && !isMenuOpen()) { + if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { + hop(); + } else if (Date.now() - lastActionTimestamp > AFK_TIME) { + // Idle for a while, do something + if (focusedElement === null) { + // Fly to an element + focusOnElement(); + lastActionTimestamp = Date.now(); + } else if (Math.random() < FOCUS_SWITCH_CHANCE) { + // Fly to another element if idle for a longer while + focusOnElement(); + lastActionTimestamp = Date.now(); + } + } + } else if (currentState === States.HOP) { + if (updateParabolicPath(HOP_SPEED)) { + setState(States.IDLE); + } + } + + // Double the chance of a feather if recently pet + const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; + if (visible && Math.random() < FEATHER_CHANCE * petMod) { + lastPetTimestamp = 0; + activateFeather(); + } + updateFeather(); + } + + function draw() { + requestAnimationFrame(draw); + + if (!visible) { + return; + } + + updateFocusedElementBounds(); + + // Update the bird's position + if (currentState === States.IDLE) { + if (focusedElement && !isWithinHorizontalBounds()) { + focusOnGround(); + } + birdY = getFocusedY(); + } else if (currentState === States.FLYING) { + // Fly to target location (even if in the air) + if (updateParabolicPath(FLY_SPEED)) { + setState(States.IDLE); + } + } + + const oldTargetY = targetY; + targetY = getFocusedY(); + // Adjust startY to account for scrolling + startY += targetY - oldTargetY; + if (targetY < 0 || targetY > window.innerHeight) { + // Fly to ground if the focused element moves out of bounds + focusOnGround(); + } + + ctx.clearRect(0, 0, canvas.width, canvas.height); + if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { + setAnimation(Animations.STILL); + } + + // Update HTML element position + setX(birdX); + setY(birdY); + } + function newStickyNote() { const id = Date.now().toString(); const site = window.location.href; @@ -1282,98 +1454,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return true; } - function init() { - if (window !== window.top) { - // Skip installation if within an iframe - log("In iframe, skipping Birb script initialization"); - return; - } - log("Sprite sheets loaded successfully, initializing bird..."); - - // Preload font - const MONOCRAFT_SRC = "https://cdn.jsdelivr.net/gh/idreesinc/Monocraft@99b32ab40612ff2533a69d8f14bd8b3d9e604456/dist/Monocraft.otf"; - const fontLink = document.createElement("link"); - fontLink.rel = "stylesheet"; - fontLink.href = `url(${MONOCRAFT_SRC}) format('opentype')`; - document.head.appendChild(fontLink); - - // Add stylesheet font-face - const fontFace = ` - @font-face { - font-family: 'Monocraft'; - src: url(${MONOCRAFT_SRC}) format('opentype'); - font-weight: normal; - font-style: normal; - } - `; - const fontStyle = document.createElement("style"); - fontStyle.innerHTML = fontFace; - document.head.appendChild(fontStyle); - - load(); - - styleElement.innerHTML = STYLESHEET; - document.head.appendChild(styleElement); - - canvas.id = "birb"; - canvas.width = birbFrames.base.getPixels()[0].length * CANVAS_PIXEL_SIZE; - canvas.height = SPRITE_HEIGHT * CANVAS_PIXEL_SIZE; - document.body.appendChild(canvas); - - window.addEventListener("scroll", () => { - lastActionTimestamp = Date.now(); - }); - - onClick(document, (e) => { - lastActionTimestamp = Date.now(); - if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { - removeMenu(); - } - }); - - onClick(canvas, () => { - if (currentAnimation === Animations.HEART && (Date.now() - lastPetTimestamp < PET_MENU_COOLDOWN)) { - // Currently being pet, don't open menu - return; - } - insertMenu(); - }); - - canvas.addEventListener("mouseover", () => { - lastActionTimestamp = Date.now(); - if (currentState === States.IDLE) { - petStack.push(Date.now()); - if (petStack.length > 10) { - petStack.shift(); - } - const pets = petStack.filter((time) => Date.now() - time < 1000).length; - if (pets >= 3) { - pet(); - // Clear the stack - petStack = []; - } - } - }); - - canvas.addEventListener("touchmove", (e) => { - pet(); - }); - - drawStickyNotes(); - - let lastUrl = (window.location.href ?? "").split("?")[0]; - setInterval(() => { - const currentUrl = (window.location.href ?? "").split("?")[0]; - if (currentUrl !== lastUrl) { - log("URL changed, updating sticky notes"); - lastUrl = currentUrl; - drawStickyNotes(); - } - }, URL_CHECK_INTERVAL); - - setInterval(update, UPDATE_INTERVAL); - } - function drawStickyNotes() { // Remove all existing sticky notes const existingNotes = document.querySelectorAll(".birb-sticky-note"); @@ -1386,89 +1466,6 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI } } - function update() { - ticks++; - - // Hide bird if the browser is fullscreen - if (document.fullscreenElement) { - hideBirb(); - // Won't be restored on fullscreen exit - } - - if (currentState === States.IDLE && !frozen && !isMenuOpen()) { - if (Math.random() < HOP_CHANCE && currentAnimation !== Animations.HEART) { - hop(); - } else if (Date.now() - lastActionTimestamp > AFK_TIME) { - // Idle for a while, do something - if (focusedElement === null) { - // Fly to an element - focusOnElement(); - lastActionTimestamp = Date.now(); - } else if (Math.random() < FOCUS_SWITCH_CHANCE) { - // Fly to another element if idle for a longer while - focusOnElement(); - lastActionTimestamp = Date.now(); - } - } - } else if (currentState === States.HOP) { - if (updateParabolicPath(HOP_SPEED)) { - setState(States.IDLE); - } - } - - // Double the chance of a feather if recently pet - const petMod = Date.now() - lastPetTimestamp < PET_BOOST_DURATION ? PET_FEATHER_BOOST : 1; - if (visible && Math.random() < FEATHER_CHANCE * petMod) { - lastPetTimestamp = 0; - activateFeather(); - } - updateFeather(); - } - - function draw() { - requestAnimationFrame(draw); - - if (!visible) { - return; - } - - updateFocusedElementBounds(); - - // Update the bird's position - if (currentState === States.IDLE) { - if (focusedElement && !isWithinHorizontalBounds()) { - focusOnGround(); - } - birdY = getFocusedY(); - } else if (currentState === States.FLYING) { - // Fly to target location (even if in the air) - if (updateParabolicPath(FLY_SPEED)) { - setState(States.IDLE); - } - } - - const oldTargetY = targetY; - targetY = getFocusedY(); - // Adjust startY to account for scrolling - startY += targetY - oldTargetY; - if (targetY < 0 || targetY > window.innerHeight) { - // Fly to ground if the focused element moves out of bounds - focusOnGround(); - } - - ctx.clearRect(0, 0, canvas.width, canvas.height); - if (currentAnimation.draw(ctx, direction, animStart, species[currentSpecies])) { - setAnimation(Animations.STILL); - } - - // Update HTML element position - setX(birdX); - setY(birdY); - } - - init(); - draw(); - /** * Create an HTML element with the specified parameters * @param {string} className @@ -2222,6 +2219,10 @@ Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATI return { ...params, [key]: value }; }, {}); } + + // Run the birb + init(); + draw(); }).catch((e) => { error("Error while loading sprite sheets: ", e); }); diff --git a/manifest.json b/manifest.json index 37f1cf4..64a6a7f 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.10.25.117", + "version": "2025.10.25.124", "homepage_url": "https://idreesinc.com", "content_scripts": [ { From 4b206f638d9423ec8d8d8b6a8fab72b2cf9a9bc7 Mon Sep 17 00:00:00 2001 From: Idrees Hassan Date: Sun, 26 Oct 2025 12:19:25 -0400 Subject: [PATCH 11/11] Reformat promise --- birb.js | 8 ++++++-- dist/birb.js | 8 ++++++-- dist/birb.user.js | 10 +++++++--- manifest.json | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/birb.js b/birb.js index 72a8564..3f00d3e 100644 --- a/birb.js +++ b/birb.js @@ -478,8 +478,12 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-expect-error -Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).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; diff --git a/dist/birb.js b/dist/birb.js index 1565bca..d9b3a20 100644 --- a/dist/birb.js +++ b/dist/birb.js @@ -821,8 +821,12 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-expect-error -Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).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; diff --git a/dist/birb.user.js b/dist/birb.user.js index d450320..1a06cad 100644 --- a/dist/birb.user.js +++ b/dist/birb.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pocket Bird // @namespace https://idreesinc.com -// @version 2025.10.25.124 +// @version 2025.10.25.130 // @description birb // @author Idrees // @downloadURL https://github.com/IdreesInc/Pocket-Bird/raw/refs/heads/main/dist/birb.user.js @@ -835,8 +835,12 @@ function loadSpriteSheetPixels(dataUri, templateColors = true) { log("Loading sprite sheets..."); -// @ts-expect-error -Promise.all([loadSpriteSheetPixels(SPRITE_SHEET), loadSpriteSheetPixels(DECORATIONS_SPRITE_SHEET, false), loadSpriteSheetPixels(FEATHER_SPRITE_SHEET)]).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; diff --git a/manifest.json b/manifest.json index 64a6a7f..519db20 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Pocket Bird", "description": "It's a bird, in your browser. What more could you want?", - "version": "2025.10.25.124", + "version": "2025.10.25.130", "homepage_url": "https://idreesinc.com", "content_scripts": [ {