From 17d331f4d2078ed52484c16ba01ff40c72b3130f Mon Sep 17 00:00:00 2001 From: jgerstbe Date: Mon, 6 May 2019 01:46:05 +0200 Subject: [PATCH] [A] getMessages() - GRP, E2E, GIF, metadata --- .gitignore | 5 ++ functions/getChats.js | 16 +++++ functions/getMessages.js | 128 +++++++++++++++++++++++++++++++++++++++ index.js | 2 + 4 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 functions/getChats.js create mode 100644 functions/getMessages.js create mode 100644 index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3f11de --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# https://git-scm.com/docs/gitignore +# https://help.github.com/articles/ignoring-files +# Example .gitignore files: https://github.com/github/gitignore +/bower_components/ +/node_modules/ \ No newline at end of file diff --git a/functions/getChats.js b/functions/getChats.js new file mode 100644 index 0000000..2575b4e --- /dev/null +++ b/functions/getChats.js @@ -0,0 +1,16 @@ +function getChats() { + let chats = []; + document.querySelectorAll('._2EXPL').forEach(chat => { + const c = { + profilePic: chat.querySelector('img') ? chat.querySelector('img').src : '', + name: chat.querySelector('._1wjpf').textContent, + message: chat.querySelector('._2_LEW ').textContent, + time: chat.querySelector('._3T2VG').textContent, + unreadMessages: false, // TODO unreadMessages + unreadCount: 0, // TODO unreadCount + } + chats.push(c); + }) + return chats; +} +getChats() diff --git a/functions/getMessages.js b/functions/getMessages.js new file mode 100644 index 0000000..d587564 --- /dev/null +++ b/functions/getMessages.js @@ -0,0 +1,128 @@ +function getMessages() { + let messages = []; + document.querySelectorAll('.vW7d1').forEach(msg => { + let m, meta = { + type: 'message', + direction: 'out', + status: null, + date: null, + time: null, + }; + // meta + if (msg.querySelector('._1DZAH')) { + // time + meta.time = msg.querySelector('._3EFt_').textContent; + //date + if (msg.querySelector('.copyable-text')) { + meta.date = msg.querySelector('.copyable-text').getAttribute('data-pre-plain-text').split('] ')[0].slice(1).split(', ')[1]; + } else if (messages.length > 0) { + meta.date = messages[messages.length - 1].date; + } + // status + if (msg.querySelector('._32uRw span')) { + if (msg.querySelector('._32uRw span').getAttribute('data-icon').includes('dblcheck')) { + meta.status = 'read'; + } else if (msg.querySelector('._32uRw span').getAttribute('data-icon').includes('check')) { + meta.status = 'send'; + } + } else { + meta.direction = 'in'; + } + console.log(meta.date, meta.time, meta.direction, meta.status) + } + // content + if (msg.classList.contains('_3rjxZ')) { + // SYSTEM MESSAGES + if (msg.querySelector('._14b5J') != null) { + // E2E _14b5J + meta.type = 'e2e', + m = { + txt: msg.querySelector('._14b5J div').textContent, + } + } else if (msg.querySelector('._2ArBI') != null) { + console.log('GRP ANNOUNCEMENT', msg) + // GRP ANNOUNCEMENT _2ArBI + meta.type = 'groupEvent', + m = { + txt: msg.querySelector('._2ArBI span').textContent, + } + } else { + console.log('date', msg) + // DATE + meta.type = 'date'; + m = { + txt: msg.querySelector('span').textContent, + } + } + } else if (msg.querySelector('._3v3PK') != null) { + console.log('media', msg) + // img .message-out + meta.type = 'media'; + m = { + image: msg.querySelector('img').src, + } + // msg.querySelector('.WEu0O').style.backgroundImage + } else if(msg.querySelector('._10MiG') != null) { + console.log('GIF', msg) + // gif ._10MiG + meta.type = 'gif'; + m = { + preview: msg.querySelector('.WEu0O').style.backgroundImage.slice(5, msg.querySelector('.WEu0O').style.backgroundImage.length-2), + video: msg.querySelector('._1Y0rl').src + } + } else if(msg.querySelector('._2hOiI') != null) { +// // ggf. alles gif ist in ._3CnDa +// console.log('GIF2', msg) +// // gif ._2hOiI +// // DAS IST EIN GIF, DAS GERADE LÄUFT +// meta.type = 'gif2'; +// m = { +// preview: msg.querySelector('.WEu0O').style.backgroundImage.slice(5, msg.querySelector('.WEu0O').style.backgroundImage.length-2), +// video: msg.querySelector('._1Y0rl').src +// } + } else if (msg.querySelector('._3FDzO') != null) { + // link ._3FDzO + meta.type = 'link'; + m = { + preview: { + img: msg.querySelector('._2H6ea').src, + content: msg.querySelector('._3BCzw').textContent + }, + message: msg.querySelector('._3zb-j.ZhF0n .selectable-text.copyable-text').textContent, + } + } else if (msg.querySelector('.quoted-mention') != null) { + console.log('response', msg) + // response .quoted-mention + meta.type = 'response', + m = { + name: msg.querySelector('.copyable-text').getAttribute('data-pre-plain-text').split('] ')[1].replace(':', '').trim(), + message: msg.querySelector('.copyable-text.selectable-text').textContent, + r_name: msg.querySelector('._2a1Yw').textContent, + r_message: msg.querySelector('.quoted-mention').textContent, + } + } else { + console.log('message', msg) + // message .Tkt2p + m = { + name: msg.querySelector('.copyable-text').getAttribute('data-pre-plain-text').split('] ')[1].replace(':', '').trim(), + message: msg.querySelector('.copyable-text').textContent, + } + } + // TODO message - function getText() bauen, welche statt textContent innerHTML nimmt und emojis im text verarbeitet & integriert + messages.push({ + meta: meta ? meta : null, + content: m ? m : null, + }); + }) + return messages; +} + +function test() { + let x = getMessages(); + console.log(x.length, 'messages', x); + console.table(x.find(m => m.meta.type === 'message')) + console.table(x.find(m => m.meta.type === 'response')) + console.table(x.find(m => m.meta.type === 'gif')) + console.table(x.find(m => m.meta.type === 'link')) +} +test() diff --git a/index.js b/index.js new file mode 100644 index 0000000..667cf3f --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +// TODO export functions +// TODO build module