Created
May 15, 2024 20:37
-
-
Save tiennou/d0675953014d128e00883c509942d25e to your computer and use it in GitHub Desktop.
Screeps - Show Creep Birthday
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Screeps Birthday viewer | |
| // @namespace http://tampermonkey.net/ | |
| // @version 2024-05-15 | |
| // @description This adds a creep's birthday to the inspector | |
| // @author Traxus, various | |
| // @run-at document-ready | |
| // @match https://screeps.com/a/ | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=screeps.com | |
| // @require https://raw.githubusercontent.com/Esryok/screeps-browser-ext/master/screeps-browser-core.js | |
| // @grant none | |
| // ==/UserScript== | |
| // Original from https://github.com/screepers/screeps-snippets/blob/master/src/client-abuse/JavaScript/util.inject.Birthday.js | |
| log("TamperMonkey - Loaded Birthday Viewer"); | |
| function log(...args) { | |
| console.warn(...args); | |
| } | |
| function formatDate(d){ | |
| return ("0" + d.getUTCHours()).slice(-2)+":"+("0" + d.getUTCMinutes()).slice(-2)+":"+("0" + d.getUTCSeconds()).slice(-2) + " " + | |
| ("0" + (d.getUTCMonth()+1)).slice(-2)+"/"+("0" + d.getUTCDate()).slice(-2)+"/"+d.getUTCFullYear() + " UTC"; | |
| }; | |
| function showBdayInternal() { | |
| let gameEl = angular.element($('section.game')); | |
| let roomEl = angular.element($('section.room')); | |
| let $rootScope = gameEl.injector().get('$rootScope'); | |
| let $compile = gameEl.injector().get('$compile'); | |
| let target = $('.object-properties .aside-block-content')[0]; | |
| let elem = $('<div class="ng-binding ng-scope"><label>BirthDate: </label>' + formatDate(new Date(parseInt(roomEl.scope().Room.selectedObject._id.substr(0,8), 16)*1000)) + '</div>'); | |
| $compile(elem)($rootScope); | |
| if(target.children.length > 1) { | |
| elem.insertBefore(target.children[2]); | |
| } else { | |
| elem.insertBefore(target.children[0].children[2]); | |
| } | |
| } | |
| // Polls every 50 milliseconds for a given condition | |
| async function waitFor(condition, pollInterval = 50, timeoutAfter) { | |
| // Track the start time for timeout purposes | |
| const startTime = Date.now(); | |
| while (true) { | |
| // Check for timeout, bail if too much time passed | |
| if(typeof(timeoutAfter) === 'number' && Date.now() > startTime + timeoutAfter) { | |
| throw 'Condition not met before timeout'; | |
| } | |
| // Check for conditon immediately | |
| const result = await condition(); | |
| // If the condition is met... | |
| if(result) { | |
| // Return the result.... | |
| return result; | |
| } | |
| // Otherwise wait and check after pollInterval | |
| await new Promise(r => setTimeout(r, pollInterval)); | |
| } | |
| } | |
| // Entry point | |
| $(document).ready(() => { | |
| waitFor(() => angular.element(document.body).scope() !== undefined).then(() => { | |
| ScreepsAdapter.onViewChange((view) => { | |
| ScreepsAdapter.$timeout(() => { | |
| if (view == 'view' && $('.object-properties .aside-block-content')[0]) { | |
| showBdayInternal(); | |
| } | |
| }, 100); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment