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.1
// @description fix the embeds!!
// @author Cinnabar
// @match https:// url here .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');
for (const preview of allPreviews) {
const linkTitle = preview.childNodes[0].children[0];
const linkDesc = preview.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];
linkTitle.text = title
linkDesc.textContent = desc
}
}
});
}
}
setInterval(previewEditor,2000);
})();
@ottersarecool

This comment has been minimized.

Copy link

@ottersarecool ottersarecool commented Apr 14, 2021

cool idea but for me it does not work

@ItsCinnabar

This comment has been minimized.

Copy link
Owner Author

@ItsCinnabar ItsCinnabar commented Apr 14, 2021

cool idea but for me it does not work

Works on my machine.

Did you forget to edit the match url?

Great bug report btw, really helps me to see what's going wrong on your end.

@ottersarecool

This comment has been minimized.

Copy link

@ottersarecool ottersarecool commented Apr 14, 2021

I have violentmonkey, I edited the match url to my element, I added a alert box which didnt work in violentmonkey at all, then I added tampermonkey to see if this works, the alert was shown but still no preview was changed at all, also not seeing anything in the js console

@tepiloxtl

This comment has been minimized.

Copy link

@tepiloxtl tepiloxtl commented Apr 15, 2021

Works for me here with Tampermonkey on Edge

@bertiebaggio

This comment has been minimized.

Copy link

@bertiebaggio bertiebaggio commented Apr 15, 2021

Wee note: refresh after saving the userscript

@bertiebaggio

This comment has been minimized.

Copy link

@bertiebaggio bertiebaggio commented Apr 15, 2021

The preview images can be filled in too (<meta property="og:image"> element). I'm no DOM tweaker by trade, but my crude attempt works so far:

https://gist.github.com/bertiebaggio/9ffe0e6e54d903c3be82cb2137aa5b3b

@ItsCinnabar

This comment has been minimized.

Copy link
Owner Author

@ItsCinnabar ItsCinnabar commented Apr 15, 2021

Thanks bertie!

Not sure otter, maybe try with tampermonkey as it seems to work for the rest of us with that? I've never used violent monkey it might be slightly different.

@ottersarecool

This comment has been minimized.

Copy link

@ottersarecool ottersarecool commented Apr 15, 2021

yeah that fixed it, got it working now! thank you guys so much <3

@cketti

This comment has been minimized.

Copy link

@cketti cketti commented May 20, 2021

Thanks for the script ❤️

I modified it to use Youtube's oEmbed API: https://gist.github.com/cketti/edbf7efe5e641792d56a384dc501ba1e

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