Skip to content

Instantly share code, notes, and snippets.

@cketti
Created May 20, 2021 00:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cketti/edbf7efe5e641792d56a384dc501ba1e to your computer and use it in GitHub Desktop.
Save cketti/edbf7efe5e641792d56a384dc501ba1e to your computer and use it in GitHub Desktop.
Element youtube preview fix (modified)
// ==UserScript==
// @name Element youtube preview
// @namespace http://tampermonkey.net/
// @version 0.3
// @description fix the embeds!!
// @author Cinnabar
// @author cketti
// @match https://app.element.io
// @match https://your.element.url.example
// @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');
for (const preview of allPreviews) {
const linkTitle = preview.querySelector('.mx_LinkPreviewWidget_title a');
if (linkTitle.text != "Before you continue to YouTube") {
continue
}
const oembedUrl = new URL("https://www.youtube.com/oembed");
oembedUrl.searchParams.append("url", linkTitle.href);
GM.xmlHttpRequest({
method: "GET",
url: oembedUrl.toString(),
onload: function(response) {
if (response.status == 200){
const data = JSON.parse(response.responseText);
// Update title
linkTitle.text = data.title;
// Remove explicit height from container element for preview image
const previewImageContainer = preview.querySelector('.mx_LinkPreviewWidget_image');
previewImageContainer.style.height = null;
// Update preview image
const previewImage = previewImageContainer.querySelector('img')
previewImage.src = data.thumbnail_url;
// Update link description with author name
const linkDescription = preview.querySelector('.mx_LinkPreviewWidget_description');
linkDescription.innerText = data.author_name;
}
}
});
}
}
setInterval(previewEditor,2000);
})();
@cketti
Copy link
Author

cketti commented May 20, 2021

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