Advertisement
XeiDaMoKa

Torn Chain Warner

Feb 16th, 2024 (edited)
1,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.58 KB | None | 0 0
  1. // ==UserScript==
  2. // @name         Torn Chain Warner
  3. // @version      0.1
  4. // @description  Check the timer and apply a flashing shadow based on the time remaining
  5. // @author       XeiDaMoKa
  6. // @match        https://www.torn.com/*
  7. // ==/UserScript==
  8.  
  9. // jQuery
  10. /* global $ */
  11.  
  12. // Change colors, times, and audio settings
  13. const settings = {
  14.     color1: { color: 'green', time: '1:45' },
  15.     color2: { color: 'orange', time: '1:00' },
  16.     color3: { color: 'red', time: '0:30' },
  17.     audio: { audioURL: 'https://dl.sndup.net/v6zq/XeiTornMusic.mp3', time: '1:42' } // 1:42 lenght of the music
  18. };
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. // Elements with visuals glowing
  26. const elements = [
  27.     '#header-root',
  28.     '#sidebarroot',
  29.     '.content-wrapper',
  30. ];
  31.  
  32. // Create an Audio element
  33. let audio = new Audio(settings.audio.audioURL);
  34.  
  35. // Check if the sidebar timer (needs change to check if theres at least 10 hits because the timer is always present)
  36. function checkTimer() {
  37.     let timerElement = $(".bar-timeleft___B9RGV");
  38.  
  39.     // If the regular timer element is not found, try finding the combat page timer
  40.     if (timerElement.length === 0) {
  41.         timerElement = $(".labelTitle___ZtfnD span span");
  42.     }
  43.  
  44.     // If neither timer element is found, stop the script
  45.     if (timerElement.length === 0) {
  46.         return;
  47.     }
  48.  
  49.     const totalSeconds = parseTime(timerElement.text());
  50.     const audioStartOffset = parseTime(settings.audio.time);
  51.  
  52.     // Calculate the remaining time before the audio should start
  53.     const remainingTime = totalSeconds - audioStartOffset;
  54.  
  55.     // Check if the remaining time is below zero or above 1:42 (lenthgs of the audio), meaning the audio should start now at certain point
  56.     if (remainingTime <= 0) {
  57.         if (audio.paused) {
  58.             audio.currentTime = audioStartOffset - totalSeconds;
  59.             audio.play();
  60.         }
  61.     } else {
  62.         if (!audio.paused) {
  63.             audio.pause();
  64.         }
  65.     }
  66.  
  67.     // Loop through the colors depending on the timer
  68.     if (totalSeconds > parseTime(settings.color1.time)) {
  69.         changeColor('transparent');
  70.     } else if (totalSeconds > parseTime(settings.color2.time)) {
  71.         changeColor(settings.color1.color);
  72.     } else if (totalSeconds > parseTime(settings.color3.time)) {
  73.         changeColor(settings.color2.color);
  74.     } else {
  75.         changeColor(settings.color3.color);
  76.     }
  77. }
  78.  
  79.  
  80. // Visuals Style
  81. function changeColor(color) {
  82.     $('#breathe-animation').remove();
  83.  
  84.     if (color !== 'transparent') {
  85.         const style = $('<style>')
  86.             .attr('id', 'breathe-animation')
  87.             .html(`
  88.                 @keyframes breathe {
  89.                     0% { box-shadow: 0 0 5px ${color}; }
  90.                     20% { box-shadow: 0 0 25px ${color}, 0 0 25px ${color}; }
  91.                     50% { box-shadow: 0 0 5px ${color}; }
  92.                     80% { box-shadow: 0 0 25px ${color}, 0 0 25px ${color}; }
  93.                     100% { box-shadow: 0 0 5px ${color}; }
  94.                 }
  95.             `);
  96.  
  97.         // Breathing animation
  98.         $('head').append(style);
  99.         for (const selector of elements) {
  100.             $(selector).css('animation', 'breathe 5s infinite');
  101.         }
  102.     } else {
  103.         // Remove transparent animation above 1:42
  104.         for (const selector of elements) {
  105.             $(selector).css('animation', 'none');
  106.         }
  107.     }
  108. }
  109.  
  110. // Function to parse mm:ss into seconds
  111. function parseTime(timeStr) {
  112.     var [minutes, seconds] = timeStr.split(':').map(Number);
  113.     return minutes * 60 + seconds;
  114. }
  115.  
  116. // Check timer
  117. setInterval(checkTimer, 1000);
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement