TW Accessible items

Add any item to be always visible on the main game screen

  1. // ==UserScript==
  2. // @name TW Accessible items
  3. // @namespace https://gf.qytechs.cn/users/205257
  4. // @version 0.2
  5. // @description Add any item to be always visible on the main game screen
  6. // @author qwerfghj
  7. // @match https://*.the-west.com.br/game.php*
  8. // @match https://*.the-west.com.pt/game.php*
  9. // @match https://*.the-west.cz/game.php*
  10. // @match https://*.the-west.de/game.php*
  11. // @match https://*.the-west.dk/game.php*
  12. // @match https://*.the-west.es/game.php*
  13. // @match https://*.the-west.fr/game.php*
  14. // @match https://*.the-west.gr/game.php*
  15. // @match https://*.the-west.hu/game.php*
  16. // @match https://*.the-west.it/game.php*
  17. // @match https://*.the-west.net/game.php*
  18. // @match https://*.the-west.nl/game.php*
  19. // @match https://*.the-west.org/game.php*
  20. // @match https://*.the-west.pl/game.php*
  21. // @match https://*.the-west.ro/game.php*
  22. // @match https://*.the-west.ru/game.php*
  23. // @match https://*.the-west.se/game.php*
  24. // @match https://*.the-west.sk/game.php*
  25. // @grant none
  26. // ==/UserScript==
  27.  
  28. (function(fn) {
  29. var newScript = document.createElement('script');
  30. newScript.setAttribute("type", "application/javascript");
  31. newScript.textContent = '(' + fn + ')();';
  32. (document.body || document.head || document.documentElement).appendChild(newScript);
  33. newScript.parentNode.removeChild(newScript);
  34. })(function() {
  35. var scriptName = "TW Accessible items";
  36. var scriptURL = "https://gf.qytechs.cn/en/scripts/371237-tw-accessible-items";
  37. var scriptAuthor = "qwerfghj";
  38. var scriptObject = "AccessibleItems";
  39. this[scriptObject] = {
  40. helpers: {
  41. item_by_id: function(item_id) {
  42. var base_id = ItemManager.itemIdToBaseItemId(item_id);
  43. return ItemManager.getByBaseId(base_id);
  44. },
  45. item_name: function(item_id) {
  46. return this.item_by_id(item_id).name;
  47. }
  48. },
  49. inventoryManager: {
  50. listeningSignals: [ 'inventory_loaded', 'inventory_changed', 'cooldown_changed', 'item_lifetime_changed', 'item_used' ],
  51. timeout: null,
  52. items: {},
  53. init: function() {
  54. EventHandler.listen(this.listeningSignals, this.signalHandler, this);
  55. },
  56. destroy: function() {
  57. clearTimeout(this.timeout);
  58. EventHandler.unlisten(this.listeningSignals, this.signalHandler, this);
  59. },
  60. check: function() {
  61. $.each(this.items, function(item_id, item) {
  62. item.getMainDiv().css("display", "none");
  63. });
  64. clearTimeout(this.timeout);
  65. var that = this;
  66. $.each(AccessibleItems.settings.getItems(), function(item_id, position) {
  67. var invItem = Bag.getItemByItemId(item_id);
  68. if(invItem) {
  69. that.handleItem(invItem, item_id, position);
  70. }
  71. });
  72. },
  73. handleItem: function(item, id, position) {
  74. if (this.items[id] === undefined) {
  75. this.items[id] = new tw2widget.InventoryItem(item.obj);
  76. this.items[id].initDisplay();
  77. var div = this.items[id].getMainDiv();
  78. div.css("position", "absolute");
  79. div.css("z-index", 1);
  80. var action = item.obj.action;
  81. this.items[id].getImgEl().click(function() { eval(action); });
  82. this.items[id].setShowcompare(false);
  83. div.appendTo(document.body);
  84. }
  85.  
  86. item.initDisplay();
  87. if (item.elCooldown) {
  88. this.items[id].showCooldown();
  89. item.elCooldown = item.elCooldown.add(this.items[id].elCooldown);
  90. }
  91.  
  92. this.items[id].setCooldown(Bag.itemCooldown[id]);
  93. this.items[id].setLifetime(item.lifetime, true);
  94. this.items[id].setCount(item.getCount());
  95. this.items[id].getMainDiv().css("left", position[0]);
  96. this.items[id].getMainDiv().css("top", position[1]);
  97. this.items[id].getMainDiv().css("display", "block");
  98. },
  99. signalHandler: function() {
  100. var that = this;
  101. this.timeout = setTimeout(function() { that.check(); }, 1000);
  102. }
  103. },
  104. settings: {
  105. init: function() {
  106. if (!AccessibleItems.storage.exists("items")) {
  107. AccessibleItems.storage.add("items", { 2485000: [80, 217] });
  108. }
  109. },
  110. getItems: function() {
  111. return AccessibleItems.storage.get("items");
  112. },
  113. saveItems: function(items) {
  114. AccessibleItems.storage.add("items", items);
  115. AccessibleItems.inventoryManager.check();
  116. AccessibleItems.script.fillGui();
  117. },
  118. add: function() {
  119. this.createEditDialog();
  120. },
  121. createEditDialog: function(init_id = 0, init_x = "", init_y = "") {
  122. var that = this;
  123.  
  124. var preview_item = null;
  125. var removePreviewIfNot = function(item_id) {
  126. if (preview_item && preview_item.getId() !== item_id) {
  127. preview_item.getMainDiv().remove();
  128. preview_item = null;
  129. }
  130. };
  131.  
  132. var id = new west.gui.Textfield().setTooltip("1234000 or [item=1234000]");
  133. var x = new west.gui.Textfield(null, "number").setPlaceholder("left").setWidth(72).setValue(init_x);
  134. var y = new west.gui.Textfield(null, "number").setPlaceholder("top").setWidth(72).setValue(init_y);
  135. var d = new west.gui.Dialog(scriptName, '<div class="txcenter"><table style="margin: 0 auto"><tr><td class="qwerfghj_acs_item" style="padding-right: 10px"></td><td class="qwerfghj_acs_item_id" style="vertical-align: middle" colspan="2">Enter item id:<br /><input type="hidden" value="' + init_id + '" class="qwerfghj_acs_id" /></td></tr><tr><td style="vertical-align: middle">Position:</td><td class="qwerfghj_acs_x"></td><td class="qwerfghj_acs_y"></td></tr></table></div>').setDraggable(true).setBlockGame(false);
  136. d.addButton("cancel", function() {
  137. removePreviewIfNot(-1);
  138. return true;
  139. });
  140. var el = function(cls) { return $(".qwerfghj_acs_" + cls, d.getMainDiv()); };
  141. d.addButton("ok", function() {
  142. var item_id = parseInt(el("id").val());
  143. if (item_id == 0) {
  144. new MessageError("Item is not selected!").show();
  145. return false;
  146. }
  147. if (x.getValue() === "" || y.getValue() === "") {
  148. new MessageError("Position is not set!").show();
  149. return false;
  150. }
  151. removePreviewIfNot(-1);
  152. that.set(item_id, [parseInt(x.getValue()), parseInt(y.getValue())]);
  153. new MessageSuccess("Item added!").show();
  154. return true;
  155. });
  156.  
  157. new tw2widget.InventoryItem(ItemManager.get(init_id)).getMainDiv().appendTo(el("item"));
  158.  
  159. if (init_id != 0) {
  160. id.setValue(init_id);
  161. }
  162.  
  163. var updatePreview = function() {
  164. var item_id = parseInt(el("id").val());
  165. if (item_id == 0 || x.getValue() === "" || y.getValue() === "") return;
  166. removePreviewIfNot(item_id);
  167. if (preview_item == null) {
  168. preview_item = new tw2widget.InventoryItem(ItemManager.get(item_id));
  169. preview_item.getMainDiv().css("z-index", 1);
  170. preview_item.getMainDiv().css("position", "absolute");
  171. preview_item.getMainDiv().append($('<span class="cooldown" style="display: inline">Preview</span>'));
  172. preview_item.getMainDiv().appendTo(document.body);
  173. }
  174.  
  175. preview_item.getMainDiv().css("left", x.getValue() + "px");
  176. preview_item.getMainDiv().css("top", y.getValue() + "px");
  177. };
  178.  
  179. id.addKeyUpListener(function() {
  180. try {
  181. var value = parseInt(id.getValue().replace(/\[item=(\d+)\]/, "$1"));
  182. var item = ItemManager.get(value);
  183. if (item) {
  184. el("item").empty();
  185. new tw2widget.InventoryItem(item).getMainDiv().appendTo(el("item"));
  186. el("id").val(value);
  187. updatePreview();
  188. }
  189. }
  190. catch (e) { }
  191. });
  192. x.getField().add(y.getField()).attr("min", 0).on("input", updatePreview);
  193.  
  194. id.getMainDiv().appendTo(el("item_id"));
  195. x.getMainDiv().appendTo(el("x"));
  196. y.getMainDiv().appendTo(el("y"));
  197.  
  198. d.show();
  199. updatePreview();
  200. },
  201. change: function(item_id) {
  202. var pos = this.getItems()[item_id] || ["", ""];
  203. this.createEditDialog(item_id, pos[0], pos[1]);
  204. },
  205. set: function(item_id, position) {
  206. var items = this.getItems();
  207. items[item_id] = position;
  208. this.saveItems(items);
  209. },
  210. remove: function(item_id) {
  211. var dialog = new west.gui.Dialog(scriptName, "Do you really want to remove " + AccessibleItems.helpers.item_name(item_id) + "?", west.gui.Dialog.SYS_QUESTION), that = this;
  212. dialog.addButton("yes", function() {
  213. var items = that.getItems();
  214. delete items[item_id];
  215. that.saveItems(items);
  216. return true;
  217. });
  218. dialog.addButton("cancel").show();
  219. }
  220. },
  221. storage: {
  222. add: function(name, value) {
  223. localStorage.setItem("EnergyBag_" + name, JSON.stringify(value));
  224. },
  225. get: function(name) {
  226. if (!this.exists(name)) {
  227. console.log("Storage contains no value for name: " + name);
  228. return null;
  229. }
  230. return JSON.parse(localStorage.getItem("EnergyBag_" + name));
  231. },
  232. exists: function(name) {
  233. return localStorage.getItem("EnergyBag_" + name) !== null;
  234. }
  235. },
  236. script: {
  237. api: null,
  238. listeningSignal: 'game_config_loaded',
  239. init: function() {
  240. var that = this;
  241. if(!!(Game && Game.loaded)) {
  242. this.api = TheWestApi.register(scriptObject, scriptName, "2.82", Game.version.toString(), scriptAuthor, scriptURL);
  243.  
  244. AccessibleItems.settings.init();
  245.  
  246. if (!!(ItemManager && ItemManager.isLoaded())) {
  247. AccessibleItems.inventoryManager.init();
  248. this.fillGui();
  249. }
  250. else {
  251. EventHandler.listen("itemmanager_loaded", function() {
  252. AccessibleItems.inventoryManager.init();
  253. that.fillGui();
  254. return EventHandler.ONE_TIME_EVENT;
  255. });
  256. }
  257.  
  258. }
  259. else {
  260. EventHandler.listen(this.listeningSignal, function() {
  261. that.init();
  262. return EventHandler.ONE_TIME_EVENT;
  263. });
  264. }
  265. },
  266. fillGui: function() {
  267. var html = '<p style="margin: 8px; margin-left: 16px; font-size: 18px;">Items to display:</p>';
  268. $.each(AccessibleItems.settings.getItems(), function(item_id, position) {
  269. html += '<p style="margin: 8px; font-size: 16px;">';
  270. html += Game.TextHandler.parse("[item=" + item_id + "]");
  271. html += " at position [" + position + "]";
  272. html += ' <a href="javascript:AccessibleItems.settings.change(' + item_id + ')">(change)</a>';
  273. html += ' <a href="javascript:AccessibleItems.settings.remove(' + item_id + ')">[remove]</a>.';
  274. html += "</p>";
  275. });
  276. html += '<div class="tw2gui_button" onclick="javascript:AccessibleItems.settings.add()" style="margin-left: 16px; margin-top: 16px;"><div class="tw2gui_button_right_cap"></div><div class="tw2gui_button_left_cap"></div><div class="tw2gui_button_middle_bg"></div><div class="textart_title">Add new item</div></div>';
  277.  
  278. this.api.setGui(html);
  279.  
  280. var script_window = wman.getById("scripts");
  281. if (script_window) {
  282. if (script_window.currentActiveTabId == scriptObject) {
  283. script_window.fireActivateTab(scriptObject);
  284. }
  285. }
  286. }
  287. }
  288. };
  289. $(function() { try { AccessibleItems.script.init(); } catch(x) { console.trace(x); } });
  290. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址