forked from fair/matrix-puppeteer-line
formating & add the missing function
This commit is contained in:
parent
703a08c84d
commit
7a10061384
@ -79,7 +79,7 @@ export default class MessagesPuppeteer {
|
|||||||
const args = [
|
const args = [
|
||||||
`--disable-extensions-except=${MessagesPuppeteer.extensionDir}`,
|
`--disable-extensions-except=${MessagesPuppeteer.extensionDir}`,
|
||||||
`--load-extension=${MessagesPuppeteer.extensionDir}`,
|
`--load-extension=${MessagesPuppeteer.extensionDir}`,
|
||||||
`--window-size=${MessagesPuppeteer.viewport.width},${MessagesPuppeteer.viewport.height+120}`,
|
`--window-size=${MessagesPuppeteer.viewport.width},${MessagesPuppeteer.viewport.height + 120}`,
|
||||||
]
|
]
|
||||||
if (MessagesPuppeteer.noSandbox) {
|
if (MessagesPuppeteer.noSandbox) {
|
||||||
args.push(`--no-sandbox`)
|
args.push(`--no-sandbox`)
|
||||||
@ -196,7 +196,7 @@ export default class MessagesPuppeteer {
|
|||||||
* @param {string} text - The text to input.
|
* @param {string} text - The text to input.
|
||||||
*/
|
*/
|
||||||
async _enterText(inputElement, text) {
|
async _enterText(inputElement, text) {
|
||||||
await inputElement.click({clickCount: 3})
|
await inputElement.click({ clickCount: 3 })
|
||||||
await inputElement.type(text)
|
await inputElement.type(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +218,7 @@ export default class MessagesPuppeteer {
|
|||||||
const qrButton = await this.page.waitForSelector("#login_qr_btn")
|
const qrButton = await this.page.waitForSelector("#login_qr_btn")
|
||||||
await qrButton.click()
|
await qrButton.click()
|
||||||
|
|
||||||
const qrElement = await this.page.waitForSelector("#login_qrcode_area div[title]", {visible: true})
|
const qrElement = await this.page.waitForSelector("#login_qrcode_area div[title]", { visible: true })
|
||||||
const currentQR = await this.page.evaluate(element => element.title, qrElement)
|
const currentQR = await this.page.evaluate(element => element.title, qrElement)
|
||||||
this._receiveQRChange(currentQR)
|
this._receiveQRChange(currentQR)
|
||||||
|
|
||||||
@ -239,7 +239,7 @@ export default class MessagesPuppeteer {
|
|||||||
const emailButton = await this.page.waitForSelector("#login_email_btn")
|
const emailButton = await this.page.waitForSelector("#login_email_btn")
|
||||||
await emailButton.click()
|
await emailButton.click()
|
||||||
|
|
||||||
await this.page.waitForSelector("#login_email_area", {visible: true})
|
await this.page.waitForSelector("#login_email_area", { visible: true })
|
||||||
this.login_email = login_data["email"]
|
this.login_email = login_data["email"]
|
||||||
this.login_password = login_data["password"]
|
this.login_password = login_data["password"]
|
||||||
await this._sendEmailCredentials()
|
await this._sendEmailCredentials()
|
||||||
@ -280,12 +280,12 @@ export default class MessagesPuppeteer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await Promise.race([
|
const result = await Promise.race([
|
||||||
() => this.page.waitForSelector("#mainApp:not(.MdNonDisp)", {timeout: 2000})
|
() => this.page.waitForSelector("#mainApp:not(.MdNonDisp)", { timeout: 2000 })
|
||||||
.then(value => {
|
.then(value => {
|
||||||
loginSuccess = true
|
loginSuccess = true
|
||||||
return value
|
return value
|
||||||
}),
|
}),
|
||||||
() => this.page.waitForSelector("#login_incorrect", {visible: true, timeout: 2000})
|
() => this.page.waitForSelector("#login_incorrect", { visible: true, timeout: 2000 })
|
||||||
.then(value => this.page.evaluate(element => element?.innerText, value)),
|
.then(value => this.page.evaluate(element => element?.innerText, value)),
|
||||||
() => this._waitForLoginCancel(),
|
() => this._waitForLoginCancel(),
|
||||||
].map(promiseFn => cancelableResolve(promiseFn)))
|
].map(promiseFn => cancelableResolve(promiseFn)))
|
||||||
@ -320,7 +320,7 @@ export default class MessagesPuppeteer {
|
|||||||
&& (text.endsWith("100%") || text.endsWith("NaN%"))
|
&& (text.endsWith("100%") || text.endsWith("NaN%"))
|
||||||
// TODO Sometimes it gets stuck at 99%...??
|
// TODO Sometimes it gets stuck at 99%...??
|
||||||
},
|
},
|
||||||
{timeout: 10000}, // Assume 10 seconds is long enough
|
{ timeout: 10000 }, // Assume 10 seconds is long enough
|
||||||
messageSyncElement)
|
messageSyncElement)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
//this._sendLoginFailure(`Failed to sync: ${err}`)
|
//this._sendLoginFailure(`Failed to sync: ${err}`)
|
||||||
@ -582,7 +582,7 @@ export default class MessagesPuppeteer {
|
|||||||
// - the most recently-sent own message is not fully read
|
// - the most recently-sent own message is not fully read
|
||||||
let chatIDToSync
|
let chatIDToSync
|
||||||
for (let i = 0, n = chatList.length; i < n; i++) {
|
for (let i = 0, n = chatList.length; i < n; i++) {
|
||||||
const chatListItem = chatList[(i+offset) % n]
|
const chatListItem = chatList[(i + offset) % n]
|
||||||
|
|
||||||
if (chatListItem.notificationCount > 0) {
|
if (chatListItem.notificationCount > 0) {
|
||||||
// Chat has unread notifications, so don't view it
|
// Chat has unread notifications, so don't view it
|
||||||
@ -693,8 +693,8 @@ export default class MessagesPuppeteer {
|
|||||||
await this._interactWithPage(async () => {
|
await this._interactWithPage(async () => {
|
||||||
this.log("Opening settings view")
|
this.log("Opening settings view")
|
||||||
await this.page.click("button.mdGHD01SettingBtn")
|
await this.page.click("button.mdGHD01SettingBtn")
|
||||||
await this.page.waitForSelector("#context_menu li#settings", {visible: true}).then(e => e.click())
|
await this.page.waitForSelector("#context_menu li#settings", { visible: true }).then(e => e.click())
|
||||||
await this.page.waitForSelector("#settings_contents", {visible: true})
|
await this.page.waitForSelector("#settings_contents", { visible: true })
|
||||||
|
|
||||||
this.log("Getting own profile info")
|
this.log("Getting own profile info")
|
||||||
ownProfile = {
|
ownProfile = {
|
||||||
@ -712,7 +712,7 @@ export default class MessagesPuppeteer {
|
|||||||
|
|
||||||
const backSelector = "#label_setting button"
|
const backSelector = "#label_setting button"
|
||||||
await this.page.click(backSelector)
|
await this.page.click(backSelector)
|
||||||
await this.page.waitForSelector(backSelector, {visible: false})
|
await this.page.waitForSelector(backSelector, { visible: false })
|
||||||
})
|
})
|
||||||
return ownProfile
|
return ownProfile
|
||||||
}
|
}
|
||||||
@ -729,11 +729,14 @@ export default class MessagesPuppeteer {
|
|||||||
return `#joined_group_list_body > li[data-chatid="${id}"]`
|
return `#joined_group_list_body > li[data-chatid="${id}"]`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_contactCountSelector() {
|
||||||
|
return `#contact_wrap_friends .MdLFT04Head._contactlist_header span[id=contact_friend_count]`
|
||||||
|
}
|
||||||
|
|
||||||
async _getWholeContactResults(page, distance) {
|
async _getWholeContactResults(page, distance) {
|
||||||
await page.bringToFront()
|
await page.bringToFront()
|
||||||
await page.waitForSelector("#contact_wrap_friends > ul.MdCMN03List")
|
await page.waitForSelector("#contact_wrap_friends > ul.MdCMN03List")
|
||||||
const results = []
|
const contactTotalCount = 0
|
||||||
const contactTotalCount =0
|
|
||||||
this.log(` distance is ${distance}`)
|
this.log(` distance is ${distance}`)
|
||||||
let incred = 0
|
let incred = 0
|
||||||
const element = await page.$(this._contactCountSelector())
|
const element = await page.$(this._contactCountSelector())
|
||||||
@ -742,6 +745,7 @@ export default class MessagesPuppeteer {
|
|||||||
return Number.parseInt(element?.innerText) || 0
|
return Number.parseInt(element?.innerText) || 0
|
||||||
})
|
})
|
||||||
this.log(` contact_friend_count is ${foundContactCount}`)
|
this.log(` contact_friend_count is ${foundContactCount}`)
|
||||||
|
//infiniting contact list scrolling
|
||||||
while (incred <= distance) {
|
while (incred <= distance) {
|
||||||
if (foundContactCount <= contactTotalCount) {
|
if (foundContactCount <= contactTotalCount) {
|
||||||
return
|
return
|
||||||
@ -757,7 +761,7 @@ export default class MessagesPuppeteer {
|
|||||||
const $lis = await page.$$("#contact_wrap_friends > ul.MdCMN03List >li")
|
const $lis = await page.$$("#contact_wrap_friends > ul.MdCMN03List >li")
|
||||||
//lookup lazy loading count
|
//lookup lazy loading count
|
||||||
const lis = $lis.slice(contactTotalCount, Math.Infinity)
|
const lis = $lis.slice(contactTotalCount, Math.Infinity)
|
||||||
contactTotalCount=$lis.length
|
contactTotalCount = $lis.length
|
||||||
}
|
}
|
||||||
|
|
||||||
async _switchChat(chatID, forceView = false) {
|
async _switchChat(chatID, forceView = false) {
|
||||||
@ -822,7 +826,7 @@ export default class MessagesPuppeteer {
|
|||||||
const leg = parseInt(ulstyleheight, 10)
|
const leg = parseInt(ulstyleheight, 10)
|
||||||
this.log(`found contact_wrap_friends height is ${leg}px`)
|
this.log(`found contact_wrap_friends height is ${leg}px`)
|
||||||
this.log(`starting to scroll to buttom`)
|
this.log(`starting to scroll to buttom`)
|
||||||
const results = await this._getWholeContactResults(this.page, leg)
|
await this._getWholeContactResults(this.page, leg)
|
||||||
|
|
||||||
this.log(`finished to scroll to buttom`)
|
this.log(`finished to scroll to buttom`)
|
||||||
}
|
}
|
||||||
@ -859,7 +863,7 @@ export default class MessagesPuppeteer {
|
|||||||
this.log(`Waiting for chat header title to be "${chatName}"`)
|
this.log(`Waiting for chat header title to be "${chatName}"`)
|
||||||
await this.page.waitForFunction(
|
await this.page.waitForFunction(
|
||||||
isCorrectChatVisible,
|
isCorrectChatVisible,
|
||||||
{polling: "mutation", timeout: 1000},
|
{ polling: "mutation", timeout: 1000 },
|
||||||
chatName)
|
chatName)
|
||||||
})
|
})
|
||||||
if (switchedTabs) {
|
if (switchedTabs) {
|
||||||
@ -879,13 +883,13 @@ export default class MessagesPuppeteer {
|
|||||||
this.log("Clicking chat header to show detail area")
|
this.log("Clicking chat header to show detail area")
|
||||||
await this.page.click("#_chat_header_area > .mdRGT04Link")
|
await this.page.click("#_chat_header_area > .mdRGT04Link")
|
||||||
this.log("Waiting for detail area")
|
this.log("Waiting for detail area")
|
||||||
await this.page.waitForSelector("#_chat_detail_area > .mdRGT02Info", {timeout: 1000})
|
await this.page.waitForSelector("#_chat_detail_area > .mdRGT02Info", { timeout: 1000 })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
this.log("Waiting for any item to appear in chat")
|
this.log("Waiting for any item to appear in chat")
|
||||||
try {
|
try {
|
||||||
await this.page.waitForSelector("#_chat_room_msg_list div", {timeout: 2000})
|
await this.page.waitForSelector("#_chat_room_msg_list div", { timeout: 2000 })
|
||||||
|
|
||||||
this.log("Waiting for chat to stabilize")
|
this.log("Waiting for chat to stabilize")
|
||||||
await this.page.evaluate(() => window.__mautrixController.waitForMessageListStability())
|
await this.page.evaluate(() => window.__mautrixController.waitForMessageListStability())
|
||||||
@ -906,7 +910,7 @@ export default class MessagesPuppeteer {
|
|||||||
|
|
||||||
async _getChatInfoUnsafe(chatID, forceView) {
|
async _getChatInfoUnsafe(chatID, forceView) {
|
||||||
// TODO Commonize this
|
// TODO Commonize this
|
||||||
let [isDirect, isGroup, isRoom] = [false,false,false]
|
let [isDirect, isGroup, isRoom] = [false, false, false]
|
||||||
switch (chatID.charAt(0)) {
|
switch (chatID.charAt(0)) {
|
||||||
case "u":
|
case "u":
|
||||||
isDirect = true
|
isDirect = true
|
||||||
@ -978,7 +982,7 @@ export default class MessagesPuppeteer {
|
|||||||
for (const participant of participants) {
|
for (const participant of participants) {
|
||||||
this.log(JSON.stringify(participant))
|
this.log(JSON.stringify(participant))
|
||||||
}
|
}
|
||||||
return {participants, ...chatListInfo}
|
return { participants, ...chatListInfo }
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Catch "An error has occurred" dialog
|
// TODO Catch "An error has occurred" dialog
|
||||||
@ -1005,7 +1009,7 @@ export default class MessagesPuppeteer {
|
|||||||
await input.press("Enter")
|
await input.press("Enter")
|
||||||
await this.page.waitForFunction(
|
await this.page.waitForFunction(
|
||||||
e => e.innerText == "",
|
e => e.innerText == "",
|
||||||
{timeout: 500},
|
{ timeout: 500 },
|
||||||
input)
|
input)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -1097,14 +1101,14 @@ export default class MessagesPuppeteer {
|
|||||||
// Sync receipts seen from newly-synced messages
|
// Sync receipts seen from newly-synced messages
|
||||||
// TODO When user leaves, clear the read-by count for the old number of other participants
|
// TODO When user leaves, clear the read-by count for the old number of other participants
|
||||||
let minCountToFind = 1
|
let minCountToFind = 1
|
||||||
for (let i = messages.length-1; i >= 0; i--) {
|
for (let i = messages.length - 1; i >= 0; i--) {
|
||||||
const message = messages[i]
|
const message = messages[i]
|
||||||
if (!message.is_outgoing) {
|
if (!message.is_outgoing) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const count = message.receipt_count
|
const count = message.receipt_count
|
||||||
if (count >= minCountToFind && message.id > (receiptMap.get(count) || 0)) {
|
if (count >= minCountToFind && message.id > (receiptMap.get(count) || 0)) {
|
||||||
minCountToFind = count+1
|
minCountToFind = count + 1
|
||||||
receiptMap.set(count, message.id)
|
receiptMap.set(count, message.id)
|
||||||
}
|
}
|
||||||
// TODO Early exit when count == num other participants
|
// TODO Early exit when count == num other participants
|
||||||
@ -1236,7 +1240,7 @@ export default class MessagesPuppeteer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _syncChat(chatID) {
|
async _syncChat(chatID) {
|
||||||
const {messages, receipts} = await this._getMessagesUnsafe(chatID)
|
const { messages, receipts } = await this._getMessagesUnsafe(chatID)
|
||||||
|
|
||||||
if (messages.length == 0) {
|
if (messages.length == 0) {
|
||||||
this.log("No new messages found in", chatID)
|
this.log("No new messages found in", chatID)
|
||||||
@ -1273,7 +1277,7 @@ export default class MessagesPuppeteer {
|
|||||||
|
|
||||||
this.log(`Received read receipt ${receipt_id} (since ${prevReceiptID}) for chat ${chat_id}`)
|
this.log(`Received read receipt ${receipt_id} (since ${prevReceiptID}) for chat ${chat_id}`)
|
||||||
if (this.client) {
|
if (this.client) {
|
||||||
this.client.sendReceipt({chat_id: chat_id, id: receipt_id})
|
this.client.sendReceipt({ chat_id: chat_id, id: receipt_id })
|
||||||
.catch(err => this.error("Error handling read receipt:", err))
|
.catch(err => this.error("Error handling read receipt:", err))
|
||||||
} else {
|
} else {
|
||||||
this.log("No client connected, not sending receipts")
|
this.log("No client connected, not sending receipts")
|
||||||
@ -1306,7 +1310,7 @@ export default class MessagesPuppeteer {
|
|||||||
receipt.chat_id = chat_id
|
receipt.chat_id = chat_id
|
||||||
try {
|
try {
|
||||||
await this.client.sendReceipt(receipt)
|
await this.client.sendReceipt(receipt)
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
this.error("Error handling read receipt:", err)
|
this.error("Error handling read receipt:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user