Skip to content

Instantly share code, notes, and snippets.

@thecotne
Last active March 11, 2021 09:55
Show Gist options
  • Save thecotne/5995a875c07137b918c7b1d0196c6be4 to your computer and use it in GitHub Desktop.
Save thecotne/5995a875c07137b918c7b1d0196c6be4 to your computer and use it in GitHub Desktop.
pornhub fullscreen on orientation change avoiding "API can only be initiated by a user gesture." warning

turns out you can't use orientationchange event on window object like so

window.addEventListener("orientationchange", () => {
  document.body.webkitRequestFullscreen()
});

you need to use change event on screen.orientation object

like this

screen.orientation.addEventListener('change', () => {
  document.body.webkitRequestFullscreen()
});

πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰ Magic Yay πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰Β 

source: https://cdn1d-static-shared.phncdn.com/html5player/videoPlayer/html5/3.1.0/mhp1138.player.HTML5PlayerMobile.js gist: https://gist.github.com/thecotne/31e374d42c80d89a244b9da24012d39b

if(('orientation' in screen) && !player.settings.embeds.enabled) {
screen.orientation.addEventListener('change', function (e) {
if (screen.orientation.type === 'landscape-primary' || screen.orientation.type === 'landscape-secondary') {
player.fullscreen.enter();
} else {
player.fullscreen.exit();
}
e.preventDefault();
e.stopPropagation();
});
}
function checkAvailability() {
var element = doc.documentElement,
elementIos = player.playerElement;//IOS contains webkitSupportsFullscreen in video element
if (element.requestFullScreen ||
element.mozRequestFullScreen ||
element.webkitRequestFullScreen ||
element.msRequestFullscreen ||
elementIos.webkitEnterFullscreen
) {
return true;
}
return false;
}
function enterFullscreen() {
var element = player.playerContainerElement;
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
// special case for iOS
} else if (ios && !element.webkitRequestFullScreen) {
// assuming we would find a video tag inside player container
var video = player.playerElement;
// there is a different method name to enter fullscreen
if (video.webkitSupportsFullscreen) {
video.webkitEnterFullscreen();
}
}
}
function exitFullscreen() {
if (doc.cancelFullScreen) {
doc.cancelFullScreen();
} else if (doc.mozCancelFullScreen) {
doc.mozCancelFullScreen();
} else if (doc.webkitCancelFullScreen) {
doc.webkitCancelFullScreen();
} else if (doc.msExitFullscreen) {
doc.msExitFullscreen();
}
}
function toggleFullscreen() {
if (fullscreenState) {
exitFullscreen();
} else {
enterFullscreen();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment