Skip to content

Instantly share code, notes, and snippets.

@alshdavid
Created July 18, 2021 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alshdavid/1fd9d3e40a1a732aef63dc95a84b48e1 to your computer and use it in GitHub Desktop.
Save alshdavid/1fd9d3e40a1a732aef63dc95a84b48e1 to your computer and use it in GitHub Desktop.
(async () => {
document.querySelector('html').innerHTML = '<head></head><body>Loading...</body>'
async function loadTornEngine(url) {
const tornEngine = document.createElement('iframe')
const onload = new Promise(res => tornEngine.onload = res)
tornEngine.src = url
tornEngine.style.height = '600px'
tornEngine.style.width = '800px'
tornEngine.style.position = 'fixed'
tornEngine.style.bottom = '0'
tornEngine.style.right = '0'
document.body.appendChild(tornEngine)
await onload
// Find a better way to know Torn has loaded
await new Promise(res => setTimeout(res, 2000))
return { windowRef: tornEngine.contentWindow, dispose: () => document.body.removeChild(tornEngine) }
}
async function getUserID() {
const { windowRef, dispose } = await loadTornEngine(`/index.php`)
const { userID } = JSON.parse(windowRef.document.querySelector('#websocketConnectionData').innerHTML)
dispose()
return userID
}
async function getUserName() {
const userID = await getUserID()
const { windowRef, dispose } = await loadTornEngine(`/profiles.php?XID=${userID}`)
const userName = windowRef.document.querySelector('.info-table .user-info-value span').innerText.split('[')[0].trim()
dispose()
return userName
}
async function searchForCash() {
const { windowRef, dispose } = await loadTornEngine('/crimes.php#')
Array.from(windowRef.document.querySelectorAll('li')).filter(el => el.innerText.includes('Search for Cash'))[0].click()
windowRef.document.querySelector('.torn-btn').click()
await new Promise(res => setTimeout(res, 2000))
Array.from(windowRef.document.querySelectorAll('li')).filter(el => el.innerText.includes('Search the Train Station'))[0].click()
windowRef.document.querySelector('.torn-btn').click()
// Temporary pause to see if it worked
await new Promise(res => setTimeout(res, 5000))
dispose()
}
let username = await getUserName()
document.body.innerHTML = `
<h1>Hi ${username}</h1>
<button id="search-for-cash">Search for cash</button>
`
document.getElementById('search-for-cash').addEventListener('click', searchForCash)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment