Allow full screen on embedded Youtube

ViolentMonkey script

< Feedback on Allow full screen on embedded Youtube

Review: OK - script works, but has bugs

§
Posted: 16.02.2021

Doesn't work

§
Posted: 17.02.2021
Edited: 17.02.2021

Doesn't work because
this document.querySelector('iframe[src^="https://www.youtube.com/embed/"]')
doesn't match this
document.querySelectorAll('iframe[src^="https://youtube.com/embed/"]')

You should use document.querySelector('iframe[src^="https://www.youtube.com/embed/"],iframe[src^="https://youtube.com/embed/"]')
But even this doesn't select more than 1 iframe, so you can't use a for condition to loop after...
this document.querySelector('iframe[src^="https://www.youtube.com/embed/"]').length won't return anything even if there's a match...


I've fully fixed your script! Please release this as an update!
// ==UserScript==
// @name Allow full screen on embedded Youtube
// @namespace mwisnicki@gmail.com
// @match *://*/*
// @grant none
// @version 1.0
// @author mwisnicki@gmail.com
// @description 3/8/2020, 10:42:28 PM
// @run-at document-end
//@noframes
// ==/UserScript==

var iframes = document.querySelectorAll("iframe"); //Get all iframes on the page
for (var i = iframes.length; i--;) { //Starts the for condition
if (iframes[i].src.match('youtube') !== null && iframes[i].src.match('allow') === null) //If the iframe has an YT link and if the iframe link doesn't have the allow attribute
{ //Starts the if condition
iframes[i].allowFullscreen = true; //Add the permission to open in full scree
iframes[i].src = iframes[i].src; //Reload the iframe to make the iframe have the new permission
} //Finishes the if condition
} //Finishes the for condition

§
Posted: 17.02.2021

This line is actually needless
iframes[i].src = iframes[i].src; //Reload the iframe to make the iframe have the new permission

marwisAuthor
§
Posted: 15.03.2021

This won't work with dynamic pages. Also need example URL as per latest comment.
I've added github link to latest script to facilitate better collaboration.

§
Posted: 15.03.2021

I don't have an account on github.

Thanks for the update, it works now, I'm just not sure if works for pages that have more than 1 youtube video iframed...

marwisAuthor
§
Posted: 15.03.2021

I see no reason why it wouldn't. Please update your rating.

§
Posted: 15.03.2021

Open any page that has 2 or more youtube iframes and paste this on the console
const URLs = [
"https://www.youtube.com/embed/",
"https://youtube.com/embed/",
"https://www.youtube-nocookie.com/embed/"
];

Then this

URLs.length, if this returns only 1 then the script would work only for the 1 iframe, and the for condition would be needless.

marwisAuthor
§
Posted: 15.03.2021

Please give specific URL

marwisAuthor
§
Posted: 15.03.2021
Open any page that has 2 or more youtube iframes and paste this on the console
const URLs = [
"https://www.youtube.com/embed/",
"https://youtube.com/embed/",
"https://www.youtube-nocookie.com/embed/"
];

Then this

URLs.length, if this returns only 1 then the script would work only for the 1 iframe, and the for condition would be needless.

This makes zero sense.

§
Posted: 16.03.2021
Edited: 16.03.2021

You are right, actually I meant the variable iframes, but I've tested that and it's returning more than 1 so it's okay. The for condition works and is really needed here.

I think you could improve the code erasing the whole function forceReloadIframe and replacing that by 1 single line
iframe.src = iframe.src; //Reload the iframe to make the iframe have the new permission
//Replace this line forceReloadIframe(iframe); with the line above if you want

But this isn't important and isn't a big deal either, your code is working so it's fine as it is too

marwisAuthor
§
Posted: 16.03.2021

I recall having some issue with simply reassigning src but couldn't reproduce it on ign.com so I've simplified reload as suggested.

§
Posted: 16.03.2021

Thanks, but actually I meant this. (Is basically the exact same thing you did anyways)


const iframes = document.body.querySelectorAll(SELECTOR)
for (const iframe of iframes) {
iframe.setAttribute("allowfullscreen","");
iframe.src = iframe.src;
console.log("Forced Youtube allowfullscreen on %o", iframe);
}


But now only the first yt iframe is reloaded, though the code works fine if executed on the console...
That's probably because you are executing the function for every single mutation in the page, instead of doing this you could just have used window.onload

https://myanimelist.net/forum/?topicid=1894279


// ==UserScript==
// @name Allow full screen on embedded Youtube
// @namespace mwisnicki@gmail.com
// @homepage https://github.com/mwisnicki/userscripts/blob/master/allowfullscreen-youtube-embed.user.js
// @match *://*/*
// @grant none
// @version 6
// @author mwisnicki@gmail.com
// @description ViolentMonkey script
// ==/UserScript==

window.onload = function() { //Starts the function when the website finished loading
const URLs = [
"https://www.youtube.com/embed/",
"https://youtube.com/embed/",
"https://www.youtube-nocookie.com/embed/"
];

const SELECTOR = URLs.map(url => `iframe[src^="${url}"]:not([allowfullscreen])`).join(', ');

const iframes = document.body.querySelectorAll(SELECTOR)
for (const iframe of iframes) {
iframe.setAttribute("allowfullscreen","");
iframe.src = iframe.src;
console.log("Forced Youtube allowfullscreen on %o", iframe);
}
}; //Finishes the onload function


Now this version I made of your script always does correctly reload the iframe to apply the allowfullscreen attribute

marwisAuthor
§
Posted: 17.03.2021

On the link you provided both videos get working fullscreen button with the script enabled.
I'm not sure why you keep thinking the loop would only execute for one element.

The fix has to run for every mutation because of websites that add iframes with javascript after onload (essentially any SPA).

§
Posted: 17.03.2021
Edited: 17.03.2021

On the link you provided both videos get working fullscreen button with the script enabled.

Only sometimes, reload the page a few times and try a few times and you will see that the script is not always executed on the correct timming, this makes the second video not reload, so even though the allowfullscreen attribute is added to the second video iframe, because the video is not reloaded is the same as nothing...


I'm not sure why you keep thinking the loop would only execute for one element.

Because your previous versions didn't work well at this point, but now this but is fixed!

Got it. But you should also wrap everything inside the window.onload advent listener, otherwise as I said your script will add the allowfullscreen attribute to all iframes, but won't always correctly reload the other iframes (besides the first iframe)

marwisAuthor
§
Posted: 29.03.2021

I believe Chrome disables all extensions on Chrome WebStore for security reasons.

§
Posted: 29.03.2021
Edited: 29.03.2021

I'm using opera

*I've just created a script that shows an alert() and that worked on that page...

§
Posted: 10.07.2021

new MutationObserver
This code just makes any script run a bunch of times when the page is still loading and usually this makes the page loading time much much slower, to fix this just wrap that mutation observer inside window.onload function (){}

@decembre
Yeah,I think it works perfectly now, but is still being executed much more than needed while the page is loading, the only problem of this is that the page loading could be greatly delayed, but it's working so I guess it's fine.
Of the author doesn't do the modification I suggested above, you can do it yourself if you want.

No, I could look for other links, but I probably won't look for urls with a bunch of YT embedded videos, because the script is probably working now on any website, besides the chrome store...
I will check your chrome store link to see if it works or not,thanks for that link.

§
Posted: 30.08.2021

Requires pages to be reloaded a lot of times just to make the script finally work

https://chrome.google.com/webstore/detail/tabli/igeehkedfibbnhbfponhjjplpkeomghi

Please do as I said, isn't that hard to wrap the mutation observer inside the window.onload is it?

§
Posted: 29.04.2024
Edited: 29.04.2024

Doesn't work because

hacker09
Thanks , your code works in 2024!

§
Posted: 04.05.2024

For some reason it breaks
https://vimeo.com/571957875
And I think that it also breaks the facebook website....

§
Posted: 05.05.2024
Edited: 05.05.2024

I may be wrong but i think it has something to do with latest chrome update that restricts greasy. It requires developer rights now.
I open page with yt and one time it works another time it doesnt.
I open extension in chrome , where script is, and it starts working again. Its weird.

Your page works fine for me. No issues. fullscreen option, but thats not yt

Post reply

Sign in to post a reply.