2021-03-15 01:40:56 -04:00
|
|
|
// matrix-puppeteer-line - A very hacky Matrix-LINE bridge based on running LINE's Chrome extension in Puppeteer
|
2021-02-26 01:28:54 -05:00
|
|
|
// Copyright (C) 2020-2021 Tulir Asokan, Andrew Ferrazzutti
|
2020-08-17 15:03:10 -04:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Definitions and docs for methods that the Puppeteer script exposes for the content script
|
|
|
|
/**
|
|
|
|
* @param {string} text - The date string to parse
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {?Date} ref - Reference date to parse relative times
|
|
|
|
* @param {?{forwardDate: boolean}} option - Extra options for parser
|
2020-08-17 15:03:10 -04:00
|
|
|
* @return {Promise<Date>}
|
|
|
|
*/
|
2020-08-24 15:24:19 -04:00
|
|
|
window.__chronoParseDate = function (text, ref, option) {}
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-06-10 02:10:18 -04:00
|
|
|
* @param {ChatListInfo[]} changes - The chats that changed.
|
2020-08-24 15:24:19 -04:00
|
|
|
* @return {Promise<void>}
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
2020-08-24 15:24:19 -04:00
|
|
|
window.__mautrixReceiveChanges = function (changes) {}
|
2021-04-23 03:38:13 -04:00
|
|
|
/**
|
|
|
|
* @param {string} messages - The ID of the chat receiving messages.
|
|
|
|
* @param {MessageData[]} messages - The messages added to a chat.
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
window.__mautrixReceiveMessages = function (chatID, messages) {}
|
2021-04-20 20:01:50 -04:00
|
|
|
/**
|
2021-06-05 23:41:05 -04:00
|
|
|
* @param {string} chatID - The ID of the chat whose receipts are being processed.
|
|
|
|
* @param {string} receipt_id - The ID of the most recently-read message for the current chat.
|
2021-04-20 20:01:50 -04:00
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2021-06-30 03:08:08 -04:00
|
|
|
window.__mautrixReceiveReceiptDirectLatest = function (chatID, receiptID) {}
|
2021-04-20 20:01:50 -04:00
|
|
|
/**
|
2021-06-05 23:41:05 -04:00
|
|
|
* @param {string} chatID - The ID of the chat whose receipts are being processed.
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {Receipt[]} receipts - All newly-seen receipts for the current chat.
|
2021-04-20 20:01:50 -04:00
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2021-06-30 03:08:08 -04:00
|
|
|
window.__mautrixReceiveReceiptMulti = function (chatID, receipts) {}
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL for the QR code.
|
2020-08-24 15:24:19 -04:00
|
|
|
* @return {Promise<void>}
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
2020-08-24 15:24:19 -04:00
|
|
|
window.__mautrixReceiveQR = function (url) {}
|
2021-02-10 02:34:19 -05:00
|
|
|
/**
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
window.__mautrixSendEmailCredentials = function () {}
|
|
|
|
/**
|
|
|
|
* @param {string} pin - The login PIN.
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
window.__mautrixReceivePIN = function (pin) {}
|
|
|
|
/**
|
|
|
|
* @param {Element} button - The button to click when a QR code or PIN expires.
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
window.__mautrixExpiry = function (button) {}
|
2021-05-30 17:41:28 -04:00
|
|
|
/**
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
window.__mautrixLoggedOut = function() {}
|
2021-06-06 18:15:38 -04:00
|
|
|
|
2021-02-25 22:21:11 -05:00
|
|
|
/**
|
2021-06-06 18:15:38 -04:00
|
|
|
* typedef ChatTypeEnum
|
2021-02-25 22:21:11 -05:00
|
|
|
*/
|
|
|
|
const ChatTypeEnum = Object.freeze({
|
|
|
|
DIRECT: 1,
|
|
|
|
GROUP: 2,
|
|
|
|
ROOM: 3,
|
|
|
|
})
|
2020-08-17 15:03:10 -04:00
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
const MSG_DECRYPTING = "ⓘ Decrypting..."
|
2021-06-30 03:08:59 -04:00
|
|
|
|
|
|
|
// TODO add more common selectors
|
|
|
|
const SEL_PARTICIPANTS_LIST = "#_chat_detail_area > .mdRGT02Info ul.mdRGT13Ul"
|
2021-06-06 18:15:38 -04:00
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
class MautrixController {
|
2021-04-27 02:59:16 -04:00
|
|
|
constructor() {
|
2020-08-17 15:03:10 -04:00
|
|
|
this.chatListObserver = null
|
2021-04-20 20:01:50 -04:00
|
|
|
this.msgListObserver = null
|
2021-04-23 03:38:13 -04:00
|
|
|
this.receiptObserver = null
|
2021-04-27 02:59:16 -04:00
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
this.qrChangeObserver = null
|
|
|
|
this.qrAppearObserver = null
|
|
|
|
this.emailAppearObserver = null
|
|
|
|
this.pinAppearObserver = null
|
2021-02-25 22:21:11 -05:00
|
|
|
this.ownID = null
|
2021-04-27 02:59:16 -04:00
|
|
|
|
2021-07-02 13:58:36 -04:00
|
|
|
this.ownMsgPromise = Promise.resolve(-1)
|
2021-04-27 02:59:16 -04:00
|
|
|
this._promiseOwnMsgReset()
|
2021-02-25 22:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setOwnID(ownID) {
|
2021-06-15 02:48:29 -04:00
|
|
|
this.ownID = ownID
|
2021-02-25 22:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Commonize with Node context
|
|
|
|
getChatType(id) {
|
|
|
|
switch (id.charAt(0)) {
|
|
|
|
case "u":
|
|
|
|
return ChatTypeEnum.DIRECT
|
|
|
|
case "c":
|
|
|
|
return ChatTypeEnum.GROUP
|
|
|
|
case "r":
|
|
|
|
return ChatTypeEnum.ROOM
|
|
|
|
default:
|
|
|
|
throw `Invalid chat ID: ${id}`
|
|
|
|
}
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 03:38:13 -04:00
|
|
|
getCurrentChatID() {
|
2021-04-20 20:01:50 -04:00
|
|
|
const chatListElement = document.querySelector("#_chat_list_body > .ExSelected > .chatList")
|
2021-04-23 03:38:13 -04:00
|
|
|
return chatListElement ? this.getChatListItemID(chatListElement) : null
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
|
|
|
* Parse a date string.
|
|
|
|
*
|
|
|
|
* @param {string} text - The string to parse
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {?Date} ref - Reference date to parse relative times
|
|
|
|
* @param {?{forwardDate: boolean}} option - Extra options for parser
|
2021-04-06 01:56:46 -04:00
|
|
|
* @return {Promise<?Date>} - The date, or null if parsing failed.
|
2020-08-17 15:03:10 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2020-08-28 09:38:06 -04:00
|
|
|
async _tryParseDate(text, ref, option) {
|
|
|
|
const parsed = await window.__chronoParseDate(text, ref, option)
|
2021-02-20 20:00:32 -05:00
|
|
|
return parsed ? new Date(parsed) : null
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 23:56:54 -04:00
|
|
|
* Parse a date separator.
|
2020-08-17 15:03:10 -04:00
|
|
|
*
|
2021-06-05 23:56:54 -04:00
|
|
|
* @param {string} text - The text in the date saparator.
|
|
|
|
* @return {Promise<?Date>} - The value of the date separator.
|
2020-08-17 15:03:10 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2021-06-05 23:56:54 -04:00
|
|
|
async _tryParseDateSeparator(text) {
|
2020-08-17 15:03:10 -04:00
|
|
|
if (!text) {
|
|
|
|
return null
|
|
|
|
}
|
2021-06-05 23:56:54 -04:00
|
|
|
// Must prefix with midnight to prevent getting noon
|
|
|
|
text = "00:00 " + text.replace(/\. /, "/")
|
2020-08-17 15:03:10 -04:00
|
|
|
const now = new Date()
|
|
|
|
let newDate = await this._tryParseDate(text)
|
|
|
|
if (!newDate || newDate > now) {
|
|
|
|
const lastWeek = new Date()
|
|
|
|
lastWeek.setDate(lastWeek.getDate() - 7)
|
|
|
|
newDate = await this._tryParseDate(text, lastWeek, { forwardDate: true })
|
|
|
|
}
|
2021-02-20 20:00:32 -05:00
|
|
|
return newDate && newDate <= now ? newDate : null
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef MessageData
|
|
|
|
* @type {object}
|
2020-08-18 09:47:06 -04:00
|
|
|
* @property {number} id - The ID of the message. Seems to be sequential.
|
2021-07-05 02:37:14 -04:00
|
|
|
* @property {?number} timestamp - The unix timestamp of the message. Accurate to the minute.
|
2020-08-18 09:47:06 -04:00
|
|
|
* @property {boolean} is_outgoing - Whether or not this user sent the message.
|
2021-04-05 03:54:35 -04:00
|
|
|
* @property {?Participant} sender - Full data of the participant who sent the message, if needed and available.
|
|
|
|
* @property {?string} html - The HTML format of the message, if necessary.
|
2021-06-06 18:15:38 -04:00
|
|
|
* @property {?ImageInfo} image - Information of the image in the message, if it's an image-only message.
|
2021-07-05 02:37:14 -04:00
|
|
|
* @property {?MemberInfo} member_info - Change to the membership status of a participant.
|
2021-06-30 03:08:08 -04:00
|
|
|
* @property {?number} receipt_count - The number of users who have read the message.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
|
|
|
|
2021-07-05 02:37:14 -04:00
|
|
|
/**
|
|
|
|
* @typedef MemberInfo
|
|
|
|
* @type {object}
|
|
|
|
* @property {boolean} invited
|
|
|
|
* @property {boolean} joined
|
|
|
|
* @property {boolean} left
|
|
|
|
* TODO Any more? How about kicked?
|
|
|
|
*/
|
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
/**
|
|
|
|
* @typedef ImageInfo
|
|
|
|
* @type {object}
|
|
|
|
* @property {string} url - The URL of the image's location.
|
|
|
|
* @property {boolean} is_sticker - Whether the sent image is a sticker.
|
|
|
|
* @property {boolean} animated - Whether the sent image is animated. Only used for stickers (for now...?).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a URL points to a loaded image or not.
|
|
|
|
*
|
|
|
|
* @param {string} src
|
2021-07-05 01:25:51 -04:00
|
|
|
* @return {boolean}
|
2021-06-06 18:15:38 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2021-03-29 01:25:05 -04:00
|
|
|
_isLoadedImageURL(src) {
|
2021-06-06 18:15:38 -04:00
|
|
|
return src && (
|
|
|
|
src.startsWith(`blob:`) ||
|
|
|
|
src.startsWith(`${document.location.origin}/res/`) && !src.startsWith(`${document.location.origin}/res/img/noimg/`))
|
2021-03-29 01:25:05 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 02:37:14 -04:00
|
|
|
/**
|
|
|
|
* Strip dimension values from an image URL, if needed.
|
|
|
|
*
|
|
|
|
* @param {string} src
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
_getComparableImageURL(src) {
|
|
|
|
return this._isLoadedImageURL(src) ? src : src.replace(/\d+x\d+/, "-x-")
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to match a Participant against an entry in the friends list,
|
|
|
|
* and set any unset properties of the Participant based on the matched item.
|
|
|
|
* Match on name first (since it's always available), then on avatar and ID (since there
|
|
|
|
* may be multiple matching names).
|
|
|
|
*
|
|
|
|
* @param {Participant} participant - The Participant to find a match for, and set properties of.
|
|
|
|
* @return {boolean} - Whether or not a match was found.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateSenderFromFriendsList(participant) {
|
|
|
|
let targetElement
|
|
|
|
const elements = document.querySelectorAll(`#contact_wrap_friends > ul > li[title='${participant.name}']`)
|
|
|
|
if (elements.length == 0) {
|
|
|
|
return false
|
|
|
|
} else if (elements.length == 1) {
|
|
|
|
targetElement = elements[0]
|
|
|
|
} else if (participant.avatar) {
|
|
|
|
const url = this._getComparableImageURL(participant.avatar.url)
|
|
|
|
// Look for multiple matching avatars, just in case.
|
|
|
|
// Could reasonably happen with "noimg" placeholder avatars.
|
|
|
|
const filteredElements = elements.filter(element => {
|
|
|
|
const pathImg = this.getFriendsListItemAvatar(element)
|
|
|
|
return pathImg && this._getComparableImageURL(pathImg.url) == url
|
|
|
|
})
|
|
|
|
if (filteredElements.length == 1) {
|
|
|
|
targetElement = filteredElements[0]
|
|
|
|
} else if (filteredElements.length != 0) {
|
|
|
|
elements = filteredElements
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!targetElement && participant.id) {
|
|
|
|
const idElement = elements.find(element => this.getFriendsListItemID(element) == participant.id)
|
|
|
|
if (idElement) {
|
|
|
|
targetElement = idElement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!targetElement) {
|
|
|
|
targetElement = elements[0]
|
|
|
|
console.warn(`Multiple matching friends found for "${participant.name}", so using first match`)
|
|
|
|
}
|
|
|
|
if (!participant.avatar) {
|
|
|
|
participant.avatar = this.getFriendsListItemAvatar(targetElement)
|
|
|
|
}
|
|
|
|
if (!participant.id) {
|
|
|
|
participant.id = this.getFriendsListItemID(targetElement)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to match a Participant against an entry in the current chat's participant list,
|
|
|
|
* and set any unset properties of the Participant based on the matched item.
|
|
|
|
* Match on name first (since it's always available), then on avatar and ID (since there
|
|
|
|
* may be multiple matching names).
|
|
|
|
*
|
|
|
|
* @param {Participant} participant - The Participant to find a match for, and set properties of.
|
|
|
|
* @return {boolean} - Whether or not a match was found.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateSenderFromParticipantList(participant) {
|
|
|
|
let targetElement
|
|
|
|
const participantsList = document.querySelector(SEL_PARTICIPANTS_LIST)
|
|
|
|
// Groups use a participant's name as the alt text of their avatar image,
|
|
|
|
// but rooms do not...ARGH! But they both use a dedicated element for it.
|
|
|
|
const elements =
|
|
|
|
Array.from(participantsList.querySelectorAll(".mdRGT13Ttl"))
|
|
|
|
.filter(e => e.innerText == participant.name)
|
|
|
|
.map(e => e.parentElement)
|
|
|
|
if (elements.length == 0) {
|
|
|
|
return false
|
|
|
|
} else if (elements.length == 1) {
|
|
|
|
targetElement = elements[0]
|
|
|
|
} else if (participant.avatar) {
|
|
|
|
const url = this._getComparableImageURL(participant.avatar.url)
|
|
|
|
// Look for multiple matching avatars, just in case.
|
|
|
|
// Could reasonably happen with "noimg" placeholder avatars.
|
|
|
|
const filteredElements = elements.filter(element => {
|
|
|
|
const pathImg = this.getParticipantListItemAvatar(element)
|
|
|
|
return pathImg && this._getComparableImageURL(pathImg.url) == url
|
|
|
|
})
|
|
|
|
if (filteredElements.length == 1) {
|
|
|
|
targetElement = filteredElements[0]
|
|
|
|
} else if (filteredElements.length != 0) {
|
|
|
|
elements = filteredElements
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!targetElement && participant.id) {
|
|
|
|
// This won't work for rooms, where participant list items don't have IDs,
|
|
|
|
// but keep this around in case they ever do...
|
|
|
|
const idElement = elements.find(element => this.getParticipantListItemID(element) == participant.id)
|
|
|
|
if (idElement) {
|
|
|
|
targetElement = idElement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO Look at the list of invited participants if no match found
|
|
|
|
|
|
|
|
if (!targetElement) {
|
|
|
|
targetElement = elements[0]
|
|
|
|
console.warn(`Multiple matching participants found for "${participant.name}", so using first match`)
|
|
|
|
}
|
|
|
|
if (!participant.avatar) {
|
|
|
|
participant.avatar = this.getParticipantListItemAvatar(targetElement)
|
|
|
|
}
|
|
|
|
if (!participant.id) {
|
|
|
|
participant.id = this.getParticipantListItemID(targetElement)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the friends/participant list to update a Participant's information.
|
|
|
|
* Try the friends list first since the particpant list for rooms doesn't have user IDs...
|
|
|
|
*
|
|
|
|
* @param {Participant} participant - The participant whose information should be updated.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateSenderFromMatch(participant) {
|
|
|
|
if (!this._updateSenderFromFriendsList(participant)) {
|
|
|
|
if (!this._updateSenderFromParticipantList(participant)) {
|
|
|
|
console.warn(`No matching item found for "${participant.name}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-06-06 18:15:38 -04:00
|
|
|
* Parse a message element.
|
2020-08-17 15:03:10 -04:00
|
|
|
*
|
|
|
|
* @param {Element} element - The message element.
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {number} chatType - What kind of chat this message is part of.
|
2021-06-06 18:15:38 -04:00
|
|
|
* @param {Date} refDate - The most recent date indicator. If undefined, do not retrieve the timestamp of this message.
|
|
|
|
* @return {Promise<MessageData>}
|
2020-08-17 15:03:10 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2021-06-06 18:15:38 -04:00
|
|
|
async _parseMessage(element, chatType, refDate) {
|
2021-02-25 22:21:11 -05:00
|
|
|
const is_outgoing = element.classList.contains("mdRGT07Own")
|
2021-07-05 02:37:14 -04:00
|
|
|
let sender
|
2021-02-25 22:21:11 -05:00
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
const receipt = element.querySelector(".mdRGT07Own .mdRGT07Read:not(.MdNonDisp)")
|
|
|
|
let receipt_count
|
|
|
|
|
2021-02-25 22:21:11 -05:00
|
|
|
// Don't need sender ID for direct chats, since the portal will have it already.
|
|
|
|
if (chatType == ChatTypeEnum.DIRECT) {
|
|
|
|
sender = null
|
2021-04-20 20:01:50 -04:00
|
|
|
receipt_count = is_outgoing ? (receipt ? 1 : 0) : null
|
2021-02-25 22:21:11 -05:00
|
|
|
} else if (!is_outgoing) {
|
2021-07-05 02:37:14 -04:00
|
|
|
sender = {
|
|
|
|
name: element.querySelector(".mdRGT07Body > .mdRGT07Ttl").innerText,
|
|
|
|
avatar: this._getPathImage(element.querySelector(".mdRGT07Img > img"))
|
2021-02-25 22:21:11 -05:00
|
|
|
}
|
2021-07-05 02:37:14 -04:00
|
|
|
this._updateSenderFromMatch(sender)
|
2021-04-20 20:01:50 -04:00
|
|
|
receipt_count = null
|
2021-02-25 22:21:11 -05:00
|
|
|
} else {
|
|
|
|
// TODO Get own ID and store it somewhere appropriate.
|
|
|
|
// Unable to get own ID from a room chat...
|
|
|
|
// if (chatType == ChatTypeEnum.GROUP) {
|
|
|
|
// const participantsList = document.querySelector("#_chat_detail_area > .mdRGT02Info ul.mdRGT13Ul")
|
|
|
|
// // TODO The first member is always yourself, right?
|
|
|
|
// // TODO Cache this so own ID can be used later
|
|
|
|
// sender = participantsList.children[0].getAttribute("data-mid")
|
|
|
|
// }
|
2021-06-30 03:08:59 -04:00
|
|
|
const participantsList = document.querySelector(SEL_PARTICIPANTS_LIST)
|
2021-07-05 02:37:14 -04:00
|
|
|
sender = {
|
|
|
|
name: this.getParticipantListItemName(participantsList.children[0]),
|
|
|
|
avatar: this.getParticipantListItemAvatar(participantsList.children[0]),
|
|
|
|
id: this.ownID
|
|
|
|
}
|
2021-04-20 20:01:50 -04:00
|
|
|
receipt_count = receipt ? this._getReceiptCount(receipt) : null
|
2021-02-25 22:21:11 -05:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
const messageData = {
|
2021-02-20 20:00:32 -05:00
|
|
|
id: +element.getAttribute("data-local-id"),
|
2021-06-06 18:15:38 -04:00
|
|
|
timestamp:
|
|
|
|
refDate !== undefined
|
|
|
|
? (await this._tryParseDate(element.querySelector("time")?.innerText, refDate))?.getTime()
|
|
|
|
: null,
|
2021-02-25 22:21:11 -05:00
|
|
|
is_outgoing: is_outgoing,
|
|
|
|
sender: sender,
|
2021-06-06 18:15:38 -04:00
|
|
|
receipt_count: receipt_count,
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
|
2021-02-20 20:00:32 -05:00
|
|
|
const messageElement = element.querySelector(".mdRGT07Body > .mdRGT07Msg")
|
2021-06-06 18:15:38 -04:00
|
|
|
const is_sticker = messageElement.classList.contains("mdRGT07Sticker")
|
2021-02-20 20:00:32 -05:00
|
|
|
if (messageElement.classList.contains("mdRGT07Text")) {
|
2021-06-06 18:15:38 -04:00
|
|
|
let msgSpan = messageElement.querySelector(".mdRGT07MsgTextInner")
|
|
|
|
try {
|
|
|
|
if (msgSpan.innerHTML == MSG_DECRYPTING) {
|
|
|
|
msgSpan = await this._waitForDecryptedMessage(element, msgSpan, 5000)
|
|
|
|
}
|
|
|
|
messageData.html = await this._parseMessageHTML(msgSpan)
|
|
|
|
} catch {
|
|
|
|
// Throw to reject, but return what was parsed so far
|
|
|
|
throw messageData
|
|
|
|
}
|
|
|
|
} else if (is_sticker || messageElement.classList.contains("mdRGT07Image")) {
|
|
|
|
// TODO Animated non-sticker images require clicking its img element, which is just a thumbnail
|
|
|
|
// Real image: "#wrap_single_image img"
|
|
|
|
// Close button: "#wrap_single_image button"
|
|
|
|
// Viewer is open/closed based on "#wrap_single_image.MdNonDisp" / "#wrap_single_image:not(.MdNonDisp)"
|
|
|
|
let img = messageElement.querySelector(".mdRGT07MsgImg > img")
|
|
|
|
if (!this._isLoadedImageURL(img.src)) {
|
|
|
|
try {
|
|
|
|
img = await this._waitForLoadedImage(img, 10000)
|
|
|
|
} catch {
|
|
|
|
// Throw to reject, but return what was parsed so far
|
|
|
|
throw messageData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messageData.image = {
|
|
|
|
url: img.src,
|
|
|
|
is_sticker: is_sticker,
|
|
|
|
is_animated: is_sticker && img.parentElement.classList.contains("animationSticker"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messageData
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Element} msgSpan
|
2021-07-05 01:25:51 -04:00
|
|
|
* @return {Promise<DOMString>}
|
2021-06-06 18:15:38 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _parseMessageHTML(msgSpan) {
|
|
|
|
const msgSpanImgs = msgSpan.getElementsByTagName("img")
|
|
|
|
if (msgSpanImgs.length == 0) {
|
|
|
|
return msgSpan.innerHTML
|
|
|
|
} else {
|
|
|
|
const unloadedImgs = Array.from(msgSpanImgs).filter(img => !this._isLoadedImageURL(img.src))
|
|
|
|
if (unloadedImgs.length > 0) {
|
|
|
|
// NOTE Use allSettled to not throw if any images time out
|
|
|
|
await Promise.allSettled(
|
|
|
|
unloadedImgs.map(img => this._waitForLoadedImage(img, 2000))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack to put sticon dimensions in HTML (which are excluded by default)
|
|
|
|
// in such a way that doesn't alter the elements that are in the DOM
|
|
|
|
const msgSpanCopy = msgSpan.cloneNode(true)
|
|
|
|
const msgSpanCopyImgs = msgSpanCopy.getElementsByTagName("img")
|
|
|
|
for (let i = 0, n = msgSpanImgs.length; i < n; i++) {
|
|
|
|
msgSpanCopyImgs[i].height = msgSpanImgs[i].height
|
|
|
|
msgSpanCopyImgs[i].width = msgSpanImgs[i].width
|
|
|
|
}
|
|
|
|
return msgSpanCopy.innerHTML
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Element} element
|
|
|
|
* @param {Element} msgSpan
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {number} timeoutLimitMillis
|
2021-06-06 18:15:38 -04:00
|
|
|
* @return {Promise<Element>}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_waitForDecryptedMessage(element, msgSpan, timeoutLimitMillis) {
|
|
|
|
console.debug("Wait for message element to finish decrypting")
|
|
|
|
console.debug(element)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let observer = new MutationObserver(changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
const isTextUpdate = change.type == "characterData"
|
|
|
|
const target = isTextUpdate ? msgSpan : element.querySelector(".mdRGT07MsgTextInner")
|
|
|
|
if (target && target.innerHTML != MSG_DECRYPTING) {
|
|
|
|
if (isTextUpdate) {
|
|
|
|
console.debug("UNLIKELY(?) EVENT -- Found decrypted message from text update")
|
|
|
|
} else {
|
|
|
|
// TODO Looks like it's div.mdRGT07Body that gets always replaced. If so, watch only for that
|
|
|
|
console.debug("Found decrypted message from element replacement")
|
|
|
|
console.debug(target)
|
|
|
|
console.debug("Added:")
|
|
|
|
for (const change of changes) {
|
|
|
|
console.debug(change.removedNodes)
|
|
|
|
}
|
|
|
|
console.debug("Removed:")
|
|
|
|
for (const change of changes) {
|
|
|
|
console.debug(change.addedNodes)
|
|
|
|
}
|
2021-03-29 01:25:05 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
observer.disconnect()
|
|
|
|
observer = null
|
|
|
|
resolve(target)
|
|
|
|
return
|
2021-03-29 01:25:05 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
if (target && target != msgSpan) {
|
|
|
|
console.debug("UNLIKELY EVENT -- Somehow added a new \"decrypting\" span, it's the one to watch now")
|
|
|
|
console.debug(target)
|
|
|
|
msgSpan = target
|
|
|
|
observer.observe(msgSpan, { characterData: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Either the span element or one of its ancestors is replaced,
|
|
|
|
// or the span element's content is updated.
|
|
|
|
// Not exactly sure which of these happens, or if the same kind
|
|
|
|
// of mutation always happens, so just look for them all...
|
|
|
|
observer.observe(element, { childList: true, subtree: true })
|
|
|
|
observer.observe(msgSpan, { characterData: true })
|
|
|
|
setTimeout(() => {
|
|
|
|
if (observer) {
|
|
|
|
observer.disconnect()
|
|
|
|
// Don't print log message, as this may be a safe timeout
|
|
|
|
reject()
|
|
|
|
}
|
|
|
|
}, timeoutLimitMillis)
|
|
|
|
})
|
|
|
|
}
|
2021-03-29 01:25:05 -04:00
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
/**
|
|
|
|
* @param {Element} img
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {number} timeoutLimitMillis
|
2021-06-06 18:15:38 -04:00
|
|
|
* @return {Promise<Element>}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_waitForLoadedImage(img, timeoutLimitMillis) {
|
|
|
|
console.debug("Wait for image element to finish loading")
|
|
|
|
console.debug(img)
|
|
|
|
// TODO Should reject on "#_chat_message_image_failure"
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let observer = new MutationObserver(changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
if (this._isLoadedImageURL(change.target.src)) {
|
|
|
|
console.debug("Image element finished loading")
|
|
|
|
console.debug(change.target)
|
|
|
|
observer.disconnect()
|
|
|
|
observer = null
|
|
|
|
resolve(change.target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
observer.observe(img, { attributes: true, attributeFilter: ["src"] })
|
|
|
|
setTimeout(() => {
|
|
|
|
if (observer) {
|
2021-03-29 01:25:05 -04:00
|
|
|
observer.disconnect()
|
2021-06-06 18:15:38 -04:00
|
|
|
// Don't print log message, as this may be a safe timeout
|
|
|
|
reject()
|
2021-03-28 04:23:07 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
}, timeoutLimitMillis)
|
|
|
|
})
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
/**
|
|
|
|
* Find the number in the "Read #" receipt message.
|
|
|
|
* Don't look for "Read" specifically, to support multiple languages.
|
|
|
|
*
|
|
|
|
* @param {Element} receipt - The element containing the receipt message.
|
2021-07-05 01:25:51 -04:00
|
|
|
* @return {number}
|
2021-04-20 20:01:50 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getReceiptCount(receipt) {
|
|
|
|
const match = receipt.innerText.match(/\d+/)
|
|
|
|
return Number.parseInt(match ? match[0] : 0) || null
|
|
|
|
}
|
|
|
|
|
2021-02-20 03:47:26 -05:00
|
|
|
|
2021-07-05 02:37:14 -04:00
|
|
|
/**
|
|
|
|
* Parse a member event element.
|
|
|
|
*
|
|
|
|
* @param {Element} element - The message element.
|
|
|
|
* @return {?MessageData} - A valid MessageData with member_info set, or null if no membership info is found.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_tryParseMemberEvent(element) {
|
|
|
|
const memberMatch = element.querySelector("time.preline")?.innerText?.match(/(.*) (joined|left)/)
|
|
|
|
if (memberMatch) {
|
|
|
|
const sender = {name: memberMatch[1]}
|
|
|
|
this._updateSenderFromMatch(sender)
|
|
|
|
return {
|
|
|
|
id: +element.getAttribute("data-local-id"),
|
|
|
|
is_outgoing: false,
|
|
|
|
sender: sender,
|
|
|
|
member_info: {
|
|
|
|
invited: false, // TODO Handle invites. Its puppet must not auto-join, though!
|
|
|
|
joined: memberMatch[2] == "joined",
|
|
|
|
left: memberMatch[2] == "left",
|
|
|
|
// TODO Any more? How about kicked?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-27 02:59:16 -04:00
|
|
|
/**
|
|
|
|
* Create and store a promise that resolves when a message written
|
|
|
|
* by the user finishes getting sent.
|
|
|
|
* Accepts selectors for elements that become visible once the message
|
|
|
|
* has succeeded or failed to be sent.
|
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {number} timeoutLimitMillis - The maximum amount of time to wait for the message to be sent.
|
2021-06-06 18:15:38 -04:00
|
|
|
* @param {string} successSelector - The selector for the element that indicates the message was sent.
|
|
|
|
* @param {?string} failureSelector - The selector for the element that indicates the message failed to be sent.
|
2021-04-27 02:59:16 -04:00
|
|
|
*/
|
2021-03-29 01:25:05 -04:00
|
|
|
promiseOwnMessage(timeoutLimitMillis, successSelector, failureSelector=null) {
|
2021-04-27 02:59:16 -04:00
|
|
|
this.promiseOwnMsgSuccessSelector = successSelector
|
|
|
|
this.promiseOwnMsgFailureSelector = failureSelector
|
2021-02-20 03:47:26 -05:00
|
|
|
|
2021-04-27 02:59:16 -04:00
|
|
|
this.ownMsgPromise = new Promise((resolve, reject) => {
|
|
|
|
this.promiseOwnMsgResolve = resolve
|
|
|
|
this.promiseOwnMsgReject = reject
|
2020-08-28 12:34:13 -04:00
|
|
|
})
|
2021-06-06 18:15:38 -04:00
|
|
|
this.promiseOwnMsgTimeoutID = setTimeout(() => {
|
|
|
|
if (this.promiseOwnMsgReject) {
|
|
|
|
console.error("Timed out waiting for own message to be sent")
|
|
|
|
this._rejectOwnMessage()
|
|
|
|
}
|
|
|
|
}, timeoutLimitMillis)
|
2020-08-28 12:34:13 -04:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:58:36 -04:00
|
|
|
/**
|
|
|
|
* Check if we're waiting for a Matrix-sent message to resolve.
|
2021-07-05 01:25:51 -04:00
|
|
|
* @return {boolean}
|
|
|
|
* @private
|
2021-07-02 13:58:36 -04:00
|
|
|
*/
|
|
|
|
_isWaitingForOwnMessage() {
|
|
|
|
return !!this.promiseOwnMsgResolve
|
|
|
|
}
|
|
|
|
|
2021-04-27 02:59:16 -04:00
|
|
|
/**
|
|
|
|
* Wait for a user-sent message to finish getting sent.
|
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @return {Promise<number>} - The ID of the sent message.
|
2021-04-27 02:59:16 -04:00
|
|
|
*/
|
|
|
|
async waitForOwnMessage() {
|
2021-07-02 13:58:36 -04:00
|
|
|
return await this.ownMsgPromise
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
/**
|
|
|
|
* @typedef ChatEvents
|
|
|
|
* @type {object}
|
|
|
|
* @property {MessageData[]} messages - All synced messages, which include receipts for them (if any).
|
|
|
|
* @property {ReceiptData[]} receipts - All synced receipts for messages already present.
|
|
|
|
*/
|
|
|
|
|
2021-07-05 02:37:14 -04:00
|
|
|
/**
|
|
|
|
* Find the reference date indicator nearest to the given element in the timeline.
|
|
|
|
* @param {Element} fromElement
|
|
|
|
* @return {Promise<?Date>} - The value of the nearest date separator.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _getNearestRefDate(fromElement) {
|
|
|
|
let element = fromElement.previousElementSibling
|
|
|
|
while (element && !element.classList.contains("mdRGT10Date")) {
|
|
|
|
element = element.previousElementSibling
|
|
|
|
}
|
|
|
|
return element ? await this._tryParseDateSeparator(element.firstElementChild.innerText) : null
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
/**
|
|
|
|
* Parse the message list of whatever the currently-viewed chat is.
|
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {?number} minID - The minimum message ID to consider.
|
|
|
|
* @return {Promise<MessageData[]>} - A list of messages.
|
2021-06-06 18:15:38 -04:00
|
|
|
*/
|
|
|
|
async parseMessageList(minID = 0) {
|
|
|
|
console.debug(`minID for full refresh: ${minID}`)
|
|
|
|
const msgList =
|
|
|
|
Array.from(document.querySelectorAll("#_chat_room_msg_list > div[data-local-id]"))
|
2021-07-05 02:37:14 -04:00
|
|
|
.filter(msg => msg.getAttribute("data-local-id") > minID)
|
2021-06-06 18:15:38 -04:00
|
|
|
if (msgList.length == 0) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
const messagePromises = []
|
|
|
|
const chatType = this.getChatType(this.getCurrentChatID())
|
2021-07-05 02:37:14 -04:00
|
|
|
let refDate
|
|
|
|
|
2021-04-23 03:38:13 -04:00
|
|
|
for (const child of msgList) {
|
|
|
|
if (child.classList.contains("mdRGT10Date")) {
|
2021-06-05 23:56:54 -04:00
|
|
|
refDate = await this._tryParseDateSeparator(child.firstElementChild.innerText)
|
2021-04-23 03:38:13 -04:00
|
|
|
} else if (child.classList.contains("MdRGT07Cont")) {
|
2021-07-05 02:37:14 -04:00
|
|
|
if (refDate === undefined) {
|
|
|
|
refDate = this._getNearestRefDate(child)
|
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
messagePromises.push(this._parseMessage(child, chatType, refDate))
|
2021-07-05 02:37:14 -04:00
|
|
|
} else if (child.classList.contains("MdRGT10Notice")) {
|
|
|
|
const memberEventMessage = this._tryParseMemberEvent(child)
|
|
|
|
if (memberEventMessage) {
|
|
|
|
// If a member event is the first message to be discovered,
|
|
|
|
// scan backwards for the nearest message before it, and use
|
|
|
|
// that message's timestamp as the timestamp of this event.
|
|
|
|
if (messagePromises.length == 0) {
|
|
|
|
let element = child.previousElementSibling
|
|
|
|
let timeElement
|
|
|
|
while (element && (!element.getAttribute("data-local-id") || !(timeElement = element.querySelector("time")))) {
|
|
|
|
element = element.previousElementSibling
|
|
|
|
}
|
|
|
|
if (element) {
|
|
|
|
if (refDate === undefined) {
|
|
|
|
refDate = this._tryFindNearestRefDate(child)
|
|
|
|
}
|
|
|
|
memberEventMessage.timestamp = (await this._tryParseDate(timeElement.innerText, refDate))?.getTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messagePromises.push(Promise.resolve(memberEventMessage))
|
|
|
|
}
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
// NOTE No message should ever time out, but use allSettled to not throw if any do
|
2021-07-05 02:37:14 -04:00
|
|
|
const messages = (await Promise.allSettled(messagePromises))
|
2021-06-06 18:15:38 -04:00
|
|
|
.filter(value => value.status == "fulfilled")
|
|
|
|
.map(value => value.value)
|
2021-07-05 02:37:14 -04:00
|
|
|
|
|
|
|
// Set the timestamps of each member event to that of the message preceding it,
|
|
|
|
// as a best-guess of its timestamp, since member events have no timestamps.
|
|
|
|
// Do this after having resolved messages.
|
|
|
|
for (let i = 1, n = messages.length; i < n; i++) {
|
|
|
|
if (messages[i].member_info) {
|
|
|
|
messages[i].timestamp = messages[i-1].timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages
|
2021-04-23 03:38:13 -04:00
|
|
|
}
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
/**
|
|
|
|
* Parse receipts of whatever the currently-viewed chat is.
|
|
|
|
* Should only be used for already-processed messages that
|
|
|
|
* get skipped by parseMessageList.
|
|
|
|
*
|
|
|
|
* @param {?Object} rctIDs - The minimum receipt ID to consider for each "read by" count.
|
|
|
|
* It's an Object because Puppeteer can't send a Map.
|
|
|
|
* @return {ReceiptData[]} - A list of receipts.
|
|
|
|
*/
|
|
|
|
parseReceiptList(rctIDs = {}) {
|
|
|
|
console.debug(`rctIDs for full refresh: ${rctIDs}`)
|
|
|
|
|
|
|
|
const isDirect = this.getChatType(this.getCurrentChatID()) == ChatTypeEnum.DIRECT
|
|
|
|
const numOthers = isDirect ? 1 : document.querySelector(SEL_PARTICIPANTS_LIST).childElementCount - 1
|
|
|
|
|
|
|
|
const idGetter = e => +e.closest("[data-local-id]").getAttribute("data-local-id")
|
|
|
|
|
|
|
|
const receipts =
|
|
|
|
Array.from(document.querySelectorAll("#_chat_room_msg_list .mdRGT07Read:not(.MdNonDisp)"))
|
|
|
|
.map(isDirect
|
|
|
|
? e => {
|
|
|
|
return {
|
|
|
|
id: idGetter(e),
|
|
|
|
count: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: e => {
|
|
|
|
return {
|
|
|
|
id: idGetter(e),
|
|
|
|
count: this._getReceiptCount(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Using two lambdas to not branch on isDirect for every element
|
|
|
|
)
|
|
|
|
|
|
|
|
const newReceipts = []
|
|
|
|
const prevFullyReadID = rctIDs[`${numOthers}`] || 0
|
|
|
|
let minCountToFind = 1
|
|
|
|
for (let i = receipts.length-1; i >= 0; i--) {
|
|
|
|
const receipt = receipts[i]
|
|
|
|
if (receipt.count >= minCountToFind && receipt.id > (rctIDs[`${receipt.count}`] || 0)) {
|
|
|
|
newReceipts.push(receipt)
|
|
|
|
if (receipt.count < numOthers) {
|
|
|
|
minCountToFind = receipt.count+1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if (receipt.id <= prevFullyReadID) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newReceipts
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:27:21 -04:00
|
|
|
/**
|
|
|
|
* @typedef PathImage
|
|
|
|
* @type object
|
2021-05-30 19:11:39 -04:00
|
|
|
* @property {?string} path - The virtual path of the image (behaves like an ID). Optional.
|
2021-06-15 02:48:29 -04:00
|
|
|
* @property {string} url - The URL of the image. Mandatory.
|
2021-03-26 02:27:21 -04:00
|
|
|
*/
|
|
|
|
|
2021-05-30 19:11:39 -04:00
|
|
|
/**
|
|
|
|
* @param {Element} img - The image element to get the URL and path of.
|
|
|
|
* @return {?PathImage} - The image URL and its path, if found.
|
2021-07-05 01:25:51 -04:00
|
|
|
* @private
|
2021-05-30 19:11:39 -04:00
|
|
|
*/
|
2021-03-26 02:27:21 -04:00
|
|
|
_getPathImage(img) {
|
|
|
|
if (img && img.src.startsWith("blob:")) {
|
|
|
|
// NOTE Having a blob but no path means the image exists,
|
|
|
|
// but in a form that cannot be uniquely identified.
|
|
|
|
// If instead there is no blob, the image is blank.
|
|
|
|
return {
|
|
|
|
path: img.getAttribute("data-picture-path"),
|
|
|
|
url: img.src,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
|
|
|
* @typedef Participant
|
|
|
|
* @type object
|
2021-05-30 19:11:39 -04:00
|
|
|
* @property {string} id - The member ID for the participant
|
|
|
|
* @property {?PathImage} avatar - The path and blob URL of the participant's avatar
|
|
|
|
* @property {string} name - The contact list name of the participant
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
|
|
|
|
2021-02-25 22:21:11 -05:00
|
|
|
getParticipantListItemName(element) {
|
|
|
|
return element.querySelector(".mdRGT13Ttl").innerText
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:27:21 -04:00
|
|
|
getParticipantListItemAvatar(element) {
|
2021-07-05 02:37:14 -04:00
|
|
|
// Has data-picture-path for rooms, but not groups
|
|
|
|
return this._getPathImage(element.querySelector(".mdRGT13Img > img[src]"))
|
2021-03-23 02:37:30 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 03:38:13 -04:00
|
|
|
getParticipantListItemID(element) {
|
2021-07-05 02:37:14 -04:00
|
|
|
// Exists for groups, but not rooms
|
|
|
|
return element.getAttribute("data-mid")
|
|
|
|
}
|
|
|
|
|
|
|
|
getFriendsListItemName(element) {
|
|
|
|
return element.title
|
|
|
|
}
|
|
|
|
|
|
|
|
getFriendsListItemAvatar(element) {
|
|
|
|
// Never has data-picture-path, but still find a PathImage in case it ever does
|
|
|
|
return this._getPathImage(element.querySelector(".mdCMN04Img > img[src]"))
|
|
|
|
}
|
|
|
|
|
|
|
|
getFriendsListItemID(element) {
|
2021-02-25 22:21:11 -05:00
|
|
|
return element.getAttribute("data-mid")
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:10:10 -04:00
|
|
|
/**
|
|
|
|
* Parse a friends list item element.
|
|
|
|
*
|
|
|
|
* @param {Element} element - The element to parse.
|
|
|
|
* @param {?string} knownID - The ID of this element, if it is known.
|
|
|
|
* @return {Participant} - The info in the element.
|
|
|
|
*/
|
|
|
|
parseFriendsListItem(element, knownID) {
|
|
|
|
return {
|
|
|
|
id: knownID || this.getFriendsListItemID(element),
|
|
|
|
avatar: this.getFriendsListItemAvatar(element),
|
|
|
|
name: this.getFriendsListItemName(element),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the friends list.
|
|
|
|
*
|
|
|
|
* @return {Participant[]}
|
|
|
|
*/
|
|
|
|
parseFriendsList() {
|
|
|
|
const friends = []
|
|
|
|
document.querySelectorAll("#contact_wrap_friends > ul > li[data-mid]")
|
|
|
|
.forEach(e => friends.push(this.parseFriendsListItem(e)))
|
|
|
|
return friends
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-02-16 02:49:54 -05:00
|
|
|
* Parse a group participants list.
|
|
|
|
* TODO Find what works for a *room* participants list...!
|
2020-08-17 15:03:10 -04:00
|
|
|
*
|
|
|
|
* @param {Element} element - The participant list element.
|
2021-06-30 03:08:08 -04:00
|
|
|
* @return {Participant[]} - The list of participants.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
|
|
|
parseParticipantList(element) {
|
2021-02-25 22:21:11 -05:00
|
|
|
// TODO Might need to explicitly exclude own user if double-puppeting is enabled.
|
|
|
|
// TODO The first member is always yourself, right?
|
|
|
|
const ownParticipant = {
|
|
|
|
// TODO Find way to make this work with multiple mxids using the bridge.
|
|
|
|
// One idea is to add real ID as suffix if we're in a group, and
|
|
|
|
// put in the puppet DB table somehow.
|
|
|
|
id: this.ownID,
|
2021-03-26 02:27:21 -04:00
|
|
|
avatar: this.getParticipantListItemAvatar(element.children[0]),
|
2021-02-25 22:21:11 -05:00
|
|
|
name: this.getParticipantListItemName(element.children[0]),
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ownParticipant].concat(Array.from(element.children).slice(1).map(child => {
|
2021-07-05 02:37:14 -04:00
|
|
|
const sender = {
|
|
|
|
name: this.getParticipantListItemName(child),
|
2021-03-26 02:27:21 -04:00
|
|
|
avatar: this.getParticipantListItemAvatar(child),
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
2021-07-05 02:37:14 -04:00
|
|
|
sender.id = this.getParticipantListItemID(child)
|
|
|
|
if (!sender.id) {
|
|
|
|
this._updateSenderFromFriendsList(sender)
|
|
|
|
}
|
|
|
|
return sender
|
2021-02-25 22:21:11 -05:00
|
|
|
}))
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 21:28:12 -04:00
|
|
|
getGroupListItemName(element) {
|
|
|
|
return element.title
|
|
|
|
}
|
|
|
|
|
|
|
|
getGroupListItemAvatar(element) {
|
|
|
|
// Does have data-picture-path
|
|
|
|
return this._getPathImage(element.querySelector(".mdCMN04Img > img[src]"))
|
|
|
|
}
|
|
|
|
|
|
|
|
getGroupListItemID(element) {
|
|
|
|
return element.getAttribute("data-chatid")
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a group list item element.
|
|
|
|
*
|
|
|
|
* @param {Element} element - The element to parse.
|
|
|
|
* @param {?string} knownID - The ID of this element, if it is known.
|
|
|
|
* @return {Participant} - The info in the element.
|
|
|
|
*/
|
|
|
|
parseGroupListItem(element, knownID) {
|
|
|
|
return {
|
|
|
|
id: knownID || this.getGroupListItemID(element),
|
|
|
|
avatar: this.getGroupListItemAvatar(element),
|
|
|
|
name: this.getGroupListItemName(element),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the group list.
|
|
|
|
*
|
|
|
|
* @param {boolean} invited - Whether to parse the list of invited groups instead of joined groups.
|
|
|
|
* @return {Participant[]}
|
|
|
|
*/
|
|
|
|
parseGroupList(invited = false) {
|
|
|
|
const groups = []
|
|
|
|
document.querySelectorAll(`#${invited ? "invited" : "joined"}_group_list_body > li[data-chatid="${id}"]`)
|
|
|
|
.forEach(e => groups.push(this.parseGroupListItem(e)))
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
|
|
|
* @typedef ChatListInfo
|
|
|
|
* @type object
|
2021-03-26 02:27:21 -04:00
|
|
|
* @property {number} id - The ID of the chat.
|
|
|
|
* @property {string} name - The name of the chat.
|
|
|
|
* @property {PathImage} icon - The path and blob URL of the chat icon.
|
2020-08-18 09:47:06 -04:00
|
|
|
* @property {string} lastMsg - The most recent message in the chat.
|
|
|
|
* May be prefixed by sender name.
|
2020-08-24 16:00:32 -04:00
|
|
|
* @property {string} lastMsgDate - An imprecise date for the most recent message
|
|
|
|
* (e.g. "7:16 PM", "Thu" or "Aug 4")
|
2021-06-10 02:10:18 -04:00
|
|
|
* @property {number} notificationCount - The number of unread messages in the chat,
|
|
|
|
* signified by the number in its notification badge.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
/**
|
|
|
|
* @typedef ChatListInfoForCycle
|
|
|
|
* @type object
|
|
|
|
* @property {number} id - The ID of the chat.
|
|
|
|
* @property {number} notificationCount - The number of unread messages in the chat,
|
|
|
|
* signified by the number in its notification badge.
|
|
|
|
* @property {number} numParticipants - The number of participants in the chat,
|
|
|
|
* signified by a count next to the chat title.
|
|
|
|
*/
|
|
|
|
|
2021-04-23 03:38:13 -04:00
|
|
|
getChatListItemID(element) {
|
2021-02-16 02:49:54 -05:00
|
|
|
return element.getAttribute("data-chatid")
|
|
|
|
}
|
|
|
|
|
|
|
|
getChatListItemName(element) {
|
|
|
|
return element.querySelector(".mdCMN04Ttl").innerText
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:27:21 -04:00
|
|
|
getChatListItemIcon(element) {
|
|
|
|
return this._getPathImage(element.querySelector(".mdCMN04Img > :not(.mdCMN04ImgInner) > img[src]"))
|
2021-03-23 02:37:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-16 02:49:54 -05:00
|
|
|
getChatListItemLastMsg(element) {
|
2021-06-10 02:10:18 -04:00
|
|
|
return element.querySelector(".mdCMN04Desc").innerHTML
|
2021-02-16 02:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getChatListItemLastMsgDate(element) {
|
|
|
|
return element.querySelector("time").innerText
|
|
|
|
}
|
|
|
|
|
2021-06-10 02:10:18 -04:00
|
|
|
getChatListItemNotificationCount(element) {
|
|
|
|
return Number.parseInt(element.querySelector(".MdIcoBadge01:not(.MdNonDisp)")?.innerText) || 0
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
getChatListItemOtherParticipantCount(element) {
|
|
|
|
const countElement = element.querySelector(".mdCMN04Count:not(.MdNonDisp)")
|
|
|
|
const match = countElement?.innerText.match(/\d+/)
|
|
|
|
return match ? match[0] - 1 : 1
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-02-16 02:49:54 -05:00
|
|
|
* Parse a conversation list item element.
|
2020-08-17 15:03:10 -04:00
|
|
|
*
|
|
|
|
* @param {Element} element - The element to parse.
|
2021-04-23 03:38:13 -04:00
|
|
|
* @param {?string} knownID - The ID of this element, if it is known.
|
2021-04-06 01:56:46 -04:00
|
|
|
* @return {ChatListInfo} - The info in the element.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
2021-04-23 03:38:13 -04:00
|
|
|
parseChatListItem(element, knownID) {
|
2021-02-25 23:59:25 -05:00
|
|
|
return !element.classList.contains("chatList") ? null : {
|
2021-04-23 03:38:13 -04:00
|
|
|
id: knownID || this.getChatListItemID(element),
|
2021-02-16 02:49:54 -05:00
|
|
|
name: this.getChatListItemName(element),
|
2021-03-26 02:27:21 -04:00
|
|
|
icon: this.getChatListItemIcon(element),
|
2021-02-16 02:49:54 -05:00
|
|
|
lastMsg: this.getChatListItemLastMsg(element),
|
|
|
|
lastMsgDate: this.getChatListItemLastMsgDate(element),
|
2021-06-10 02:10:18 -04:00
|
|
|
notificationCount: this.getChatListItemNotificationCount(element),
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 21:28:12 -04:00
|
|
|
/**
|
|
|
|
* Return the IDs of all groups that aren't in the list of recent chats.
|
|
|
|
*
|
|
|
|
* @return {string[]} - The list of group IDs.
|
|
|
|
*/
|
|
|
|
getJoinedNonrecentGroupIDs() {
|
|
|
|
const ids = []
|
|
|
|
for (const e of document.querySelectorAll("#joined_group_list_body > li[data-chatid]")) {
|
|
|
|
const id = e.getAttribute("data-chatid")
|
|
|
|
if (!document.querySelector(`#_chat_list_body > li > div[data-chatid="${id}"]`)) {
|
|
|
|
ids.push(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-02-21 02:07:48 -05:00
|
|
|
* Parse the list of recent/saved chats.
|
2021-06-06 18:15:38 -04:00
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @return {ChatListInfo[]} - The list of chats.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
2021-02-21 02:07:48 -05:00
|
|
|
parseChatList() {
|
|
|
|
const chatList = document.querySelector("#_chat_list_body")
|
|
|
|
return Array.from(chatList.children).map(
|
2021-02-16 02:49:54 -05:00
|
|
|
child => this.parseChatListItem(child.firstElementChild))
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
/**
|
|
|
|
* Parse a conversation list item element for cycling.
|
|
|
|
*
|
|
|
|
* @param {Element} element - The element to parse.
|
|
|
|
* @return {ChatListInfoForCycle} - The info in the element.
|
|
|
|
*/
|
|
|
|
parseChatListItemForCycle(element) {
|
|
|
|
return {
|
|
|
|
id: this.getChatListItemID(element),
|
|
|
|
notificationCount: this.getChatListItemNotificationCount(element),
|
|
|
|
otherParticipantCount: this.getChatListItemOtherParticipantCount(element),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the list of recent/saved chats, but for properties
|
|
|
|
* relevant to knowing which chat to cycle onto for read receipts.
|
|
|
|
*
|
|
|
|
* @return {ChatListInfoForCycle[]} - The list of chats with relevant properties.
|
|
|
|
*/
|
|
|
|
parseChatListForCycle() {
|
|
|
|
const chatList = document.querySelector("#_chat_list_body")
|
|
|
|
return Array.from(chatList.children).map(
|
|
|
|
child => this.parseChatListItemForCycle(child.firstElementChild))
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-03-26 02:27:21 -04:00
|
|
|
* Download an image at a given URL and return it as a data URL.
|
2020-08-17 15:03:10 -04:00
|
|
|
*
|
2021-03-26 02:27:21 -04:00
|
|
|
* @param {string} url - The URL of the image to download.
|
2020-08-17 15:03:10 -04:00
|
|
|
* @return {Promise<string>} - The data URL (containing the mime type and base64 data)
|
|
|
|
*/
|
2021-03-26 02:27:21 -04:00
|
|
|
async readImage(url) {
|
|
|
|
const resp = await fetch(url)
|
2020-08-17 15:03:10 -04:00
|
|
|
const reader = new FileReader()
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
reader.onload = () => resolve(reader.result)
|
|
|
|
reader.onerror = reject
|
|
|
|
})
|
|
|
|
reader.readAsDataURL(await resp.blob())
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:27:27 -04:00
|
|
|
/**
|
|
|
|
* Wait for updates to the active chat's message list to settle down.
|
|
|
|
* Wait an additional bit of time every time an update is observed.
|
|
|
|
* TODO Look (harder) for an explicit signal of when a chat is fully updated...
|
|
|
|
*
|
2021-07-05 01:25:51 -04:00
|
|
|
* @return {Promise<void>}
|
2021-06-16 02:27:27 -04:00
|
|
|
*/
|
|
|
|
waitForMessageListStability() {
|
|
|
|
// Increase this if messages get missed on sync / chat change.
|
|
|
|
// Decrease it if response times are too slow.
|
2021-06-17 23:55:19 -04:00
|
|
|
const delayMillis = 500
|
2021-06-16 02:27:27 -04:00
|
|
|
|
|
|
|
let myResolve
|
|
|
|
const promise = new Promise(resolve => {myResolve = resolve})
|
|
|
|
|
|
|
|
let observer
|
|
|
|
const onTimeout = () => {
|
|
|
|
console.log("Message list looks stable, continue")
|
|
|
|
console.debug(`timeoutID = ${timeoutID}`)
|
|
|
|
observer.disconnect()
|
|
|
|
myResolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeoutID
|
|
|
|
const startTimer = () => {
|
|
|
|
timeoutID = setTimeout(onTimeout, delayMillis)
|
|
|
|
}
|
|
|
|
|
|
|
|
observer = new MutationObserver(changes => {
|
|
|
|
clearTimeout(timeoutID)
|
|
|
|
console.log("CHANGE to message list detected! Wait a bit longer...")
|
|
|
|
console.debug(`timeoutID = ${timeoutID}`)
|
|
|
|
console.debug(changes)
|
|
|
|
startTimer()
|
|
|
|
})
|
|
|
|
observer.observe(
|
|
|
|
document.querySelector("#_chat_message_area"),
|
|
|
|
{childList: true, attributes: true, subtree: true})
|
|
|
|
startTimer()
|
|
|
|
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
/**
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {MutationRecord[]} mutations - The mutation records that occurred
|
2020-08-17 15:03:10 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_observeChatListMutations(mutations) {
|
2021-02-25 23:59:25 -05:00
|
|
|
// TODO Observe *added/removed* chats, not just new messages
|
2021-06-10 02:10:18 -04:00
|
|
|
const changedChats = new Set()
|
2020-08-17 15:03:10 -04:00
|
|
|
for (const change of mutations) {
|
2021-02-25 23:59:25 -05:00
|
|
|
if (change.target.id == "_chat_list_body") {
|
|
|
|
// TODO
|
|
|
|
// These could be new chats, or they're
|
|
|
|
// existing ones that just moved around.
|
|
|
|
/*
|
|
|
|
for (const node of change.addedNodes) {
|
|
|
|
}
|
|
|
|
*/
|
2021-06-17 00:42:06 -04:00
|
|
|
} else if (change.target.tagName == "LI" && change.addedNodes.length == 1) {
|
2021-04-27 02:59:16 -04:00
|
|
|
if (change.target.classList.contains("ExSelected")) {
|
2021-06-06 18:15:38 -04:00
|
|
|
console.debug("Not using chat list mutation response for currently-active chat")
|
2021-04-23 03:38:13 -04:00
|
|
|
continue
|
|
|
|
}
|
2021-06-17 00:42:06 -04:00
|
|
|
const chat = this.parseChatListItem(change.addedNodes[0])
|
|
|
|
if (chat) {
|
|
|
|
console.log("Added chat list item:", chat)
|
|
|
|
changedChats.add(chat)
|
|
|
|
} else {
|
|
|
|
console.debug("Could not parse added node as a chat list item:", node)
|
2021-02-25 23:59:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// change.removedNodes tells you which chats that had notifications are now read.
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
2021-06-10 02:10:18 -04:00
|
|
|
if (changedChats.size > 0) {
|
|
|
|
console.debug("Dispatching chat list mutations:", changedChats)
|
|
|
|
window.__mautrixReceiveChanges(Array.from(changedChats)).then(
|
2020-08-24 16:00:32 -04:00
|
|
|
() => console.debug("Chat list mutations dispatched"),
|
|
|
|
err => console.error("Error dispatching chat list mutations:", err))
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:59:25 -05:00
|
|
|
* Add a mutation observer to the chat list.
|
2020-08-17 15:03:10 -04:00
|
|
|
*/
|
2021-02-25 23:59:25 -05:00
|
|
|
addChatListObserver() {
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removeChatListObserver()
|
2021-04-27 02:59:16 -04:00
|
|
|
this.chatListObserver = new MutationObserver(async (mutations) => {
|
2021-07-02 13:58:36 -04:00
|
|
|
if (this._isWaitingForOwnMessage()) {
|
2021-06-06 18:15:38 -04:00
|
|
|
// Wait for pending sent messages to be resolved before responding to mutations
|
|
|
|
try {
|
|
|
|
await this.ownMsgPromise
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
2021-04-27 02:59:16 -04:00
|
|
|
|
2020-08-24 16:00:32 -04:00
|
|
|
try {
|
|
|
|
this._observeChatListMutations(mutations)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error observing chat list mutations:", err)
|
|
|
|
}
|
|
|
|
})
|
2021-02-25 23:59:25 -05:00
|
|
|
this.chatListObserver.observe(
|
|
|
|
document.querySelector("#_chat_list_body"),
|
|
|
|
{ childList: true, subtree: true })
|
2021-06-06 18:15:38 -04:00
|
|
|
console.log("Started chat list observer")
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect the most recently added mutation observer.
|
|
|
|
*/
|
|
|
|
removeChatListObserver() {
|
|
|
|
if (this.chatListObserver !== null) {
|
|
|
|
this.chatListObserver.disconnect()
|
|
|
|
this.chatListObserver = null
|
2021-06-06 18:15:38 -04:00
|
|
|
console.log("Disconnected chat list observer")
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
/**
|
2021-06-30 03:04:25 -04:00
|
|
|
* @typedef ReceiptData
|
|
|
|
* @type {object}
|
|
|
|
* @property {number} id - The ID of the read message.
|
|
|
|
* @property {?number} count - The number of users who have read the message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {MutationRecord[]} mutations - The mutation records that occurred
|
2021-06-06 18:15:38 -04:00
|
|
|
* @param {string} chatID - The ID of the chat being observed.
|
2021-04-20 20:01:50 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2021-04-30 01:55:51 -04:00
|
|
|
_observeReceiptsDirect(mutations, chatID) {
|
2021-04-20 20:01:50 -04:00
|
|
|
let receipt_id
|
|
|
|
for (const change of mutations) {
|
|
|
|
if ( change.target.classList.contains("mdRGT07Read") &&
|
|
|
|
!change.target.classList.contains("MdNonDisp")) {
|
|
|
|
const msgElement = change.target.closest(".mdRGT07Own")
|
|
|
|
if (msgElement) {
|
2021-06-30 03:11:26 -04:00
|
|
|
const id = +msgElement.getAttribute("data-local-id")
|
2021-04-20 20:01:50 -04:00
|
|
|
if (!receipt_id || receipt_id < id) {
|
|
|
|
receipt_id = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receipt_id) {
|
2021-04-30 01:55:51 -04:00
|
|
|
window.__mautrixReceiveReceiptDirectLatest(chatID, receipt_id).then(
|
2021-04-20 20:01:50 -04:00
|
|
|
() => console.debug(`Receipt sent for message ${receipt_id}`),
|
|
|
|
err => console.error(`Error sending receipt for message ${receipt_id}:`, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {MutationRecord[]} mutations - The mutation records that occurred
|
2021-06-06 18:15:38 -04:00
|
|
|
* @param {string} chatID - The ID of the chat being observed.
|
2021-04-20 20:01:50 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2021-04-30 01:55:51 -04:00
|
|
|
_observeReceiptsMulti(mutations, chatID) {
|
2021-04-23 03:38:13 -04:00
|
|
|
const ids = new Set()
|
2021-04-20 20:01:50 -04:00
|
|
|
const receipts = []
|
|
|
|
for (const change of mutations) {
|
2021-04-30 01:55:51 -04:00
|
|
|
const target = change.type == "characterData" ? change.target.parentElement : change.target
|
2021-06-06 18:15:38 -04:00
|
|
|
if ( target.classList.contains("mdRGT07Read") &&
|
|
|
|
!target.classList.contains("MdNonDisp"))
|
2021-04-30 01:55:51 -04:00
|
|
|
{
|
2021-06-06 18:15:38 -04:00
|
|
|
const msgElement = target.closest(".mdRGT07Own")
|
2021-04-20 20:01:50 -04:00
|
|
|
if (msgElement) {
|
2021-04-23 03:38:13 -04:00
|
|
|
const id = +msgElement.getAttribute("data-local-id")
|
|
|
|
if (!ids.has(id)) {
|
|
|
|
ids.add(id)
|
|
|
|
receipts.push({
|
|
|
|
id: id,
|
2021-06-06 18:15:38 -04:00
|
|
|
count: this._getReceiptCount(target),
|
2021-04-23 03:38:13 -04:00
|
|
|
})
|
|
|
|
}
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receipts.length > 0) {
|
2021-04-30 01:55:51 -04:00
|
|
|
window.__mautrixReceiveReceiptMulti(chatID, receipts).then(
|
2021-04-23 03:38:13 -04:00
|
|
|
() => console.debug(`Receipts sent for ${receipts.length} messages`),
|
|
|
|
err => console.error(`Error sending receipts for ${receipts.length} messages`, err))
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
/**
|
|
|
|
* @typedef PendingMessage
|
|
|
|
* @type object
|
|
|
|
*
|
|
|
|
* @property {Promise<MessageData>} promise
|
2021-06-30 03:08:08 -04:00
|
|
|
* @property {number} id
|
2021-06-06 18:15:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef SameIDMsgs
|
|
|
|
* @type object
|
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @property {number} id
|
2021-06-06 18:15:38 -04:00
|
|
|
* @property {PendingMessage[]} msgs
|
|
|
|
* @property {Function} resolve
|
2021-06-30 03:08:08 -04:00
|
|
|
* @property {number} numRejected
|
2021-06-06 18:15:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binary search for the array of messages with the provided ID.
|
|
|
|
*
|
|
|
|
* @param {SameIDMsgs[]} sortedSameIDMsgs
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {number} id
|
2021-06-06 18:15:38 -04:00
|
|
|
* @param {boolean} returnClosest - If true, return the index of the nearest result on miss instead of -1.
|
2021-06-30 03:08:08 -04:00
|
|
|
* @return {number} The index of the matched element, or -1 if not found.
|
2021-07-05 01:25:51 -04:00
|
|
|
* @private
|
2021-06-06 18:15:38 -04:00
|
|
|
*/
|
|
|
|
_findMsgsForID(
|
|
|
|
sortedSameIDMsgs, id, returnClosest = false,
|
|
|
|
lowerBound = 0, upperBound = sortedSameIDMsgs.length - 1)
|
|
|
|
{
|
|
|
|
if (lowerBound > upperBound) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (returnClosest && lowerBound == upperBound) {
|
|
|
|
// Caller must check if the result has a matching ID or not
|
|
|
|
return sortedSameIDMsgs[lowerBound].id <= id ? lowerBound : lowerBound-1
|
|
|
|
}
|
|
|
|
const i = lowerBound + Math.floor((upperBound - lowerBound)/2)
|
|
|
|
const val = sortedSameIDMsgs[i]
|
|
|
|
if (val.id == id) {
|
|
|
|
return i
|
|
|
|
} else if (val.id < id) {
|
|
|
|
return this._findMsgsForID(
|
|
|
|
sortedSameIDMsgs, id, returnClosest,
|
|
|
|
i+1, upperBound)
|
|
|
|
} else {
|
|
|
|
return this._findMsgsForID(
|
|
|
|
sortedSameIDMsgs, id, returnClosest,
|
|
|
|
lowerBound, i-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the given message to the proper inner array.
|
|
|
|
* In no inner array exists, insert a new one, preserving sort order.
|
|
|
|
* Return the wrapper of which inner array was added to or created.
|
|
|
|
*
|
|
|
|
* @param {SameIDMsgs[]} sortedSameIDMsgs
|
|
|
|
* @param {PendingMessage} msg
|
|
|
|
* @return {SameIDMsgs}
|
2021-07-05 01:25:51 -04:00
|
|
|
* @private
|
2021-06-06 18:15:38 -04:00
|
|
|
*/
|
|
|
|
_insertMsgByID(sortedSameIDMsgs, msg) {
|
|
|
|
let i = this._findMsgsForID(sortedSameIDMsgs, msg.id, true)
|
|
|
|
if (i != -1 && sortedSameIDMsgs[i].id == msg.id) {
|
|
|
|
sortedSameIDMsgs[i].msgs.push(msg)
|
|
|
|
console.debug("UNLIKELY(?) EVENT -- Found two new message elements with the same ID, so tracking both of them")
|
|
|
|
} else {
|
|
|
|
sortedSameIDMsgs.splice(++i, 0, {
|
|
|
|
id: msg.id,
|
|
|
|
msgs: [msg],
|
|
|
|
numRejected: 0,
|
|
|
|
resolve: null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sortedSameIDMsgs[i]
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
/**
|
2021-04-23 03:38:13 -04:00
|
|
|
* Add a mutation observer to the message list of the current chat.
|
|
|
|
* Used for observing new messages & read receipts.
|
2021-06-06 18:15:38 -04:00
|
|
|
*
|
2021-06-30 03:08:08 -04:00
|
|
|
* @param {?number} minID - The minimum message ID to consider.
|
2021-04-20 20:01:50 -04:00
|
|
|
*/
|
2021-06-06 18:15:38 -04:00
|
|
|
addMsgListObserver(minID = 0) {
|
2021-04-20 20:01:50 -04:00
|
|
|
const chat_room_msg_list = document.querySelector("#_chat_room_msg_list")
|
|
|
|
if (!chat_room_msg_list) {
|
|
|
|
console.debug("Could not start msg list observer: no msg list available!")
|
|
|
|
return
|
|
|
|
}
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removeMsgListObserver()
|
|
|
|
|
|
|
|
const chatID = this.getCurrentChatID()
|
|
|
|
const chatType = this.getChatType(chatID)
|
2021-04-20 20:01:50 -04:00
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
// NEED TO HANDLE:
|
|
|
|
// * message elements arriving in any order
|
|
|
|
// * messages being potentially pending (i.e. decrypting or loading),
|
|
|
|
// and resolving in a potentially different order than they arrived in
|
|
|
|
// * pending messages potentially having multiple elements associated with
|
|
|
|
// them, where only one of them resolves
|
|
|
|
// * message elements being added/removed any number of times, which may
|
|
|
|
// or may not ever resolve
|
|
|
|
// * outgoing messages (i.e. sent by the bridge)
|
|
|
|
// And must send resolved messages to the bridge *in order*!
|
|
|
|
// BUT: Assuming that incoming messages will never be younger than a resolved one.
|
|
|
|
|
|
|
|
const sortedSameIDMsgs = []
|
|
|
|
const pendingMsgElements = new Set()
|
|
|
|
|
2021-04-27 02:59:16 -04:00
|
|
|
this.msgListObserver = new MutationObserver(changes => {
|
2021-06-06 18:15:38 -04:00
|
|
|
console.debug(`MESSAGE LIST CHANGES: check since ${minID}`)
|
|
|
|
const remoteMsgs = []
|
2021-04-23 03:38:13 -04:00
|
|
|
for (const change of changes) {
|
2021-06-06 18:15:38 -04:00
|
|
|
console.debug("---new change set---")
|
|
|
|
for (const child of change.addedNodes) {
|
|
|
|
if (!pendingMsgElements.has(child) &&
|
|
|
|
child.tagName == "DIV" &&
|
|
|
|
child.hasAttribute("data-local-id") &&
|
|
|
|
// Skip timestamps, as these are always current
|
|
|
|
child.classList.contains("MdRGT07Cont"))
|
|
|
|
{
|
2021-06-30 03:11:26 -04:00
|
|
|
const msgID = +child.getAttribute("data-local-id")
|
2021-06-06 18:15:38 -04:00
|
|
|
if (msgID > minID) {
|
|
|
|
pendingMsgElements.add(child)
|
|
|
|
|
|
|
|
// TODO Maybe handle own messages somewhere else...?
|
|
|
|
const ownMsg = this._observeOwnMessage(child)
|
|
|
|
if (ownMsg) {
|
|
|
|
console.log("Found own bridge-sent message, will wait for it to resolve")
|
|
|
|
console.debug(child)
|
|
|
|
this.ownMsgPromise
|
|
|
|
.then(msgID => {
|
|
|
|
console.log("Resolved own bridge-sent message")
|
|
|
|
console.debug(ownMsg)
|
|
|
|
pendingMsgElements.delete(ownMsg)
|
|
|
|
if (minID < msgID) {
|
|
|
|
minID = msgID
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
console.log("Rejected own bridge-sent message")
|
|
|
|
console.debug(ownMsg)
|
|
|
|
pendingMsgElements.delete(ownMsg)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log("Found remote message")
|
|
|
|
console.debug(child)
|
|
|
|
remoteMsgs.push({
|
|
|
|
id: msgID,
|
|
|
|
element: child
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
}
|
|
|
|
// NOTE Ignoring removedNodes because an element can always be added back.
|
|
|
|
// Will simply let permanently-removed nodes time out.
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
if (remoteMsgs.length == 0) {
|
|
|
|
console.debug("Found no new remote messages")
|
2021-04-27 02:59:16 -04:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
|
|
|
|
// No need to sort remoteMsgs, because sortedSameIDMsgs is enough
|
|
|
|
for (const msg of remoteMsgs) {
|
|
|
|
const messageElement = msg.element
|
|
|
|
const pendingMessage = {
|
|
|
|
id: msg.id,
|
|
|
|
promise: this._parseMessage(messageElement, chatType)
|
|
|
|
}
|
|
|
|
const sameIDMsgs = this._insertMsgByID(sortedSameIDMsgs, pendingMessage)
|
|
|
|
|
|
|
|
const handleMessage = async (messageData) => {
|
|
|
|
minID = messageData.id
|
|
|
|
sortedSameIDMsgs.shift()
|
|
|
|
await window.__mautrixReceiveMessages(chatID, [messageData])
|
|
|
|
if (sortedSameIDMsgs.length > 0 && sortedSameIDMsgs[0].resolve) {
|
|
|
|
console.debug("Allowing queued resolved message to be sent")
|
|
|
|
console.debug(sortedSameIDMsgs[0])
|
|
|
|
sortedSameIDMsgs[0].resolve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingMessage.promise.then(
|
|
|
|
async (messageData) => {
|
|
|
|
const i = this._findMsgsForID(sortedSameIDMsgs, messageData.id)
|
|
|
|
if (i == -1) {
|
|
|
|
console.debug(`Got resolved message for already-handled ID ${messageData.id}, ignore it`)
|
|
|
|
pendingMsgElements.delete(messageElement)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (i != 0) {
|
|
|
|
console.debug(`Got resolved message for later ID ${messageData.id}, wait for earlier messages`)
|
|
|
|
await new Promise(resolve => sameIDMsgs.resolve = resolve)
|
|
|
|
console.debug(`Message before ID ${messageData.id} finished, can now send this one`)
|
|
|
|
} else {
|
|
|
|
console.debug(`Got resolved message for earliest ID ${messageData.id}, send it`)
|
|
|
|
}
|
|
|
|
console.debug(messageElement)
|
|
|
|
pendingMsgElements.delete(messageElement)
|
|
|
|
handleMessage(messageData)
|
|
|
|
},
|
|
|
|
// error case
|
|
|
|
async (messageData) => {
|
|
|
|
console.debug("Message element rejected")
|
|
|
|
console.debug(messageElement)
|
|
|
|
pendingMsgElements.delete(messageElement)
|
|
|
|
if (++sameIDMsgs.numRejected == sameIDMsgs.msgs.length) {
|
|
|
|
// Note that if another message element with this ID somehow comes later, it'll be ignored.
|
|
|
|
console.debug(`All messages for ID ${sameIDMsgs.id} rejected, abandoning this ID and sending dummy message`)
|
|
|
|
// Choice of which message to send should be arbitrary
|
|
|
|
handleMessage(messageData)
|
|
|
|
}
|
|
|
|
})
|
2021-04-23 03:38:13 -04:00
|
|
|
}
|
|
|
|
})
|
2021-04-27 02:59:16 -04:00
|
|
|
this.msgListObserver.observe(
|
|
|
|
chat_room_msg_list,
|
2021-04-23 03:38:13 -04:00
|
|
|
{ childList: true })
|
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
console.debug(`Started msg list observer with minID = ${minID}`)
|
2021-04-23 03:38:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
const observeReadReceipts = (
|
|
|
|
chatType == ChatTypeEnum.DIRECT ?
|
2021-04-20 20:01:50 -04:00
|
|
|
this._observeReceiptsDirect :
|
|
|
|
this._observeReceiptsMulti
|
2021-04-23 03:38:13 -04:00
|
|
|
).bind(this)
|
2021-04-20 20:01:50 -04:00
|
|
|
|
2021-04-23 03:38:13 -04:00
|
|
|
this.receiptObserver = new MutationObserver(changes => {
|
2021-04-20 20:01:50 -04:00
|
|
|
try {
|
2021-04-23 03:38:13 -04:00
|
|
|
observeReadReceipts(changes, chatID)
|
2021-04-20 20:01:50 -04:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error observing msg list mutations:", err)
|
|
|
|
}
|
|
|
|
})
|
2021-04-23 03:38:13 -04:00
|
|
|
this.receiptObserver.observe(
|
2021-04-30 01:55:51 -04:00
|
|
|
chat_room_msg_list, {
|
|
|
|
subtree: true,
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ["class"],
|
|
|
|
characterData: chatType != ChatTypeEnum.DIRECT,
|
|
|
|
})
|
2021-04-23 03:38:13 -04:00
|
|
|
|
|
|
|
console.debug("Started receipt observer")
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
_observeOwnMessage(ownMsg) {
|
2021-07-02 13:58:36 -04:00
|
|
|
if (!this._isWaitingForOwnMessage()) {
|
2021-06-06 18:15:38 -04:00
|
|
|
return null
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
|
|
|
|
const successElement =
|
|
|
|
ownMsg.querySelector(this.promiseOwnMsgSuccessSelector)
|
|
|
|
if (successElement) {
|
|
|
|
if (successElement.classList.contains("MdNonDisp")) {
|
|
|
|
console.log("Invisible success for own bridge-sent message, will wait for it to resolve")
|
|
|
|
console.log(successElement)
|
2021-05-06 00:43:26 -04:00
|
|
|
} else {
|
2021-06-06 18:15:38 -04:00
|
|
|
console.debug("Already visible success, must not be it")
|
|
|
|
console.debug(successElement)
|
|
|
|
return null
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2021-04-27 02:59:16 -04:00
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
const failureElement =
|
|
|
|
this.promiseOwnMsgFailureSelector &&
|
|
|
|
ownMsg.querySelector(this.promiseOwnMsgFailureSelector)
|
|
|
|
if (failureElement) {
|
|
|
|
if (failureElement.classList.contains("MdNonDisp")) {
|
|
|
|
console.log("Invisible failure for own bridge-sent message, will wait for it (or success) to resolve")
|
|
|
|
console.log(failureElement)
|
|
|
|
} else {
|
|
|
|
console.debug("Already visible failure, must not be it")
|
|
|
|
console.log(failureElement)
|
|
|
|
return null
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
} else if (this.promiseOwnMsgFailureSelector) {
|
|
|
|
return null
|
|
|
|
}
|
2021-04-27 02:59:16 -04:00
|
|
|
|
2021-06-06 18:15:38 -04:00
|
|
|
const msgID = +ownMsg.getAttribute("data-local-id")
|
|
|
|
this.visibleSuccessObserver = new MutationObserver(
|
|
|
|
this._getOwnVisibleCallback(msgID))
|
|
|
|
this.visibleSuccessObserver.observe(
|
|
|
|
successElement,
|
|
|
|
{ attributes: true, attributeFilter: ["class"] })
|
|
|
|
|
|
|
|
if (this.promiseOwnMsgFailureSelector) {
|
|
|
|
this.visibleFailureObserver = new MutationObserver(
|
|
|
|
this._getOwnVisibleCallback())
|
|
|
|
this.visibleFailureObserver.observe(
|
|
|
|
failureElement,
|
2021-05-06 00:43:26 -04:00
|
|
|
{ attributes: true, attributeFilter: ["class"] })
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
2021-06-06 18:15:38 -04:00
|
|
|
|
|
|
|
return ownMsg
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_getOwnVisibleCallback(msgID=null) {
|
|
|
|
const isSuccess = !!msgID
|
|
|
|
return changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
if (!change.target.classList.contains("MdNonDisp")) {
|
2021-06-06 18:15:38 -04:00
|
|
|
console.log(`Resolved ${isSuccess ? "success" : "failure"} for own bridge-sent message`)
|
2021-04-27 02:59:16 -04:00
|
|
|
console.log(change.target)
|
|
|
|
isSuccess ? this._resolveOwnMessage(msgID) : this._rejectOwnMessage(change.target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_resolveOwnMessage(msgID) {
|
2021-05-06 00:43:26 -04:00
|
|
|
if (!this.promiseOwnMsgResolve) return
|
2021-06-06 18:15:38 -04:00
|
|
|
clearTimeout(this.promiseOwnMsgTimeoutID)
|
2021-04-27 02:59:16 -04:00
|
|
|
const resolve = this.promiseOwnMsgResolve
|
|
|
|
this._promiseOwnMsgReset()
|
|
|
|
|
2021-06-30 03:04:25 -04:00
|
|
|
resolve(msgID)
|
2021-04-27 02:59:16 -04:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:43:26 -04:00
|
|
|
_rejectOwnMessage(failureElement = null) {
|
|
|
|
if (!this.promiseOwnMsgReject) return
|
2021-04-27 02:59:16 -04:00
|
|
|
const reject = this.promiseOwnMsgReject
|
|
|
|
this._promiseOwnMsgReset()
|
|
|
|
|
|
|
|
reject(failureElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
_promiseOwnMsgReset() {
|
|
|
|
this.promiseOwnMsgSuccessSelector = null
|
|
|
|
this.promiseOwnMsgFailureSelector = null
|
|
|
|
this.promiseOwnMsgResolve = null
|
|
|
|
this.promiseOwnMsgReject = null
|
2021-06-06 18:15:38 -04:00
|
|
|
this.promiseOwnMsgTimeoutID = null
|
2021-04-27 02:59:16 -04:00
|
|
|
|
|
|
|
if (this.visibleSuccessObserver) {
|
|
|
|
this.visibleSuccessObserver.disconnect()
|
|
|
|
}
|
|
|
|
this.visibleSuccessObserver = null
|
|
|
|
if (this.visibleFailureObserver) {
|
|
|
|
this.visibleFailureObserver.disconnect()
|
|
|
|
}
|
|
|
|
this.visibleFailureObserver = null
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
removeMsgListObserver() {
|
2021-04-23 03:38:13 -04:00
|
|
|
let result = false
|
2021-04-20 20:01:50 -04:00
|
|
|
if (this.msgListObserver !== null) {
|
|
|
|
this.msgListObserver.disconnect()
|
|
|
|
this.msgListObserver = null
|
|
|
|
console.debug("Disconnected msg list observer")
|
2021-04-23 03:38:13 -04:00
|
|
|
result = true
|
|
|
|
}
|
|
|
|
if (this.receiptObserver !== null) {
|
|
|
|
this.receiptObserver.disconnect()
|
|
|
|
this.receiptObserver = null
|
|
|
|
console.debug("Disconnected receipt observer")
|
|
|
|
result = true
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
2021-04-23 03:38:13 -04:00
|
|
|
return result
|
2021-04-20 20:01:50 -04:00
|
|
|
}
|
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
addQRChangeObserver(element) {
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removeQRChangeObserver()
|
2021-02-10 02:34:19 -05:00
|
|
|
this.qrChangeObserver = new MutationObserver(changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
if (change.attributeName === "title" && change.target instanceof Element) {
|
|
|
|
window.__mautrixReceiveQR(change.target.getAttribute("title"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.qrChangeObserver.observe(element, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ["title"],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeQRChangeObserver() {
|
|
|
|
if (this.qrChangeObserver !== null) {
|
|
|
|
this.qrChangeObserver.disconnect()
|
|
|
|
this.qrChangeObserver = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addQRAppearObserver(element) {
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removeQRAppearObserver()
|
2021-02-10 02:34:19 -05:00
|
|
|
this.qrAppearObserver = new MutationObserver(changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
for (const node of change.addedNodes) {
|
|
|
|
const qrElement = node.querySelector("#login_qrcode_area div[title]")
|
|
|
|
if (qrElement) {
|
|
|
|
window.__mautrixReceiveQR(qrElement.title)
|
|
|
|
window.__mautrixController.addQRChangeObserver(element)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.qrAppearObserver.observe(element, {
|
|
|
|
childList: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeQRAppearObserver() {
|
|
|
|
if (this.qrAppearObserver !== null) {
|
|
|
|
this.qrAppearObserver.disconnect()
|
|
|
|
this.qrAppearObserver = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:49:54 -05:00
|
|
|
addEmailAppearObserver(element) {
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removeEmailAppearObserver()
|
2021-02-10 02:34:19 -05:00
|
|
|
this.emailAppearObserver = new MutationObserver(changes => {
|
2020-08-17 15:03:10 -04:00
|
|
|
for (const change of changes) {
|
2021-02-10 02:34:19 -05:00
|
|
|
for (const node of change.addedNodes) {
|
2021-02-11 00:04:25 -05:00
|
|
|
const emailArea = node.querySelector("#login_email_area")
|
2021-02-20 03:48:08 -05:00
|
|
|
if (emailArea && !emailArea.classList.contains("MdNonDisp")) {
|
2021-02-10 02:34:19 -05:00
|
|
|
window.__mautrixSendEmailCredentials()
|
|
|
|
return
|
|
|
|
}
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-02-10 02:34:19 -05:00
|
|
|
this.emailAppearObserver.observe(element, {
|
|
|
|
childList: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEmailAppearObserver() {
|
|
|
|
if (this.emailAppearObserver !== null) {
|
|
|
|
this.emailAppearObserver.disconnect()
|
|
|
|
this.emailAppearObserver = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:49:54 -05:00
|
|
|
addPINAppearObserver(element) {
|
2021-04-23 03:38:13 -04:00
|
|
|
this.removePINAppearObserver()
|
2021-02-10 02:34:19 -05:00
|
|
|
this.pinAppearObserver = new MutationObserver(changes => {
|
|
|
|
for (const change of changes) {
|
|
|
|
for (const node of change.addedNodes) {
|
|
|
|
const pinElement = node.querySelector("div.mdCMN01Code")
|
|
|
|
if (pinElement) {
|
|
|
|
window.__mautrixReceivePIN(pinElement.innerText)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.pinAppearObserver.observe(element, {
|
|
|
|
childList: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removePINAppearObserver() {
|
|
|
|
if (this.pinAppearObserver !== null) {
|
|
|
|
this.pinAppearObserver.disconnect()
|
|
|
|
this.pinAppearObserver = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__mautrixController = new MautrixController()
|
2021-06-05 23:51:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch for an error dialog / PIN expiry dialog to appear, and click its "OK" button.
|
|
|
|
* Must watch for both its parent appearing & it being added to its parent in the first place.
|
|
|
|
*/
|
|
|
|
const layer = document.querySelector("#layer_contents")
|
|
|
|
new MutationObserver(() => {
|
|
|
|
if (!layer.classList.contains("MdNonDisp")) {
|
|
|
|
const button = layer.querySelector("dialog button")
|
|
|
|
if (button) {
|
2021-07-22 03:26:39 -04:00
|
|
|
console.log("Popup appeared, clicking OK button to continue")
|
2021-06-05 23:51:12 -04:00
|
|
|
button.click()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).observe(layer, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ["class"],
|
|
|
|
childList: true,
|
|
|
|
})
|
2021-05-30 17:41:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch for being logged out.
|
|
|
|
*/
|
|
|
|
const mainApp = document.querySelector("#mainApp")
|
|
|
|
new MutationObserver(() => {
|
|
|
|
if (mainApp.classList.contains("MdNonDisp")) {
|
|
|
|
window.__mautrixLoggedOut()
|
|
|
|
}
|
|
|
|
}).observe(mainApp, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ["class"],
|
|
|
|
})
|