- type: ask
param: targetMessageCount
message: >-
📌 You can use this command or JS code as a base for creating other commands
or automations.
How many messages would you like to extract?
options:
- label: 50 messages
value: 50
- label: 100 messages
value: 100
- label: 200 messages
value: 200
- $custom
default: ''
vision:
enabled: false
mode: area
hint: ''
send: true
optionsInvalid: false
- type: js
args: targetMessageCount
code: |
async function scrollAndCollectMessages (targetMessageCount) {
const config = {
scroller: '[class*="scroller_"]',
messageItem: '[class^="messageListItem_"]',
timestamp: '[class^="timestamp_"] time',
username: '[class^="username_"]',
content: '[class^="markup_"][class*="messageContent_"]',
reply: '[class^="repliedMessage_"]',
}
async function collectMessages () {
const messages = []
const uniqueMessageIds = new Set()
let retries = 0
const maxRetries = 5
async function scrollAndWait () {
const elements = document.querySelectorAll(config.scroller)
const scrollableElement = elements[elements.length - 1]
if (scrollableElement) {
scrollableElement.scrollBy(0, -2000)
await new Promise(resolve => setTimeout(resolve, 200))
}
}
function extractCurrentMessages () {
const messageElements = document.querySelectorAll(config.messageItem)
let newMessagesFound = false
messageElements.forEach(message => {
const messageId =
message.id || message.getAttribute('data-list-item-id')
if (uniqueMessageIds.has(messageId)) return
const timestamp =
message.querySelector(config.timestamp)?.textContent || ''
const username =
message.querySelector(config.username)?.textContent || ''
const content =
message.querySelector(config.content)?.textContent || ''
const replyElement = message.querySelector(config.reply)
const replyTo = replyElement
? replyElement.querySelector(config.username)?.textContent || ''
: null
let formattedMessage = `${timestamp}: ${username}: ${content}`
if (replyTo) {
formattedMessage += ` (Replied to: ${replyTo})`
}
uniqueMessageIds.add(messageId)
messages.push(formattedMessage)
newMessagesFound = true
})
return newMessagesFound
}
while (messages.length < targetMessageCount && retries < maxRetries) {
const newMessagesFound = extractCurrentMessages()
if (messages.length < targetMessageCount) {
await scrollAndWait()
if (!newMessagesFound) {
retries++
} else {
retries = 0
}
}
}
if (messages.length > targetMessageCount) {
messages.splice(targetMessageCount)
}
return messages
}
try {
const messages = await collectMessages()
const url = window.location.href
return { messages, url }
} catch (error) {
return null
}
}
return scrollAndCollectMessages(targetMessageCount)
param: messages
timeout: 150000
silent: true
- type: say
message: |-
**Array:**
{{messages}}
This automation command is created by a community member. HARPA AI team does not audit community commands.
Please review the command carefully and only install if you trust the creator.
All rights reserved © HARPA AI TECHNOLOGIES LLC, 2021 — 2024
Designed and engineered in Finland 🇫🇮