1
0
Fork 0
mirror of https://git.jami.net/savoirfairelinux/jami-client-qt.git synced 2025-04-21 21:52:03 +02:00
jami-client-qt/resources/misc/previewInterop.js
Trevor Tabah 2e67dc1bd8 chatview: replace web chat view with qml listview
Introduces a primitive QML ListView based chat view lacking
features present in the previous web chat view, that will be added
in subsequent commits(styling, preview/media/link/file-transfer
message type support, etc.).

Gitlab: #467
Change-Id: Iedc40f6172a6cdacc48cda6f4187053fbf226713
2021-10-06 15:51:20 -04:00

54 lines
1.7 KiB
JavaScript

_ = new QWebChannel(qt.webChannelTransport, function (channel) {
window.jsbridge = channel.objects.jsbridge
})
function log(msg) {
window.jsbridge.log(msg)
}
function getPreviewInfo(messageId, url) {
var title = null
var description = null
var image = null
if (!url.includes("http://") && !url.includes("https://")) {
url = "http://".concat(url)
}
fetch(url, {
mode: 'no-cors',
headers: {'Set-Cookie': 'SameSite=None; Secure'}
}).then(function (response) {
return response.text()
}).then(function (html) {
// create DOM from html string
var parser = new DOMParser()
var doc = parser.parseFromString(html, "text/html")
if (!url.includes("twitter.com")){
title = getTitle(doc)
image = getImage(doc, url)
description = getDescription(doc)
var domain = (new URL(url))
domain = (domain.hostname).replace("www.", "")
} else {
title = "Twitter. It's what's happening."
}
window.jsbridge.infoReady(messageId, {
'title': title,
'image': image,
'description': description,
'url': url,
'domain': domain,
})
}).catch(function (err) {
log("Error occured while fetching document: " + err)
})
}
function parseMessage(messageId, message) {
var links = linkify.find(message)
if (links.length === 0) {
return
}
getPreviewInfo(messageId, links[0].href)
window.jsbridge.linkifyReady(messageId, linkifyStr(message))
}