Advertisement
Gatuno

Dead Frontier - Money Warning script for Tampermonkey

Feb 11th, 2025
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Dead Frontier - Money Warning for Inner City
  3. // @namespace   Dead Frontier - Shrike00
  4. // @include     /^https?://fairview\.deadfrontier\.com/onlinezombiemmo/index\.php$/
  5. // @grant       none
  6. // @version     0.1.2
  7. // @author      Shrike00
  8. // @description Displays a pop-up warning when leaving the outpost with cash equal or over the threshold value.
  9. // @license     MIT
  10. // @downloadURL https://update.greasyfork.org/scripts/437455/Dead%20Frontier%20-%20Money%20Warning%20for%20Inner%20City.user.js
  11. // @updateURL https://update.greasyfork.org/scripts/437455/Dead%20Frontier%20-%20Money%20Warning%20for%20Inner%20City.meta.js
  12. // ==/UserScript==
  13.  
  14. // Changelog
  15. // 0.1.2 - August 6, 2023
  16. // - Change: Changed text colours.
  17. // 0.1.1
  18. // - Feature: Added optional warning banner to the outpost page.
  19.  
  20. // Procedures partly taken from hotrod's base.js and outpost.js (moving to inner city).
  21. // TODO: Optional banner display
  22.  
  23. (function() {
  24.  
  25.     'use strict';
  26.     // User Options
  27.     const threshold = 1;
  28.     const display_popup_warning = true;
  29.     const display_warning_banner = true;
  30.  
  31.     // Helpers
  32.     function getCash() {
  33.         // Returns current cash-on-hand.
  34.         const uservars = window.userVars;
  35.         return parseInt(uservars["df_cash"]);
  36.     }
  37.  
  38.     function innerCityButton() {
  39.         // Returns button element for going to the Inner City.
  40.         const outpost = document.getElementById("outpost");
  41.         const navigables = outpost.getElementsByClassName("opElem");
  42.         for (let i = 0; i < navigables.length; i++) {
  43.             const child = navigables[i];
  44.             const button = child.children[0];
  45.             if (button.innerHTML.indexOf("Inner City") != -1) {
  46.                 return button;
  47.             }
  48.         }
  49.     }
  50.  
  51.     function waitForInnerCityButton(callback, timeout) {
  52.         // Waits for Inner City button to exist, then calls callback.
  53.         const start = performance.now();
  54.         const check = setInterval(function() {
  55.             const innercity = innerCityButton();
  56.             if (innercity !== undefined) {
  57.                 clearInterval(check);
  58.                 callback();
  59.             }
  60.             if (performance.now() - start > timeout) {
  61.                 clearInterval(check);
  62.             }
  63.         }, 200);
  64.     }
  65.  
  66.     // Major Subroutines
  67.  
  68.     function removeEventListeners(button) {
  69.         // Removes original event listeners from button.
  70.         button.removeEventListener("click", nChangePage);
  71.         button.removeEventListener("auxclick", nChangePage);
  72.     }
  73.  
  74.     function setWarningHidden() {
  75.         // Hides pop-up warning.
  76.         const prompt = document.getElementById("prompt");
  77.         const gamecontent = document.getElementById("gamecontent");
  78.         prompt.removeAttribute("style");
  79.         gamecontent.removeAttribute("class");
  80.     }
  81.  
  82.     function setWarningVisible(innercityCallback) {
  83.         // Shows pop-up warning/prompt.
  84.         const prompt = document.getElementById("prompt");
  85.         const gamecontent = document.getElementById("gamecontent");
  86.         // Warning message
  87.         prompt.style.setProperty("display", "inline");
  88.         gamecontent.className = "warning";
  89.         gamecontent.style.setProperty("font-family", "\"Courier New CE\", Arial");
  90.         gamecontent.style.setProperty("font-weight", "bold");
  91.         gamecontent.style.setProperty("color", "white");
  92.         gamecontent.style.setProperty("text-align", "center");
  93.         const cash = getCash().toLocaleString();
  94.         // Copied from DF CSS.
  95.         gamecontent.innerHTML = "You are carrying<br><span style=\"background: #E6CC4D; background-image: -webkit-gradient( linear, left top, left bottom, color-stop(0, #E6CC4D), color-stop(0.5, #E6CC4D), color-stop(1, #000000) ); -webkit-background-clip: text; -webkit-text-fill-color: transparent;\">$" + cash + "</span><br> Are you sure you wish to continue?";
  96.         // Buttons
  97.         const noButton = document.createElement("button");
  98.         noButton.style.position = "absolute";
  99.         noButton.style.top = "72px";
  100.         noButton.style.left = "151px";
  101.         noButton.innerHTML = "No";
  102.         noButton.addEventListener("click", setWarningHidden);
  103.         gamecontent.appendChild(noButton);
  104.         const yesButton = document.createElement("button");
  105.         yesButton.style.position = "absolute";
  106.         yesButton.style.left = "86px";
  107.         yesButton.style.top = "72px";
  108.         yesButton.innerHTML = "Yes";
  109.         yesButton.addEventListener("click", innercityCallback);
  110.         gamecontent.appendChild(yesButton);
  111.     }
  112.  
  113.     function pageChangeInnerCityCallback(innercityButton) {
  114.         // Returns function (event callback) that goes to Inner City.
  115.         return function(e) {
  116.             const sound = innercityButton.dataset.sound !== undefined && innercityButton.dataset.sound !== "0";
  117.             const modify = innercityButton.dataset.mod;
  118.             const pageNum = parseInt(innercityButton.dataset.page);
  119.             if (sound) {
  120.                 playSound("outpost");
  121.                 setTimeout(function() {
  122.                     doPageChange(pageNum, modify);
  123.                 }, 1000);
  124.             } else {
  125.                 doPageChange(pageNum, modify);
  126.             }
  127.         }
  128.     }
  129.  
  130.     function warningBanner() {
  131.         // Creates warning banner element without text (to be supplied by caller).
  132.         const banner = document.createElement("div");
  133.         banner.style.position = "absolute";
  134.         banner.style["text-align"] = "center";
  135.         banner.style["font-size"] = "16px";
  136.         banner.style.width = "500px";
  137.         banner.style.top = "500px";
  138.         banner.style.left = "100px";
  139.         banner.style.color = "red";
  140.         banner.setAttribute("mwic", "banner");
  141.         return banner;
  142.     }
  143.  
  144.     function removeWarningBanner(outpost) {
  145.         // Removes warning banner from outpost screen.
  146.         const children = outpost.children;
  147.         for (let i = 0; i < children.length; i++) {
  148.             const child = children[i];
  149.             if (child.getAttribute("mwic") == "banner") {
  150.                 child.remove();
  151.                 break;
  152.             }
  153.         }
  154.     }
  155.  
  156.     function refreshBanner(outpost) {
  157.         removeWarningBanner(outpost);
  158.         const banner = warningBanner();
  159.         banner.innerHTML = "<span style=\"color: white;\">You are carrying</span> <span style=\"background: #E6CC4D; background-image: -webkit-gradient( linear, left top, left bottom, color-stop(0, #E6CC4D), color-stop(0.5, #E6CC4D), color-stop(1, #000000) ); -webkit-background-clip: text; -webkit-text-fill-color: transparent;\">$" + getCash().toLocaleString() + "</span>";
  160.         outpost.append(banner);
  161.     }
  162.  
  163.     // Main Function
  164.     function main() {
  165.         // Banner
  166.         let previous_cash = 0;
  167.         if (getCash() >= threshold && display_warning_banner) {
  168.             const outpost = document.getElementById("outpost");
  169.             refreshBanner(outpost);
  170.         }
  171.         // Test on interval, in case cash changes on outpost screen for whatever reason.
  172.         setInterval(function() {
  173.             if (getCash() >= threshold && display_warning_banner && previous_cash != getCash()) {
  174.                 refreshBanner(outpost);
  175.                 previous_cash = getCash();
  176.             }
  177.         }, 1000);
  178.         // Pop-up
  179.         waitForInnerCityButton(function() {
  180.             const innercity = innerCityButton();
  181.             if (display_popup_warning) {
  182.                 function innerCityEventListener(e) {
  183.                     if (getCash() >= threshold) {
  184.                         setWarningVisible(pageChangeInnerCityCallback(innercity));
  185.                     } else {
  186.                         pageChangeInnerCityCallback(innercity)();
  187.                     }
  188.                 }
  189.                 removeEventListeners(innercity);
  190.                 innercity.addEventListener("click", innerCityEventListener);
  191.                 innercity.addEventListener("auxclick", innerCityEventListener);
  192.             }
  193.         }, 2000);
  194.     }
  195.  
  196.     main();
  197. })();
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement