IdlePixel Keybinds

Adds customizable keybinds for panel switching and casting spells in combat.

  1. // ==UserScript==
  2. // @name IdlePixel Keybinds
  3. // @namespace com.anwinity.idlepixel
  4. // @version 1.0.0
  5. // @description Adds customizable keybinds for panel switching and casting spells in combat.
  6. // @author Anwinity
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const SPELL_DEFAULTS = {
  17. heal: "h",
  18. fire: "f",
  19. reflect: "r",
  20. invisibility: "i"
  21. };
  22. const SPELLS = Object.keys(Magic.spell_info);
  23. const PANELS = ["keyitems", "mining", "crafting", "gathering", "farming", "brewing", "woodcutting", "cooking", "fishing", "combat", "combat-canvas", "quests", "settings", "shop", "player-market", "donor-shop", "achievements", "idlepixelplus"];
  24.  
  25. let SPELL_MAPPING = {};
  26. let PANEL_MAPPING = {};
  27.  
  28. class KeyBindPlugin extends IdlePixelPlusPlugin {
  29. constructor() {
  30. super("keybinds", {
  31. about: {
  32. name: GM_info.script.name,
  33. version: GM_info.script.version,
  34. author: GM_info.script.author,
  35. description: GM_info.script.description + `<br />In general, lowercase letters map to their respective keys. For other keys, check out <a href="https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values">this page</a>. Note that this does NOT use KeyboardEvent.keyCode, it uses KeyboardEvent.key.`
  36. },
  37. config: [
  38. {
  39. label: "Combat Spells",
  40. type: "label"
  41. },
  42. ... SPELLS.map(spell => {
  43. return {
  44. id: spell,
  45. label: spell,
  46. type: "string",
  47. default: SPELL_DEFAULTS[spell]||""
  48. }
  49. }),
  50. {
  51. label: "Panel Switching",
  52. type: "label"
  53. },
  54. ...PANELS.map(panel => {
  55. return {
  56. id: panel,
  57. label: panel,
  58. type: "string",
  59. default: ""
  60. }
  61. })
  62. ]
  63. });
  64. }
  65.  
  66. onConfigsChanged() {
  67. SPELL_MAPPING = {};
  68. PANEL_MAPPING = {};
  69.  
  70. SPELLS.forEach(spell => {
  71. let key = this.getConfig(spell);
  72. if(key) {
  73. SPELL_MAPPING[key] = spell;
  74. }
  75. });
  76.  
  77. PANELS.forEach(panel => {
  78. let key = this.getConfig(panel);
  79. if(key) {
  80. PANEL_MAPPING[key] = panel;
  81. }
  82. });
  83. }
  84.  
  85. handleKeyEvent(event) {
  86. if(event.key in SPELL_MAPPING) {
  87. if(Globals.currentPanel == "panel-combat-canvas") {
  88. const spell = SPELL_MAPPING[event.key];
  89. IdlePixelPlus.sendMessage(`SPELL=${spell}`);
  90. }
  91. }
  92. if(event.key in PANEL_MAPPING) {
  93. const focused = document.activeElement;
  94. if(!focused || !["INPUT", "SELECT"].includes((focused.tagName||"").toUpperCase())) {
  95. const panel = PANEL_MAPPING[event.key];
  96. if(panel == "combat-canvas") {
  97. if(Combat.in_combat()) {
  98. IdlePixelPlus.setPanel(panel);
  99. }
  100. }
  101. else {
  102. IdlePixelPlus.setPanel(panel);
  103. }
  104. }
  105. }
  106. }
  107.  
  108. onLogin() {
  109. const self = this;
  110.  
  111. document.addEventListener("keyup", e => {
  112. this.handleKeyEvent(e);
  113. });
  114.  
  115. this.onConfigsChanged();
  116. }
  117.  
  118. }
  119.  
  120. const plugin = new KeyBindPlugin();
  121. IdlePixelPlus.registerPlugin(plugin);
  122.  
  123. })();