Created
May 20, 2021 00:10
-
-
Save cketti/edbf7efe5e641792d56a384dc501ba1e to your computer and use it in GitHub Desktop.
Element youtube preview fix (modified)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://gist.github.com/ItsCinnabar/ebcfe4f6b3ea7d224a8e1ef0783edeb2