Idle Pixel Audio Alerts

Audio Alerts for DHP

  1. // ==UserScript==
  2. // @name Idle Pixel Audio Alerts
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2.1
  5. // @description Audio Alerts for DHP
  6. // @author Felipe Dounford
  7. // @require https://gf.qytechs.cn/scripts/461221-hack-timer-js-by-turuslan/code/Hack%20Timerjs%20By%20Turuslan.js?version=1159560
  8. // @require https://gf.qytechs.cn/scripts/441206-idlepixel/code/IdlePixel+.js
  9. // @match *://idle-pixel.com/login/play*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=gf.qytechs.cn
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. //Change ding url to set default sound
  19. let ding = 'https://raw.githubusercontent.com/Dounford-Felipe/Audio-Alerts/main/ding.wav'
  20. //Change defaultText to set default TTS Text
  21. let defaultText = 'Ready'
  22. let dropdownPresets = {
  23. "Key Items": ["Treasure Map", "Green Treasure Map", "Red Treasure Map","Moon Distance","Sun Distance"],
  24. Geodes: ["Grey Geode", "Blue Geode", "Green Geode", "Red Geode", "Cyan Geode", "Ancient Geode"],
  25. Resources: ["Oil", "Stardust", "Energy"],
  26. "Potions Timers": ["Stardust Potion", "Anti Disease", "Tree Speed", "Great Stardust", "Farming Speed", "Super Stardust", "Unique Gathering", "Heat", "Bait", "Bone", "Promethium", "Ultra Stardust", "Rocket Speed", "Titanium", "Geode", "Stone Converter", "Combat Loot", "Rotten", "Merchant Speed", "Ancient Ore", "Guardian Key"],
  27. Timers: ["Rocket", "Rocket Idle", "Beehive", "Aquarium", "Birdhouse", "Cook Book", "Crystal Ball", "Event Upcomming", "Row Boat", "Canoe Boat", "Stardust Boat", "Farm Timer 1", "Farm Timer 2", "Farm Timer 3", "Farm Timer 4", "Farm Timer 5", "Tree Timer 1", "Tree Timer 2", "Tree Timer 3", "Tree Timer 4", "Tree Timer 5"],
  28. "Fight Points": ["Fields", "Forest", "Caves", "Volcano", "Northern Fields", "Haunted Mansion", "Beach", "Blood Fields", "Blood Forest", "Blood Caves", "Blood Volcano", "Guardian 1", "Guardian 2", "Guardian 3", "Guardian 4"]
  29. }
  30. let Presets = {
  31. "treasureMap": {
  32. "name": "treasure_map",
  33. "type": "eq",
  34. "value": "1"
  35. },
  36. "greenTreasureMap": {
  37. "name": "green_treasure_map",
  38. "type": "eq",
  39. "value": "1"
  40. },
  41. "redTreasureMap": {
  42. "name": "red_treasure_map",
  43. "type": "eq",
  44. "value": "1"
  45. },
  46. "moonDistance": {
  47. "name": "moon_distance",
  48. "type": "le",
  49. "value": "300000"
  50. },
  51. "sunDistance": {
  52. "name": "sun_distance",
  53. "type": "le",
  54. "value": "130000000"
  55. },
  56. "greyGeode": {
  57. "name": "grey_geode",
  58. "type": "gt",
  59. "value": "5"
  60. },
  61. "blueGeode": {
  62. "name": "blue_geode",
  63. "type": "gt",
  64. "value": "5"
  65. },
  66. "greenGeode": {
  67. "name": "green_geode",
  68. "type": "gt",
  69. "value": "5"
  70. },
  71. "redGeode": {
  72. "name": "red_geode",
  73. "type": "gt",
  74. "value": "5"
  75. },
  76. "cyanGeode": {
  77. "name": "cyan_geode",
  78. "type": "gt",
  79. "value": "5"
  80. },
  81. "ancientGeode": {
  82. "name": "ancient_geode",
  83. "type": "gt",
  84. "value": "5"
  85. },
  86. "oil": {
  87. "name": "oil",
  88. "type": "ge",
  89. "value": "25000"
  90. },
  91. "stardust": {
  92. "name": "stardust",
  93. "type": "ge",
  94. "value": "2000000"
  95. },
  96. "energy": {
  97. "name": "energy",
  98. "type": "lt",
  99. "value": "10000"
  100. },
  101. "stardustPotion": {
  102. "name": "stardust_potion_timer",
  103. "type": "le",
  104. "value": "10"
  105. },
  106. "antiDisease": {
  107. "name": "anti_disease_potion_timer",
  108. "type": "le",
  109. "value": "10"
  110. },
  111. "treeSpeed": {
  112. "name": "tree_speed_potion_timer",
  113. "type": "le",
  114. "value": "10"
  115. },
  116. "greatStardust": {
  117. "name": "great_stardust_potion_timer",
  118. "type": "le",
  119. "value": "10"
  120. },
  121. "farmingSpeed": {
  122. "name": "farming_speed_potion_timer",
  123. "type": "le",
  124. "value": "10"
  125. },
  126. "superStardust": {
  127. "name": "super_stardust_potion_timer",
  128. "type": "le",
  129. "value": "10"
  130. },
  131. "uniqueGathering": {
  132. "name": "gathering_unique_potion_timer",
  133. "type": "le",
  134. "value": "10"
  135. },
  136. "heat": {
  137. "name": "heat_potion_timer",
  138. "type": "le",
  139. "value": "10"
  140. },
  141. "bait": {
  142. "name": "bait_potion_timer",
  143. "type": "le",
  144. "value": "10"
  145. },
  146. "bone": {
  147. "name": "bone_potion_timer",
  148. "type": "le",
  149. "value": "10"
  150. },
  151. "promethium": {
  152. "name": "promethium_potion_timer",
  153. "type": "le",
  154. "value": "10"
  155. },
  156. "ultraStardust": {
  157. "name": "ultra_stardust_potion_timer",
  158. "type": "le",
  159. "value": "10"
  160. },
  161. "rocketSpeed": {
  162. "name": "rocket_potion_timer",
  163. "type": "le",
  164. "value": "10"
  165. },
  166. "titanium": {
  167. "name": "titanium_potion_timer",
  168. "type": "le",
  169. "value": "10"
  170. },
  171. "geode": {
  172. "name": "geode_potion_timer",
  173. "type": "le",
  174. "value": "10"
  175. },
  176. "stoneConverter": {
  177. "name": "stone_converter_potion_timer",
  178. "type": "le",
  179. "value": "10"
  180. },
  181. "combatLoot": {
  182. "name": "combat_loot_potion_timer",
  183. "type": "le",
  184. "value": "10"
  185. },
  186. "rotten": {
  187. "name": "rotten_potion_timer",
  188. "type": "le",
  189. "value": "10"
  190. },
  191. "merchantSpeed": {
  192. "name": "merchant_speed_potion_timer",
  193. "type": "le",
  194. "value": "10"
  195. },
  196. "ancientOre": {
  197. "name": "ancient_ore_potion_timer",
  198. "type": "le",
  199. "value": "10"
  200. },
  201. "guardianKey": {
  202. "name": "guardian_key_potion_timer",
  203. "type": "le",
  204. "value": "10"
  205. },
  206. "rocket": {
  207. "name": "rocket_km",
  208. "type": "eq",
  209. "value": "0"
  210. },
  211. "rocketIdle": {
  212. "name": "rocket_status",
  213. "type": "eq",
  214. "value": "none"
  215. },
  216. "beehive": {
  217. "name": "beehive_timer",
  218. "type": "eq",
  219. "value": "1"
  220. },
  221. "aquarium": {
  222. "name": "aquarium_timer",
  223. "type": "eq",
  224. "value": "0"
  225. },
  226. "birdhouse": {
  227. "name": "birdhouse_timer",
  228. "type": "eq",
  229. "value": "1"
  230. },
  231. "cookBook": {
  232. "name": "cooks_book_timer",
  233. "type": "eq",
  234. "value": "1"
  235. },
  236. "crystalBall": {
  237. "name": "crystal_ball_timer",
  238. "type": "eq",
  239. "value": "0"
  240. },
  241. "eventUpcomming": {
  242. "name": "event_upcomming_timer",
  243. "type": "ne",
  244. "value": "0"
  245. },
  246. "rowBoat": {
  247. "name": "row_boat_timer",
  248. "type": "eq",
  249. "value": "1"
  250. },
  251. "canoeBoat": {
  252. "name": "canoe_boat_timer",
  253. "type": "eq",
  254. "value": "1"
  255. },
  256. "stardustBoat": {
  257. "name": "stardust_boat_timer",
  258. "type": "eq",
  259. "value": "1"
  260. },
  261. "farmTimer1": {
  262. "name": "farm_timer_1",
  263. "type": "eq",
  264. "value": "1"
  265. },
  266. "farmTimer2": {
  267. "name": "farm_timer_2",
  268. "type": "eq",
  269. "value": "1"
  270. },
  271. "farmTimer3": {
  272. "name": "farm_timer_3",
  273. "type": "eq",
  274. "value": "1"
  275. },
  276. "farmTimer4": {
  277. "name": "farm_timer_4",
  278. "type": "eq",
  279. "value": "1"
  280. },
  281. "farmTimer5": {
  282. "name": "farm_timer_5",
  283. "type": "eq",
  284. "value": "1"
  285. },
  286. "treeTimer1": {
  287. "name": "tree_timer_1",
  288. "type": "eq",
  289. "value": "1"
  290. },
  291. "treeTimer2": {
  292. "name": "tree_timer_2",
  293. "type": "eq",
  294. "value": "1"
  295. },
  296. "treeTimer3": {
  297. "name": "tree_timer_3",
  298. "type": "eq",
  299. "value": "1"
  300. },
  301. "treeTimer4": {
  302. "name": "tree_timer_4",
  303. "type": "eq",
  304. "value": "1"
  305. },
  306. "treeTimer5": {
  307. "name": "tree_timer_5",
  308. "type": "eq",
  309. "value": "1"
  310. },
  311. "fields": {
  312. "name": "fight_points",
  313. "type": "ge",
  314. "value": "300"
  315. },
  316. "forest": {
  317. "name": "fight_points",
  318. "type": "ge",
  319. "value": "600"
  320. },
  321. "caves": {
  322. "name": "fight_points",
  323. "type": "ge",
  324. "value": "900"
  325. },
  326. "volcano": {
  327. "name": "fight_points",
  328. "type": "ge",
  329. "value": "1500"
  330. },
  331. "northernFields": {
  332. "name": "fight_points",
  333. "type": "ge",
  334. "value": "2000"
  335. },
  336. "hauntedMansion": {
  337. "name": "fight_points",
  338. "type": "ge",
  339. "value": "3500"
  340. },
  341. "beach": {
  342. "name": "fight_points",
  343. "type": "ge",
  344. "value": "5000"
  345. },
  346. "bloodFields": {
  347. "name": "fight_points",
  348. "type": "ge",
  349. "value": "1000"
  350. },
  351. "bloodForest": {
  352. "name": "fight_points",
  353. "type": "ge",
  354. "value": "2000"
  355. },
  356. "bloodCaves": {
  357. "name": "fight_points",
  358. "type": "ge",
  359. "value": "3500"
  360. },
  361. "bloodVolcano": {
  362. "name": "fight_points",
  363. "type": "ge",
  364. "value": "5000"
  365. },
  366. "guardian1": {
  367. "name": "fight_points",
  368. "type": "ge",
  369. "value": "4000"
  370. },
  371. "guardian2": {
  372. "name": "fight_points",
  373. "type": "ge",
  374. "value": "6000"
  375. },
  376. "guardian3": {
  377. "name": "fight_points",
  378. "type": "ge",
  379. "value": "10000"
  380. },
  381. "guardian4": {
  382. "name": "fight_points",
  383. "type": "ge",
  384. "value": "10000"
  385. }
  386. }
  387.  
  388.  
  389.  
  390. class AlertsPlugin extends IdlePixelPlusPlugin {
  391. constructor() {
  392. super("alerts", {
  393. about: {
  394. name: GM_info.script.name + " (ver: " + GM_info.script.version + ")",
  395. version: GM_info.script.version,
  396. author: GM_info.script.author,
  397. description: GM_info.script.description
  398. }
  399. });
  400. this.muteAllAlerts = false;
  401. this.alertVolume = 100;
  402. this.alertVoices = [];
  403. this.alertVoice = '';
  404. this.alerts = [];
  405. }
  406. onLogin() {
  407. IdlePixelPlus.plugins.alerts.addUI()
  408. IdlePixelPlus.plugins.alerts.loadAlerts()
  409. IdlePixelPlus.plugins.alerts.addDropdown()
  410. speechSynthesis.onvoiceschanged = function () {
  411. IdlePixelPlus.plugins.alerts.getVoices()
  412. }
  413. const alertLoopInterval = setInterval(function(){
  414. IdlePixelPlus.plugins.alerts.newValue()
  415. IdlePixelPlus.plugins.alerts.alertLoop()
  416. }, 1000);
  417. }
  418. //Gets the tts voices, populate the select with them and set the current voice
  419. getVoices() {
  420. IdlePixelPlus.plugins.alerts.alertVoices = speechSynthesis.getVoices();
  421. const voiceSelect = document.getElementById('ttsVoices');
  422. IdlePixelPlus.plugins.alerts.alertVoices.forEach((voice, index) => {
  423. const option = document.createElement('option');
  424. option.value = index;
  425. option.textContent = voice.name;
  426. voiceSelect.appendChild(option);
  427. });
  428. // Set the current voice based on the value stored in localStorage or use the first voice
  429. IdlePixelPlus.plugins.alerts.alertVoice = localStorage.getItem('audioAlertsVoice') ? IdlePixelPlus.plugins.alerts.alertVoices[localStorage.getItem('audioAlertsVoice')] : IdlePixelPlus.plugins.alerts.alertVoices[0]
  430. document.getElementById('ttsVoices').value = localStorage.getItem('audioAlertsVoice') ? localStorage.getItem('audioAlertsVoice') : 0
  431. }
  432.  
  433. //Adds the table and Style
  434. addUI() {
  435. if (document.querySelector('.dounModalDim') == null) {
  436. let style = document.createElement('style');
  437. style.innerHTML = `
  438. .dounModalParent {
  439. width: 100%;
  440. height: 100%;
  441. position: absolute;
  442. top: 0px;
  443. }
  444. .dounModalDim {
  445. background-color: black;
  446. opacity: 0.7;
  447. width: 100%;
  448. height: 100%;
  449. position: absolute;
  450. }
  451. .dounModalContent {
  452. margin-right: auto;
  453. margin-left: auto;
  454. width: 35%;
  455. border-radius: 5px;
  456. top: 100px;
  457. position: sticky;
  458. }`;
  459. document.head.appendChild(style);
  460. }
  461. let alertTable = `<table class="table table-secondary table-bordered table-striped" style="text-align: center;font-weight: bold;text-shadow: 1px 1px white;">
  462. <thead style="vertical-align: middle;">
  463. <tr>
  464. <th style="width: 30%;">Variable</th>
  465. <th style="width: 10%;" colspan="2">Trigger</th>
  466. <th style="width: 10%;">Current Value</th>
  467. <th style="width: 10%;">Sound Type</th>
  468. <th style="width: 20%;">Option</th>
  469. <th style="width: 10%;">Enabled</th>
  470. <th></th>
  471. </tr>
  472. </thead>
  473. <tbody id="alertsBody">
  474. </tbody>
  475. <tfoot>
  476. <tr id="alertsFooter">
  477. <td colspan="3">
  478. <select id="ttsVoices" onchange="IdlePixelPlus.plugins.alerts.alertVoice = IdlePixelPlus.plugins.alerts.alertVoices[this.value]" style="width:100%"></select>
  479. </td>
  480. <td colspan="2">
  481. <input type="checkbox" onclick="IdlePixelPlus.plugins.alerts.muteAllAlerts = !IdlePixelPlus.plugins.alerts.muteAllAlerts" style="margin-right: 10px;"> Mute ALL
  482. </td>
  483. <td>
  484. <input type="range" min="1" max="100" value="100" id="alertVolume" onchange="IdlePixelPlus.plugins.alerts.alertVolume = this.value"> Volume
  485. </td>
  486. <td>
  487. <button onclick="IdlePixelPlus.plugins.alerts.saveAlerts()">Save</button>
  488. </td>
  489. <td>
  490. <button onclick="IdlePixelPlus.plugins.alerts.addAlert()">ADD</button>
  491. </td>
  492. </tr>
  493. <tr>
  494. <td colspan="4">
  495. <button onclick="IdlePixelPlus.plugins.alerts.openImportModal()">Import Alerts</button>
  496. </td>
  497. <td colspan="4">
  498. <button onclick="IdlePixelPlus.plugins.alerts.exportAlerts()" style="margin-left: 30;">Export Alerts</button>
  499. </td>
  500. </tr>
  501. </tfoot>
  502. </table>
  503. <br>
  504. <br>
  505. <table class="table table-secondary table-borderless" style="text-align: center;text-shadow: 1px 1px white;width: 60%;margin: auto;border: 1px solid black;">
  506. <thead>
  507. <tr>
  508. <th style="width: 20%;">Preset Type</th>
  509. <th style="width: 20%;">Preset</th>
  510. <th style="width: 10%;"></th>
  511. </tr>
  512. </thead>
  513. <tbody>
  514. <tr>
  515. <td style="padding-bottom: 10px;">
  516. <select id="dropdownPresetsType" size="1">
  517. <option value="" selected="selected">Select Type</option>
  518. </select>
  519. </td>
  520. <td style="padding-bottom: 10px;">
  521. <select id="dropdownPresetsPreset" size="1">
  522. <option value="" selected="selected">Select Preset</option>
  523. </select>
  524. </td>
  525. <td style="padding-bottom: 10px;">
  526. <button onclick="IdlePixelPlus.plugins.alerts.addPreset()">ADD</button>
  527. </td>
  528. </tr>
  529. </tbody>
  530. </table>`
  531. IdlePixelPlus.addPanel("audioAlerts", "Audio Alerts", alertTable);
  532. let alertButton = `<div onclick="IdlePixelPlus.setPanel('audioAlerts')" class="hover hover-menu-bar-item left-menu-item">
  533. <table class="game-menu-bar-left-table-btn left-menu-item-other" style="width: 100%;">
  534. <tbody>
  535. <tr>
  536. <td style="width: 30px;"><img id="menu-bar-idlepixelplus-icon" src="https://dhm.idle-pixel.com/images/soundOn.png" class="w20" title="alerts"></td>
  537. <td>AUDIO ALERTS</td>
  538. </tr>
  539. </tbody>
  540. </table>
  541. </div>`
  542. document.getElementById('menu-bar-buttons').insertAdjacentHTML('beforeend', alertButton);
  543. let importAlertsModal = `<div class="dounModalParent" style="display:none" id="importAlertsModal">
  544. <div class="dounModalDim" onclick="document.getElementById('importAlertsModal').style.display='none'"></div>
  545. <div class="modal-content dounModalContent">
  546. <div class="modal-header">
  547. <h5 class="modal-title text-secondary">Import Alerts</h5>
  548. <button type="button" class="btn-close" onclick="document.getElementById('importAlertsModal').style.display = 'none'"></button>
  549. </div>
  550. <div class="modal-body" style="text-align: center;">
  551. <center>
  552. Enter Alerts JSON<br>
  553. <input type="text" id="importAlerts">
  554. </center>
  555. </div>
  556. <div class="modal-footer">
  557. <button onclick="document.getElementById('importAlertsModal').style.display = 'none'"><span class="font-pixel hover">Close</span></button>
  558. <button onclick="IdlePixelPlus.plugins.alerts.importAlerts()" class="background-primary"><span class="font-pixel hover">Import</span></button>
  559. </div>
  560. </div>
  561. </div>`
  562. document.getElementById('content').insertAdjacentHTML('beforeend', importAlertsModal)
  563. }
  564.  
  565. //Adds new alert row and a new key to alerts array
  566. addAlert = function() {
  567. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  568. let totalAlerts = alertRows.length
  569. let alertRow = document.createElement('tr')
  570. alertRow.id = `alert${totalAlerts+1}`
  571. alertRow.innerHTML = `<td>
  572. <input placeholder="Variable Name" id="variableName${totalAlerts+1}" style="width:100%">
  573. </td>
  574. <td>
  575. <select id="variableType${totalAlerts+1}">
  576. <option value="lt">&lt;</option>
  577. <option value="le">&le;</option>
  578. <option value="gt">&gt;</option>
  579. <option value="ge">&ge;</option>
  580. <option value="mod">&#37;</option>
  581. <option value="eq">&equals;</option>
  582. <option value="ne">&ne;</option>
  583. </select>
  584. </td>
  585. <td>
  586. <input placeholder="Value to Trigger" id="wantedValue${totalAlerts+1}">
  587. </td>
  588. <td><span id="variableValue${totalAlerts+1}"></span></td>
  589. <td>
  590. <select id="audioType${totalAlerts+1}">
  591. <option value="audio" selected="">Audio File</option>
  592. <option value="tts">Text To Speech</option>
  593. <option value="eval">Eval (Advanced Users Only!)</option>
  594. </select>
  595. </td>
  596. <td>
  597. <input placeholder="Text to Speech or sound URL" id="soundOption${totalAlerts+1}">
  598. </td>
  599. <td>
  600. <input type="checkbox" id="enabled${totalAlerts+1}">
  601. </td>
  602. <td style="padding-right: 6px;">
  603. <button onclick="IdlePixelPlus.plugins.alerts.removeAlert(this.parentNode.parentNode)">Delete</button>
  604. </td>`
  605. document.getElementById('alertsBody').append(alertRow)
  606. IdlePixelPlus.plugins.alerts.alerts[totalAlerts] = {type:'lt',variableName:'',wantedValue:'',soundType:'audio',sound:ding,enabled:false,triggered:false}
  607. }
  608.  
  609. //Remove alert row and the array key, also changes the id of the remaining rows
  610. removeAlert = function(row) {
  611. let oldRows = document.getElementById('alertsBody').getElementsByTagName("tr").length
  612. let newId = 1
  613. let id = row.id.slice(5)
  614. IdlePixelPlus.plugins.alerts.alerts.splice(id-1,1)
  615. row.remove()
  616. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  617. // Update remaining row IDs
  618. for (let i = 0; i < alertRows.length; i++) {alertRows[i].id = `alert${i+1}`}
  619. for (let i = 0; i <= oldRows; i++){
  620. if(document.getElementById("variableName"+i)){
  621. document.getElementById("variableName"+i).id = "variableName"+newId
  622. document.getElementById("variableType"+i).id = "variableType"+newId
  623. document.getElementById("wantedValue"+i).id = "wantedValue"+newId
  624. document.getElementById("variableValue"+i).id = "variableValue"+newId
  625. document.getElementById("audioType"+i).id = "audioType"+newId
  626. document.getElementById("soundOption"+i).id = "soundOption"+newId
  627. document.getElementById("enabled"+i).id = "enabled"+newId
  628. newId++
  629. }
  630. }
  631. // Add a new alert if there are no rows remaining
  632. if (alertRows.length == 0) {IdlePixelPlus.plugins.alerts.addAlert()}
  633. }
  634.  
  635. //Save the alerts, also sets the alerts, volume and current voice on localStorage
  636. saveAlerts = function() {
  637. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  638. for (let i = 0; i < alertRows.length; i++) {
  639. IdlePixelPlus.plugins.alerts.alerts[i].type = alertRows[i].getElementsByTagName('select')[0].value
  640. IdlePixelPlus.plugins.alerts.alerts[i].variableName = 'var_' + alertRows[i].getElementsByTagName('input')[0].value
  641. IdlePixelPlus.plugins.alerts.alerts[i].wantedValue = alertRows[i].getElementsByTagName('input')[1].value
  642. IdlePixelPlus.plugins.alerts.alerts[i].soundType = alertRows[i].getElementsByTagName('select')[1].value
  643. IdlePixelPlus.plugins.alerts.alerts[i].sound = alertRows[i].getElementsByTagName('input')[2].value == '' ? ding : alertRows[i].getElementsByTagName('input')[2].value
  644. IdlePixelPlus.plugins.alerts.alerts[i].enabled = alertRows[i].getElementsByTagName("input")[3].checked
  645. IdlePixelPlus.plugins.alerts.alerts[i].triggered = document.getElementById('alertsBody').getElementsByTagName("tr")[i].getElementsByTagName('td')[3].style.cssText == '--bs-table-accent-bg: yellow;' ? true : false;
  646. }
  647. let key = `audioAlerts`;
  648. localStorage.setItem(key, JSON.stringify(IdlePixelPlus.plugins.alerts.alerts));
  649. localStorage.setItem('audioAlertsVolume', IdlePixelPlus.plugins.alerts.alertVolume);
  650. let voiceIndex = document.getElementById('ttsVoices').value
  651. localStorage.setItem('audioAlertsVoice', voiceIndex);
  652. localStorage.setItem('audioAlertsMuted', IdlePixelPlus.plugins.alerts.muteAllAlerts);
  653. }
  654.  
  655. //Loads both volume and alerts from the localStorage
  656. loadAlerts() {
  657. let key = `audioAlerts`;
  658. let audioAlerts = localStorage.getItem(key);
  659. if (audioAlerts) {
  660. audioAlerts = JSON.parse(audioAlerts);
  661. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  662. for (let i = 0; i < audioAlerts.length; i++) {
  663. IdlePixelPlus.plugins.alerts.addAlert()
  664. alertRows[i].getElementsByTagName('select')[0].value = audioAlerts[i].type
  665. alertRows[i].getElementsByTagName('input')[0].value = audioAlerts[i].variableName.slice(4)
  666. alertRows[i].getElementsByTagName('input')[1].value = audioAlerts[i].wantedValue
  667. alertRows[i].getElementsByTagName('select')[1].value = audioAlerts[i].soundType
  668. alertRows[i].getElementsByTagName('input')[3].checked = audioAlerts[i].enabled
  669. alertRows[i].getElementsByTagName('input')[2].value = audioAlerts[i].sound == ding ? '' : audioAlerts[i].sound;
  670. alertRows[i].getElementsByTagName('td')[3].style.cssText = audioAlerts[i].triggered == true ? '--bs-table-accent-bg: yellow;' : ''
  671. }
  672. IdlePixelPlus.plugins.alerts.alerts = audioAlerts;
  673. } else {IdlePixelPlus.plugins.alerts.addAlert()}
  674. IdlePixelPlus.plugins.alerts.alertVolume = localStorage.getItem('audioAlertsVolume') ? localStorage.getItem('audioAlertsVolume') : 100;
  675. IdlePixelPlus.plugins.alerts.muteAllAlerts = localStorage.getItem('audioAlertsMuted') ? localStorage.getItem('audioAlertsMuted') : false;
  676. document.getElementById('alertVolume').value = IdlePixelPlus.plugins.alerts.alertVolume
  677. }
  678.  
  679. //Displays the current value of the alert variables
  680. newValue() {
  681. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  682. for (let i = 0; i < alertRows.length; i++) {
  683. alertRows[i].getElementsByTagName('span')[0].innerText = window[IdlePixelPlus.plugins.alerts.alerts[i].variableName] == undefined ? '' : window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]
  684. }
  685. }
  686.  
  687. //This is were the alert is checked
  688. alertLoop() {
  689. let alertNumber = IdlePixelPlus.plugins.alerts.alerts.length;
  690. for (let i = 0; i < alertNumber; i++) {
  691. if (IdlePixelPlus.plugins.alerts.alerts[i].enabled) {
  692. let type = IdlePixelPlus.plugins.alerts.alerts[i].type
  693. let triggered = 0
  694. switch(type) {
  695. case "lt": {
  696. triggered = parseInt(window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]) < parseInt(IdlePixelPlus.plugins.alerts.alerts[i].wantedValue) ? 1 : 0
  697. break;
  698. }
  699. case "le": {
  700. triggered = parseInt(window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]) <= parseInt(IdlePixelPlus.plugins.alerts.alerts[i].wantedValue) ? 1 : 0
  701. break;
  702. }
  703. case "gt": {
  704. triggered = parseInt(window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]) > parseInt(IdlePixelPlus.plugins.alerts.alerts[i].wantedValue) ? 1 : 0
  705. break;
  706. }
  707. case "ge": {
  708. triggered = parseInt(window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]) >= parseInt(IdlePixelPlus.plugins.alerts.alerts[i].wantedValue) ? 1 : 0
  709. break;
  710. }
  711. case "mod": {
  712. const [mod, remainder] = IdlePixelPlus.plugins.alerts.alerts[i].wantedValue.split(",");
  713. triggered = parseInt(window[IdlePixelPlus.plugins.alerts.alerts[i].variableName]) % parseInt(mod) == parseInt(remainder) ? 1 : 0
  714. break;
  715. }
  716. case "eq": {
  717. triggered = window[IdlePixelPlus.plugins.alerts.alerts[i].variableName] === IdlePixelPlus.plugins.alerts.alerts[i].wantedValue ? 1 : 0
  718. break;
  719. }
  720. case "ne": {
  721. triggered = window[IdlePixelPlus.plugins.alerts.alerts[i].variableName] !== IdlePixelPlus.plugins.alerts.alerts[i].wantedValue && typeof window[IdlePixelPlus.plugins.alerts.alerts[i].variableName] != 'undefined' ? 1 : 0
  722. break;
  723. }
  724. }
  725. if (triggered == 1 && IdlePixelPlus.plugins.alerts.alerts[i].triggered == false) {
  726. IdlePixelPlus.plugins.alerts.triggerAlert(i);
  727. }
  728. if (triggered == 0 && IdlePixelPlus.plugins.alerts.alerts[i].triggered == true) {
  729. IdlePixelPlus.plugins.alerts.alerts[i].triggered = false
  730. document.getElementById('alertsBody').getElementsByTagName("tr")[i].getElementsByTagName('td')[3].style.cssText=''
  731. }
  732. }
  733. }
  734. }
  735.  
  736. //This is were the alert happen
  737. triggerAlert(i) {
  738. IdlePixelPlus.plugins.alerts.alerts[i].triggered = true
  739. document.getElementById('alertsBody').getElementsByTagName("tr")[i].getElementsByTagName('td')[3].style.cssText = '--bs-table-accent-bg: yellow;';
  740. if (IdlePixelPlus.plugins.alerts.muteAllAlerts != true) {
  741. if(IdlePixelPlus.plugins.alerts.alerts[i].soundType == "audio") {
  742. let sound = new Audio(IdlePixelPlus.plugins.alerts.alerts[i].sound)
  743. sound = isNaN(sound.duration) ? new Audio(ding) : sound
  744. sound.volume = IdlePixelPlus.plugins.alerts.alertVolume / 100
  745. sound.play()
  746. } else if (IdlePixelPlus.plugins.alerts.alerts[i].soundType == "tts") {
  747. const message = new SpeechSynthesisUtterance();
  748. message.text = IdlePixelPlus.plugins.alerts.alerts[i].sound == ding ? defaultText : IdlePixelPlus.plugins.alerts.alerts[i].sound
  749. message.voice = IdlePixelPlus.plugins.alerts.alertVoice
  750. message.volume = IdlePixelPlus.plugins.alerts.alertVolume / 100
  751. window.speechSynthesis.speak(message);
  752. } else if (IdlePixelPlus.plugins.alerts.alerts[i].soundType == "eval") {// Remove from here
  753. let command = IdlePixelPlus.plugins.alerts.alerts[i].sound == 'https://raw.githubusercontent.com/Dounford-Felipe/Audio-Alerts/main/ding.wav' ? `console.log('You need to set ' + IdlePixelPlus.plugins.alerts.alerts[i].variableName + ' command')` : IdlePixelPlus.plugins.alerts.alerts[i].sound
  754. eval(command) // To here if you don't want eval
  755. }
  756. IdlePixelPlus.plugins.alerts.saveAlerts()
  757. }
  758. }
  759.  
  760. //Presets are added here
  761. addPreset() {
  762. IdlePixelPlus.plugins.alerts.addAlert()
  763. let alertRows = document.getElementById('alertsBody').getElementsByTagName("tr")
  764. let alertRow = alertRows.length - 1;
  765. let preset = document.getElementById("dropdownPresetsPreset").value;
  766. alertRows[alertRow].getElementsByTagName('select')[0].value = Presets[preset].type
  767. alertRows[alertRow].getElementsByTagName('input')[0].value = Presets[preset].name
  768. alertRows[alertRow].getElementsByTagName('input')[1].value = Presets[preset].value
  769. alertRows[alertRow].getElementsByTagName('input')[3].checked = true
  770. IdlePixelPlus.plugins.alerts.saveAlerts()
  771. }
  772. addDropdown() {
  773. let typeSel = document.getElementById("dropdownPresetsType");
  774. let fieldSel = document.getElementById("dropdownPresetsPreset");
  775. for (let type in dropdownPresets) {
  776. typeSel.options[typeSel.options.length] = new Option(type, type);
  777. }
  778. typeSel.onchange = function () {
  779. fieldSel.length = 1; // remove all options bar first
  780. if (this.selectedIndex < 1) return; // done
  781. let ft = dropdownPresets[this.value];
  782. for (let field in dropdownPresets[this.value]) {
  783. fieldSel.options[fieldSel.options.length] = new Option(ft[field], ft[field].charAt(0).toLocaleLowerCase() + ft[field].slice(1).replaceAll(' ',''));
  784. }
  785. }
  786. typeSel.onchange();
  787. }
  788. openImportModal() {
  789. document.getElementById('importAlertsModal').style.display=''
  790. }
  791. importAlerts() {
  792. let importedJSON = '';
  793. if (document.getElementById('importAlerts').value == '') {return} else {importedJSON = JSON.parse(document.getElementById('importAlerts').value)};
  794. let currentRows = document.getElementById('alertsBody').getElementsByTagName("tr").length;
  795. if (document.getElementById("variableName"+currentRows).value !== '') {currentRows++};
  796. importedJSON.forEach(function(alert){
  797. IdlePixelPlus.plugins.alerts.addAlert()
  798. document.getElementById("variableName"+currentRows).value = alert.variableName.slice(4)
  799. document.getElementById("variableType"+currentRows).value = alert.type
  800. document.getElementById("wantedValue"+currentRows).value = alert.wantedValue
  801. document.getElementById("audioType"+currentRows).value = alert.soundType
  802. document.getElementById("soundOption"+currentRows).value = alert.sound == ding ? '' : alert.sound;
  803. document.getElementById("variableValue"+currentRows).parentNode.cssText = alert.triggered == true ? '--bs-table-accent-bg: yellow;' : ''
  804. document.getElementById("enabled"+currentRows).checked = alert.enabled
  805. currentRows++
  806. })
  807. document.getElementById('importAlerts').value = ''
  808. document.getElementById('importAlertsModal').style.display='none';
  809. IdlePixelPlus.plugins.alerts.saveAlerts();
  810. }
  811. exportAlerts () {
  812. let saveData = JSON.stringify(IdlePixelPlus.plugins.alerts.alerts);
  813. const blob = new Blob([saveData], { type: "application/json" });
  814. const a = document.createElement("a");
  815. a.href = window.URL.createObjectURL(blob);
  816. a.download = 'DHP-Alerts-' + var_username;
  817. document.body.appendChild(a);
  818. a.click();
  819. document.body.removeChild(a);
  820. }
  821. }
  822. const plugin = new AlertsPlugin();
  823. IdlePixelPlus.registerPlugin(plugin);
  824. })();

QingJ © 2025

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