From 6f2e16ecd73532703d862d633a1d6c8db9176c83 Mon Sep 17 00:00:00 2001 From: pot0rpan <57581179+pot0rpan@users.noreply.github.com> Date: Sat, 12 Feb 2022 14:32:38 -0700 Subject: [PATCH] Add injectCreepSay client abuse --- README.md | 1 + src/client-abuse/TypeScript/injectCreepSay.ts | 205 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 src/client-abuse/TypeScript/injectCreepSay.ts diff --git a/README.md b/README.md index b61e8c5..a47f328 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ For pull requests, we use [GitConsensus](https://www.gitconsensus.com/) to allow |JS| [util.inject.RoomTracker.js](/src/client-abuse/JavaScript/util.inject.RoomTracker.js) |semperrabbit|Allows for the retrieval of rooms currently being viewed in the client from in-game code| |JS| [util.inject.RoomViewNotifier.js](/src/client-abuse/JavaScript/util.inject.RoomViewNotifier.js) |semperrabbit| adds currently viewed room to memory | |JS| [util.inject.TEMPLATE.js](/src/client-abuse/JavaScript/util.inject.TEMPLATE.js) |semperrabbit|template for injections| +|TS| [injectCreepSay.ts](/src/client-abuse/TypeScript/injectCreepSay.ts) |poot|Add creep say input to sidebar| ### globals diff --git a/src/client-abuse/TypeScript/injectCreepSay.ts b/src/client-abuse/TypeScript/injectCreepSay.ts new file mode 100644 index 0000000..2b9d58c --- /dev/null +++ b/src/client-abuse/TypeScript/injectCreepSay.ts @@ -0,0 +1,205 @@ +/** + * Creep Say client abuse + * Posted 2022-02-12 by poot + * Thanks to semperrabbit for the templates and examples + * + * Adds a row to the right sidebar when a creep is selected, + * allowing you to instruct creeps to say things on-demand from the client. + * + * Import into main.ts with `import 'injectCreepSay';` + * This will call `global.injectCreepSay()` every global reset, + * and can be forced by calling `forceInjectCreepSay()` from the game CLI + */ + +declare var global: any; +declare var angular: any; +declare var $: any; + +type SelectedObject = { + _id: string; + x: number; + y: number; +} & { + type: 'creep'; + name: string; + user: string; +}; + +//! This function must have no dependencies outside of the function body since it gets stringified +function INJECT_ME() { + const STYLES = ``; + + if (window._injectCreepSayInstalled) { + console.info( + '[injectCreepSay]: Already ran injection script, reload page first' + ); + return; + } + + window._injectCreepSayInstalled = true; + const [, page] = window.location.hash.split('/'); + + /* Ensure we are in room view */ + if (page !== 'room') return; + + if (!window._userId) { + window._userId = angular.element(document.body).scope().Me()._id; + } + + /* Append CSS stylesheet to head */ + if (!$('#creep-say-styles')[0]) { + const css = document.createElement('style'); + css.id = 'creep-say-styles'; + css.appendChild(document.createTextNode(STYLES)); + document.head.appendChild(css); + } + + /* Add handler to add elements to sidebar and assign listeners when selected object changes */ + angular + .element($('.room.ng-scope')) + .scope() + .$watch('Room.selectedObject', onSelectedObjectChange); + + function onSelectedObjectChange(selectedObject: SelectedObject | null): void { + if (!selectedObject) return; + + let sayForm = $('.creep-say')[0]; + + if ( + !selectedObject || + selectedObject.type !== 'creep' || + selectedObject.user !== window._userId + ) { + if (sayForm) { + sayForm.parentNode.removeChild(sayForm); + } + + return; + } + + if (sayForm) { + return; + } + + sayForm = document.createElement('form'); + sayForm.classList.add('creep-say', 'body'); + + sayForm.addEventListener('submit', (e: any) => { + e.preventDefault(); + const textEl = $('#say-text')[0]; + const isPublic = $('#say-public')[0].checked; + if (!textEl.value) return; + + angular + .element(document.body) + .injector() + .get('Connection') + .sendConsoleCommand( + `_sayCreep = Game.creeps["${selectedObject.name}"]; _sayCreep && _sayCreep.say("${textEl.value}", ${isPublic});` + ); + + textEl.value = ''; + }); + + sayForm.innerHTML = ` + +