Skip to content

Instantly share code, notes, and snippets.

@m0o0scar
Last active August 12, 2022 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0o0scar/3ef414ddd1ce9b20f8c89aa49b7a2513 to your computer and use it in GitHub Desktop.
Save m0o0scar/3ef414ddd1ce9b20f8c89aa49b7a2513 to your computer and use it in GitHub Desktop.
[SeaTalk Web] Copy selected messages to clipboard
#toolbar {
position: absolute;
top: -1px;
right: 180px;
padding: 10px;
border: 1px solid #ccc;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
box-shadow: 0px 0px 10px rgb(0 0 0 / 20%);
}
function uidToName(uid) {
return store.getState().contact.userInfo[uid]?.name || uid;
}
function messageToText({ tag, content, senderId, timeStamp }) {
const name = uidToName(senderId);
const time = new Date(timeStamp * 1000).toTimeString().split(' ')[0];
const prefix = `[${time}] ${name}:`;
switch(tag) {
case 'text': return `${prefix} ${content.text}`;
case 'sticker.s':
case 'sticker.c': return `${prefix} [STICKER]`;
case 'image': return `${prefix} [IMAGE]`;
case 'image.gif': return `${prefix} [GIF]`;
case 'file': return `${prefix} [FILE]`;
default: return `${prefix} [${tag}]`;
}
}
function getSelectedMessages() {
const { selectedSession, multiSelectingSessions, lists, messages } = store.getState().messages;
if (!selectedSession) return [];
const {type, id} = selectedSession;
const sid = `${type}-${id}`;
const { selectedMessages = [] } = multiSelectingSessions[sid] || {};
if (!selectedMessages.length) return [];
selectedMessages.sort();
const messageInfos = selectedMessages.map(mid => messages[`${type}-${id}-${mid}`]);
const contents = messageInfos.map(messageToText);
return contents;
}
function copySelectedMessages() {
const msgs = getSelectedMessages();
if (msgs.length) {
navigator.clipboard.writeText(msgs.join('\r\n'));
// exit multi-select mode
document.querySelector('.messages-multi-select-mode-toolbar > .exit > .common-icon').click()
} else {
alert('No message is selected');
}
}
function createButton(label, onclick) {
const btn = document.createElement('button');
btn.innerText = label;
btn.onclick = onclick;
return btn;
}
function initUI() {
const toolbar = document.createElement('div');
toolbar.id = 'toolbar';
document.body.appendChild(toolbar);
console.log(toolbar);
toolbar.appendChild(createButton('Copy Selected Msg', copySelectedMessages))
}
initUI();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment