From ac0a50082f3c6fba92916c98d002c3529381354c Mon Sep 17 00:00:00 2001 From: Shir Serlui <70711723+shirser121@users.noreply.github.com> Date: Tue, 12 Dec 2023 20:56:44 +0200 Subject: [PATCH] Fix link preview (#2681) * Fix link preview * Fix typo * Fix preview options * Add example for preview --- example.js | 3 +++ src/util/Injected.js | 38 ++++++++++++++------------------------ 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/example.js b/example.js index 53315c4..dd708dc 100644 --- a/example.js +++ b/example.js @@ -66,6 +66,9 @@ client.on('message', async msg => { } else if (msg.body.startsWith('!echo ')) { // Replies with the same message msg.reply(msg.body.slice(6)); + } else if (msg.body.startsWith('!preview ')) { + const text = msg.body.slice(9); + msg.reply(text, null, { linkPreview: true }); } else if (msg.body.startsWith('!desc ')) { // Change the group description let chat = await msg.getChat(); diff --git a/src/util/Injected.js b/src/util/Injected.js index d6ebeeb..f411058 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -50,6 +50,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.EphemeralFields = window.mR.findModule('getEphemeralFields')[0]; window.Store.MsgActionChecks = window.mR.findModule('canSenderRevokeMsg')[0]; window.Store.QuotedMsg = window.mR.findModule('getQuotedMsgObj')[0]; + window.Store.LinkPreview = window.mR.findModule('getLinkPreview')[0]; window.Store.Socket = window.mR.findModule('deprecatedSendIq')[0]; window.Store.SocketWap = window.mR.findModule('wap')[0]; window.Store.SearchContext = window.mR.findModule('getSearchContext')[0].getSearchContext; @@ -107,12 +108,6 @@ exports.ExposeStore = (moduleRaidStr) => { // eslint-disable-next-line no-undef if ((m = window.mR.findModule('ChatCollection')[0]) && m.ChatCollection && typeof m.ChatCollection.findImpl === 'undefined' && typeof m.ChatCollection._find !== 'undefined') m.ChatCollection.findImpl = m.ChatCollection._find; - // TODO remove these once everybody has been updated to WWebJS with legacy sessions removed - const _linkPreview = window.mR.findModule('queryLinkPreview'); - if (_linkPreview && _linkPreview[0] && _linkPreview[0].default) { - window.Store.Wap = _linkPreview[0].default; - } - const _isMDBackend = window.mR.findModule('isMDBackend'); if(_isMDBackend && _isMDBackend[0] && _isMDBackend[0].isMDBackend) { window.Store.MDBackend = _isMDBackend[0].isMDBackend(); @@ -271,15 +266,14 @@ exports.LoadUtils = () => { if (options.linkPreview) { delete options.linkPreview; - - // Not supported yet by WhatsApp Web on MD - if(!window.Store.MDBackend) { - const link = window.Store.Validators.findLink(content); - if (link) { - const preview = await window.Store.Wap.queryLinkPreview(link.url); + const link = window.Store.Validators.findLink(content); + if (link) { + let preview = await window.Store.LinkPreview.getLinkPreview(link); + if (preview && preview.data) { + preview = preview.data; preview.preview = true; preview.subtype = 'url'; - options = { ...options, ...preview }; + options = {...options, ...preview}; } } } @@ -379,17 +373,13 @@ exports.LoadUtils = () => { } if (options.linkPreview) { - options.linkPreview = null; - - // Not supported yet by WhatsApp Web on MD - if(!window.Store.MDBackend) { - const link = window.Store.Validators.findLink(content); - if (link) { - const preview = await window.Store.Wap.queryLinkPreview(link.url); - preview.preview = true; - preview.subtype = 'url'; - options = { ...options, ...preview }; - } + delete options.linkPreview; + const link = window.Store.Validators.findLink(content); + if (link) { + const preview = await window.Store.LinkPreview.getLinkPreview(link); + preview.preview = true; + preview.subtype = 'url'; + options = { ...options, ...preview }; } }