Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Element youtube preview fix
// ==UserScript==
// @name Element youtube preview
// @namespace http://tampermonkey.net/
// @version 0.2
// @description fix the embeds!!
// @author Cinnabar (bertieb for images)
// @match https://example.com/
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant GM.xmlHttpRequest
// @connect youtube.com
// @connect youtu.be
// ==/UserScript==
(function() {
'use strict';
function previewEditor() {
// const allPreviews = document.getElementsByClassName('mx_LinkPreviewWidget_caption');
const allPreviews = document.getElementsByClassName('mx_LinkPreviewWidget');
for (const preview of allPreviews) {
const previewImg = preview.childNodes[0].children[0];
const caption = preview.childNodes[1];
const linkTitle = caption.childNodes[0].children[0];
const linkDesc = caption.childNodes[2]
if (linkTitle.text != "Before you continue to YouTube") {
continue
}
GM.xmlHttpRequest({
method: "GET",
url: linkTitle.href,
onload: function(response) {
if (response.status == 200){
const title = response.responseText.match(/<title[^>]*>([^<]+)<\/title>/)[1];
const desc = response.responseText.match(/<meta name="description" content="(.*?)">/)[1];
const imgsrc = response.responseText.match(/<meta property="og:image" content="(.*?)">/)[1];
linkTitle.text = title
linkDesc.textContent = desc
previewImg.src = imgsrc;
}
}
});
}
}
setInterval(previewEditor,2000);
})();
@bertiebaggio

This comment has been minimized.

Copy link
Owner Author

@bertiebaggio bertiebaggio commented Apr 15, 2021

As noted in the original gist, remember to update the @match line with the URL of your Element client -- eg // @match https://app.element.io -- and refresh the page after saving/enabling the script.

@ItsCinnabar

This comment has been minimized.

Copy link

@ItsCinnabar ItsCinnabar commented Apr 15, 2021

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment