The3vilM0nk3y

autoInfiltrate.js

May 5th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const doc = eval('document')
  2.  
  3. /** @param {import(".").NS} ns */
  4. export async function main (ns) {
  5.   ns.tail()
  6.   if (ns.getPlayer().city != 'Aevum') {
  7.     if (!ns.travelToCity('Aevum')) { return ns.tprint('ERROR: Sorry, you need at least 200k to travel.') }
  8.   }
  9.   getCity().click()
  10.   let canceled = false
  11.   const cancelHook = function() {
  12.     const btn = [...doc.getElementsByTagName('button')].find(e => e.innerText === 'Cancel')
  13.     if (!btn) {
  14.       return
  15.     }
  16.     const oldFn = btn.onclick
  17.     btn.onclick = () => { canceled = true; oldFn() }
  18.   }
  19.   while (!canceled) {
  20.     let fail = false
  21.     getEcorp().click()
  22.     clickUntrusted('Infil')
  23.     //      clickUntrusted("Start");
  24.     console.log('***************************************************************Started')
  25.     while (!infiltrationComplete()) {
  26.       await ns.asleep(1000)
  27.       cancelHook()
  28.       if (getEcorp()) {
  29.         // booted to city!
  30.         fail = true
  31.         break
  32.       }
  33.       console.log('***************************************************************Waiting')
  34.     }
  35.     if (fail) {
  36.       continue
  37.     }
  38.  
  39.     // clickUntrusted("Sell");
  40.     console.log('***************************************************************Sell')
  41.     getButton('Sell').click()
  42.     await ns.sleep(1000)
  43.   }
  44. }
  45.  
  46. function getCity () {
  47.   for (const elem of doc.querySelectorAll('p')) {
  48.     if (elem.textContent == 'City') {
  49.       return elem
  50.     }
  51.   }
  52. }
  53.  
  54. function getEcorp () {
  55.   return doc.querySelector('[aria-label="ECorp"]')
  56. }
  57.  
  58. function getButton (text) {
  59.   for (const elem of doc.querySelectorAll('button')) {
  60.     if (elem.textContent.toLowerCase().includes(text.toLowerCase())) {
  61.       return elem
  62.     }
  63.   }
  64. }
  65.  
  66. function clickUntrusted (text) {
  67.   const button = getButton(text)
  68.   const handler = Object.keys(button)[1]
  69.   button[handler].onClick({ isTrusted: true })
  70. }
  71.  
  72. function infiltrationComplete () {
  73.   return [...doc.querySelectorAll('h4')].some(e => e.innerText === 'Infiltration successful!')
  74. }
  75.  
Add Comment
Please, Sign In to add comment