1
0
Fork 0
mirror of https://git.jami.net/savoirfairelinux/jami-client-qt.git synced 2025-08-18 05:35:46 +02:00
jami-client-qt/resources/webengine/previewInfo.js

127 lines
4.7 KiB
JavaScript
Raw Normal View History

misc: vendor libjamiclient into 'src/libclient/' This is libjamiclient from the jami-libclient.git repository as of 767c45b8b09806ac05cbada720864df93588c047, with the following changes: * src/libclient/newaccountmodel.cpp: (NewAccountModelPimpl::removeFromAccounts): The lock wait inside '#ifdef CHK_FREEABLE_BEFORE_ERASE_ACCOUNT' was not updated when the type of 'accounts' was changed over the years from this: std::map<std::string, account::Info> to this: std::map<QString, std::pair<account::Info, std::shared_ptr<Database>>> Basically we need to get the 'first' of the pair for 'account::Info'. So we now do that. * src/libclient/avmodel.cpp: * src/libclient/callbackshandler.cpp: * src/libclient/contactmodel.cpp: * src/libclient/conversationmodel.cpp: * src/libclient/database.cpp: * src/libclient/namedirectory.cpp: * src/libclient/newaccountmodel.cpp: * src/libclient/newcallmodel.cpp: * src/libclient/newdevicemodel.cpp: * src/libclient/peerdiscoverymodel.cpp: * src/libclient/pluginmodel.cpp: * src/libclient/smartinfohub.cpp: * src/libclient/vcard.h: * src/libclient/authority/storagehelper.cpp: Replace Qt's 'foreach' with 'Q_FOREACH' and its 'emit' with 'Q_EMIT' because in the client-qt code base we have '-DQT_NO_KEYWORDS' to avoid conflicts with other libraries we use. * cmake/FindLibJami.cmake: Import cmake/FindRing.cmake from the jami-libclient.git repository. Then, rename RING_BUILD_DIR to LIBJAMI_BUILD_DIR (though the old name is still supported for now). Also update other references of Ring to Jami. Further, add additional calls to 'find_library' to make sure specified local paths for libjami are checked before system-wide ones (in case of older/obsolete libjami being available system-wide, which might happen on GNU/Linux systems). * translations/lrc_*.ts: Import translation files from the libclient repository. The message location paths were corrected by running "sed -i 's|../src|&/libclient|g' lrc_*.ts" in 'translations/'. .tx/config: Add section for the newly-imported libclient translations. * CMakeLists.txt: Reformat, plus various fixes and cleanups, such as changing indentation to 2 spaces and wrapping lines at 70 characters, renaming the parent directory of translations from 'ring' to 'jami', and using all lowercase function calls. Also add copyright headers. * src/app/appsettingsmanager.cpp: (AppSettingsManager::loadTranslations): * src/app/utilsadapter.cpp (UtilsAdapter::supportedLang): Update to adapt to the renaming of the parent directory of translations from 'ring' to 'jami'. GitLab: #748 Change-Id: I86e3b0fb30e554755023e7b858b6a0d132cd59ab
2022-05-06 16:00:16 -04:00
/* MIT License
Copyright (c) 2019 Andrej Gajdos
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
/**
* Retrieves the title of a webpage which is used to fill out the preview of a hyperlink
* @param doc the DOM of the url that is being previewed
* @returns the title of the given webpage
*/
function getTitle(doc){
const og_title = doc.querySelector('meta[property="og:title"]')
if (og_title !== null && og_title.content.length > 0) {
return og_title.content
}
const twitter_title = doc.querySelector('meta[name="twitter:title"]')
if (twitter_title !== null && twitter_title.content.length > 0) {
return twitter_title.content
}
const doc_title = doc.title
if (doc_title !== null && doc_title.length > 0) {
return doc_title
}
if (doc.querySelector("h1") !== null){
const header_1 = doc.querySelector("h1").innerHTML
if (header_1 !== null && header_1.length > 0) {
return header_1
}
}
if (doc.querySelector("h2") !== null){
const header_2 = doc.querySelector("h2").innerHTML
if (header_2 !== null && header_2.length > 0) {
return header_2
}
}
return null
}
/**
* Obtains a description of the webpage for the hyperlink preview
* @param doc the DOM of the url that is being previewed
* @returns a description of the webpage
*/
function getDescription(doc){
const og_description = doc.querySelector('meta[property="og:description"]')
if (og_description !== null && og_description.content.length > 0) {
return og_description.content
}
const twitter_description = doc.querySelector('meta[name="twitter:description"]')
if (twitter_description !== null && twitter_description.content.length > 0) {
return twitter_description.content
}
const meta_description = doc.querySelector('meta[name="description"]')
if (meta_description !== null && meta_description.content.length > 0) {
return meta_description.content
}
var all_paragraphs = doc.querySelectorAll("p")
let first_visible_paragraph = null
for (var i = 0; i < all_paragraphs.length; i++) {
if (all_paragraphs[i].offsetParent !== null &&
!all_paragraphs[i].childElementCount !== 0) {
first_visible_paragraph = all_paragraphs[i].textContent
break
}
}
return first_visible_paragraph
}
/**
* Gets the image that represents a webpage.
* @param doc the DOM of the url that is being previewed
* @returns the image representing the url or null if no such image was found
*/
function getImage(doc) {
const og_image = doc.querySelector('meta[property="og:image"]')
if (og_image !== null && og_image.content.length > 0){
return og_image.content
}
const image_rel_link = doc.querySelector('link[rel="image_src"]')
if (image_rel_link !== null && image_rel_link.href.length > 0){
return image_rel_link.href
}
const twitter_img = doc.querySelector('meta[name="twitter:image"]')
if (twitter_img !== null && twitter_img.content.length > 0) {
return twitter_img.content
}
let imgs = Array.from(doc.getElementsByTagName("img"))
if (imgs.length > 0) {
imgs = imgs.filter(img => {
let add_image = true
if (img.naturalWidth > img.naturalHeight) {
if (img.naturalWidth / img.naturalHeight > 3) {
add_image = false
}
} else {
if (img.naturalHeight / img.naturalWidth > 3) {
add_image = false
}
}
if (img.naturalHeight <= 50 || img.naturalWidth <= 50) {
add_image = false
}
return add_image
})
}
return null
}