-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
147 lines (116 loc) · 4.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { WebcastPushConnection } = require('tiktok-live-connector')
const puppeteer = require('puppeteer-core');
(async () => {
let removeLastLikeTimeout = null
const injectUsername = async (user, x, y) => {
await page.evaluate((user, x, y, removeLastLikeTimeout) => {
const existingDiv = document.getElementById('user-like-div')
if (existingDiv) {
document.body.removeChild(existingDiv)
}
const div = document.createElement('div')
div.id = 'user-like-div'
div.style.position = 'absolute'
div.style.top = `${y}px`
div.style.left = `${x}px`
div.style.padding = '5px'
div.style.display = 'flex'
div.style.alignItems = 'center'
const img = document.createElement('img')
img.src = user.profilePictureUrl
img.style.width = '30px'
img.style.height = '30px'
img.style.borderRadius = '50%'
img.style.marginRight = '10px'
const text = document.createElement('span')
text.innerText = user.nickname
div.appendChild(img)
div.appendChild(text)
document.body.appendChild(div)
// Clear the timeout if another user clicks before 1 second
if (removeLastLikeTimeout) {
clearTimeout(removeLastLikeTimeout)
}
// Remove the text element after 1 second if no other clicks
removeLastLikeTimeout = setTimeout(() => {
const lastDiv = document.getElementById('user-like-div')
if (lastDiv) {
document.body.removeChild(lastDiv)
}
}, 1000)
}, user, x, y, removeLastLikeTimeout)
};
// Username of someone who is currently live
let tiktokUsername = "leebrucce"
// Create a new wrapper object and pass the username
let tiktokLiveConnection = new WebcastPushConnection(tiktokUsername)
// Connect to the chat (await can be used as well)
tiktokLiveConnection.connect().then(state => {
console.info(`Connected to roomId ${state.roomId}`)
}).catch(err => {
console.error('Failed to connect', err.message)
})
// // Define the events that you want to handle
// // In this case we listen to chat messages (comments)
// tiktokLiveConnection.on('chat', data => {
// console.log(`${data.uniqueId} writes: ${data.comment}`)
// })
// // And here we receive gifts sent to the streamer
// tiktokLiveConnection.on('gift', data => {
// console.log(`${data.uniqueId} (userId:${data.userId}) sends ${data.giftId}`)
// })
tiktokLiveConnection.on('like', data => {
console.log(`${data.uniqueId} sent ${data.likeCount} likes, total likes: ${data.totalLikeCount}`)
console.log(data);
clickCenter(data.likeCount, data)
})
tiktokLiveConnection.on('error', err => {
console.error('Error!', err.message)
})
const browserURL = 'http://localhost:9222'
// Connect to the remote Chrome instance
const browser = await puppeteer.connect({
browserURL
})
// https://games.crazygames.com/en_US/capybara-clicker/index.html
const pages = await browser.pages()
const page = pages[0]
console.log(page, page.url());
// Wait for the page to load completely
await page.waitForSelector('canvas') // Assuming the game is rendered in a canvas element
// Get the bounding box of the element you want to click
const element = await page.$('#unity-canvas')
const boundingBox = await element.boundingBox()
// Calculate the center of the element
const x = boundingBox.x + boundingBox.width / 2 - 100
const y = boundingBox.y + boundingBox.height / 2
const clickCenter = async (amount, user, timeout = 150) => {
const click = () => new Promise(resolve => {
setTimeout(async () => {
await page.mouse.click(x, y)
await injectUsername(user, x, y);
resolve()
}, timeout)
})
for (let i = 0; i < amount; i++) {
await click()
}
};
function exitHandler(options, exitCode) {
if (exitCode || exitCode === 0) console.log(exitCode)
if (options.exit) {
console.log('Cleaning up before exit...')
tiktokLiveConnection?.disconnect()
process.exit()
}
}
// do something when app is closing
process.on('exit', exitHandler.bind(null, { exit: true }))
// catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { exit: true }))
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))
// catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))
})();