The West Perseus Toolkit

Useful tools for The West.

当前为 2018-10-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name The West Perseus Toolkit
  3. // @author Mr. Perseus
  4. // @namespace tw-perseus
  5. // @description Useful tools for The West.
  6. // @include https://*.the-west.*/game.php*
  7. // @include http://*.the-west.*/game.php*
  8. // @include https://*.tw.innogames.*/game.php*
  9. // @include http://*.tw.innogames.*/game.php*
  10. // @version 0.3.0
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. /*globals $*/
  15. (function (fn) {
  16. const script = document.createElement("script");
  17. script.setAttribute("type", "application/javascript");
  18. script.textContent = `(${fn})();`;
  19. document.body.appendChild(script);
  20. document.body.removeChild(script);
  21. }(() => {
  22. $(document).ready(() => {
  23. const TWPT = {
  24. base64: {
  25. menuImage: "url('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAABmgAwAEAAAAAQAAABkAAAAA/+0AOFBob3Rvc2hvcCAzLjAAOEJJTQQEAAAAAAAAOEJJTQQlAAAAAAAQ1B2M2Y8AsgTpgAmY7PhCfv/AABEIABkAGQMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/3QAEAAL/2gAMAwEAAhEDEQA/APHLTT9Z1bU7101HUApunA2t8qgHp0rqbXwHq0ttuOo6kGI67zXpX7PEA1bw5rGmGCF3S6llRmAGMOSQTjjI5r0iNLRtEvpBEgCNEA2QNoO7+eBXwmPzDFqs4wdld213sfQUIUlH3o3/AOCfJHjfw54i8PKsrajqSLhHG9iMgkeorpvt93/z9Tf9917h+0FdaMPAWvQ3yJNeyWkK2I8v5rVgvLFv4geDtFeBbx6frXuZZiJ1qT53do4qyXNeKsf/0PP/AIdePv8AhHW1TTGvmtop7lvMHK5AbPb612yfEjRfIZBfwFWxkZPOOmRXmV1/rR9Kb2r5fE5LSxE/aOTR7VLHSpR5eVMu/E74gx6zaPZ28jTGQqhbDEkZ6fhUGW/55tUEf+tH1FSV3YXDQwkOSBz1q0q0rs//2Q==')",
  26. },
  27. version: "0.3.0",
  28. settingsKey: "TWPT_preferences",
  29. defaultPreferences: {
  30. JobHighlighter: true,
  31. CinemaSkipButton: true,
  32. ZoomMap: true,
  33. DisablePremiumNotifications: true,
  34. NineTimesFifteenButton: true,
  35. },
  36. preferences: {},
  37. currentZoom: 1,
  38. };
  39.  
  40. TWPT.Updater = {
  41. init () {
  42. setTimeout(TWPT.Updater.load, 5000);
  43. },
  44.  
  45. load () {
  46. $.getScript("https://rawcdn.githack.com/mr-perseus/tw-js-library/master/script-updater.js", () => {
  47. if (scriptUpdater.TWPT > TWPT.version) {
  48. const updateMessage = new west.gui.Dialog(
  49. "Update: The West Perseus Toolkit",
  50. `<span>Update Available<br><br><b>v${scriptUpdater.TWPT}:</b><br>${scriptUpdater.TWPTNew
  51. }</span>`, west.gui.Dialog.SYS_WARNING
  52. ).addButton("Update", () => {
  53. updateMessage.hide();
  54. location.href = "https://greasyfork.org/scripts/370137-the-west-perseus-toolkit/code/The%20West%20Perseus%20Toolkit.user.js";
  55. }).addButton("cancel").show();
  56. }
  57. });
  58. },
  59. };
  60.  
  61. TWPT.Settings = {
  62. init () {
  63. const storage = JSON.parse(localStorage.getItem(TWPT.settingsKey));
  64. TWPT.preferences = storage ? storage : TWPT.defaultPreferences;
  65.  
  66. const div = $("<div class=\"ui_menucontainer\" />");
  67. const link = $("<div id=\"TWPT_Menu\" class=\"menulink\" title=\"The West Perseus Toolkit\" />")
  68. .css("background-image", TWPT.base64.menuImage)
  69. .css("background-position", "0px 0px")
  70. .mouseenter(function () {
  71. $(this).css("background-position", "-2px 0px");
  72. })
  73. .mouseleave(function () {
  74. $(this).css("background-position", "0px 0px");
  75. });
  76.  
  77. $(link).on("click", () => {
  78. TWPT.Settings.refreshMenu();
  79. });
  80.  
  81. $("#ui_menubar").append((div).append(link).append("<div class=\"menucontainer_bottom\" />"));
  82. },
  83.  
  84. refreshMenu () {
  85. const win = wman.open("TWPTSettings", "TWPT Settings", "noreload").setMaxSize(1268, 838).setMiniTitle("TWPT Settings");
  86. const scrollPane = new west.gui.Scrollpane();
  87.  
  88. const setTitle = function (name) {
  89. scrollPane.appendContent(`<p><span style="font-size: 130%; font-weight: bold; font-style: italic; display: inline-block; margin-top: 20px;">${
  90. name}</span></p>`);
  91. };
  92.  
  93. const setCheckBox = function (prefName, text) {
  94. const checkbox = new west.gui.Checkbox(text);
  95. checkbox.setId(`TWPT_${prefName}`);
  96. if (TWPT.preferences[prefName]) {
  97. checkbox.toggle();
  98. }
  99. checkbox.setCallback(() => {
  100. TWPT.preferences[prefName] = checkbox.isSelected();
  101. localStorage.setItem(TWPT.settingsKey, JSON.stringify(TWPT.preferences));
  102. TWPT.Settings.refreshMenu();
  103. new UserMessage("Okay. Please refresh your page.", "success").show();
  104. });
  105. scrollPane.appendContent(checkbox.getMainDiv());
  106. };
  107.  
  108. setTitle("Enabled Features");
  109. setCheckBox("JobHighlighter", "Enable Silver / Gold job highlighter (doesn't search for them on it's own).");
  110. setCheckBox("CinemaSkipButton", "Enable the Cinema Skip button (allows to skip cinema videos after 5 seconds).");
  111. setCheckBox("ZoomMap", "Enable the Zoom feature (hover the minimap icon on the top right and scroll up / down to zoom out / in).");
  112. setCheckBox("DisablePremiumNotifications", "Suppress energy refill and automation premium notifications.");
  113. setCheckBox("NineTimesFifteenButton", "Add a button to job windows which allows you to start 9x 15 second jobs at once.");
  114. setTitle("Feedback");
  115. scrollPane.appendContent("<ul style=\"margin-left:15px;line-height:18px;\">" +
  116. "<li>Send a message to <a target=\"_blank\" href=\"https://www.the-west.de/?ref=west_invite_linkrl&player_id=83071&world_id=1&hash=0dc5\">Mr. Perseus on world DE1</a></li>" +
  117. "<li>Contact me on <a target=\"_blank\" href=\"https://greasyfork.org/forum/messages/add/Mr. Perseus\">Greasy Fork</a></li>" +
  118. "<li>Send me a message on the <a target=\"_blank\" href=\"https://forum.beta.the-west.net/index.php?conversations/add&to=Mr.%20Perseus\">The West Beta Forum</a> or the <a target=\"_blank\" href=\"https://forum.the-west.de/index.php?conversations/add&to=Mr.%20Perseus\">German The West Forum</a></li>" +
  119. "</ul><br />Check out other scripts on <a target=\"_blank\" href=\"https://greasyfork.org/de/users/179973-mr-perseus\">Greasyfork</a>.");
  120.  
  121. win.appendToContentPane(scrollPane.getMainDiv());
  122. },
  123. };
  124.  
  125. TWPT.JobHighlighter = {
  126. init () {
  127. $("head").append("<style type=\"text/css\">" +
  128. ".jobgroup.silver {background-color: rgba(192, 192, 192, .7); border-radius: 10%; } " +
  129. ".jobgroup.gold {background-color: rgba(255, 215, 0, .7); border-radius: 10%; }" +
  130. "</style>");
  131.  
  132. // eslint-disable-next-line camelcase
  133. Map.Component.JobGroup.prototype.backup_getAdditionalClasses = Map.Component.JobGroup.prototype.getAdditionalClasses;
  134. Map.Component.JobGroup.prototype.getAdditionalClasses = function (tileX, tileY) {
  135. const backupClasses = Map.Component.JobGroup.prototype.backup_getAdditionalClasses.apply(this, arguments);
  136. const featuredJobs = Map.JobHandler.Featured[`${this.getLeft(tileX)}-${this.getTop(tileY)}`] || {};
  137.  
  138. for (const property in featuredJobs) {
  139. if (featuredJobs.hasOwnProperty(property)) {
  140. if (featuredJobs[property]["gold"]) {
  141. return `${backupClasses} gold`;
  142. }
  143. if (featuredJobs[property]["silver"]) {
  144. return `${backupClasses} silver`;
  145. }
  146. }
  147. }
  148.  
  149. return backupClasses;
  150. };
  151. },
  152. };
  153.  
  154. TWPT.CinemaSkipButton = {
  155. init () {
  156. const button = new west.gui.Button("Skip ad", () => {
  157. CinemaWindow.controller("rewards");
  158. });
  159.  
  160. // eslint-disable-next-line camelcase
  161. CinemaWindow.backup_cotroller = CinemaWindow.controller;
  162. CinemaWindow.controller = function (key) {
  163. button.setVisible(false);
  164. button.disable();
  165.  
  166. // Uncomment the following line if you want to access rewards directly.
  167. // if (key === "video") return CinemaWindow.backup_cotroller("rewards");
  168.  
  169.  
  170. if (key === "video") {
  171. let count = 5;
  172. const countDown = () => {
  173. if (count > 0) {
  174. button.setCaption(`Skip ad (${count})`);
  175. setTimeout(countDown, 1000);
  176. count--;
  177. } else {
  178. button.setCaption("Skip ad");
  179. button.enable();
  180. }
  181. };
  182. button.setVisible(true);
  183. countDown();
  184. }
  185.  
  186. // If there is no ad available you should be able to get the rewards.
  187. if (key === "noVideo") {
  188. return CinemaWindow.backup_cotroller("rewards");
  189. }
  190.  
  191. return CinemaWindow.backup_cotroller(key);
  192. };
  193.  
  194. // eslint-disable-next-line camelcase
  195. CinemaWindow.backup_open = CinemaWindow.open;
  196. CinemaWindow.open = function (townId) {
  197. CinemaWindow.backup_open(townId);
  198. const header = $(this.window.divMain).find(".tw2gui_inner_window_title");
  199. button.divMain.setAttribute("style", "margin-left: 20px; margin-top: -20px");
  200. button.setVisible(false);
  201. header.append(button.getMainDiv());
  202. };
  203. },
  204. };
  205.  
  206. TWPT.ZoomMap = {
  207. init () {
  208. $(window).bind("mousewheel", (event) => {
  209. if ($("#ui_minimap").is(":hover")) {
  210. if (event.originalEvent.wheelDelta >= 0) {
  211. if (TWPT.currentZoom < 1.95) TWPT.currentZoom += 0.1;
  212. } else {
  213. if (TWPT.currentZoom > 0.75) TWPT.currentZoom -= 0.1;
  214. }
  215.  
  216. document.getElementById("map").style.zoom = TWPT.currentZoom;
  217. }
  218. });
  219. },
  220. };
  221.  
  222. TWPT.DisablePremiumNotifications = {
  223. init () {
  224. Premium.checkForEnergyPremium = function (callback, failCallback) {
  225. if (typeof failCallback !== "undefined") return failCallback();
  226. };
  227. Premium.checkForAutomationPremium = function (callback, failCallback) {
  228. if (typeof failCallback !== "undefined") return failCallback();
  229. };
  230. },
  231. };
  232.  
  233. TWPT.NineTimesFifteenButton = {
  234. init () {
  235. // eslint-disable-next-line camelcase
  236. JobWindow.backup_initView = JobWindow.initView;
  237. JobWindow.initView = function () {
  238. JobWindow.backup_initView.apply(this, arguments);
  239. const button = new west.gui.Button("9x 15s", () => {
  240. button.disable();
  241. const jobAmountNum = this.window.divMain.getElementsByClassName("job-amount-num")[0];
  242. const numberBefore = jobAmountNum.innerHTML;
  243. jobAmountNum.innerHTML = "9";
  244. $(".job_durationbar.job_durationbar_short").click();
  245. setTimeout(() => {
  246. button.enable();
  247. jobAmountNum.innerHTML = numberBefore;
  248. }, 5000);
  249. });
  250.  
  251. const buttonDiv = button.getMainDiv();
  252. buttonDiv.style["z-index"] = "5";
  253. buttonDiv.style.bottom = "25px";
  254. buttonDiv.style.left = "300px";
  255. this.window.divMain.querySelector("div.tw2gui_window_content_pane").appendChild(button.getMainDiv());
  256. };
  257. },
  258. };
  259.  
  260. try {
  261. TWPT.Updater.init();
  262. TWPT.Settings.init();
  263. Object.keys(TWPT.preferences).forEach((property) => {
  264. if (TWPT.preferences[property]) {
  265. try {
  266. TWPT[property].init();
  267. } catch (err) {
  268. console.log(`TWPT Error with feature "${property}".`, err.stack);
  269. }
  270. }
  271. });
  272. } catch (err) {
  273. console.log(err.stack);
  274. }
  275. });
  276. }));