Skip to content

Instantly share code, notes, and snippets.

@bertiebaggio
Forked from ItsCinnabar/preview.user.js
Last active April 15, 2021 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertiebaggio/9ffe0e6e54d903c3be82cb2137aa5b3b to your computer and use it in GitHub Desktop.
Save bertiebaggio/9ffe0e6e54d903c3be82cb2137aa5b3b to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

Great work!

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