admin管理员组

文章数量:1399925

Following Mozilla's API document on Fullscreen, I've placed the following code in my website, it simply takes the whole document (html element) and makes the page go fullscreen once the user clicks anywhere in the page, and once there's another click, page goes back to normal.

var videoElement = document.getElementsByTagName('html')[0];

function toggleFullScreen() {
    if (!document.mozFullScreen) {
        if (videoElement.mozRequestFullScreen) {
            videoElement.mozRequestFullScreen();
        }
    } else {
        if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } 
    }
}

window.addEventListener("click", function(e) {
    toggleFullScreen();
}, false);

My question is how can I save this fullscreen state so every time that Firefox loads up, that page is still on fullscreen. Or any workaround? This is for Firefox for Android.

Following Mozilla's API document on Fullscreen, I've placed the following code in my website, it simply takes the whole document (html element) and makes the page go fullscreen once the user clicks anywhere in the page, and once there's another click, page goes back to normal.

var videoElement = document.getElementsByTagName('html')[0];

function toggleFullScreen() {
    if (!document.mozFullScreen) {
        if (videoElement.mozRequestFullScreen) {
            videoElement.mozRequestFullScreen();
        }
    } else {
        if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } 
    }
}

window.addEventListener("click", function(e) {
    toggleFullScreen();
}, false);

My question is how can I save this fullscreen state so every time that Firefox loads up, that page is still on fullscreen. Or any workaround? This is for Firefox for Android.

Share Improve this question edited Apr 11, 2018 at 16:19 ramonovski asked Apr 11, 2018 at 16:07 ramonovskiramonovski 4143 silver badges16 bronze badges 3
  • 3 You can't ..... – Evert Commented Apr 11, 2018 at 16:09
  • 2 If it is possible, it sounds like really bad UX. As a user I would be very surprised and confused if a web site automatically went to fullscreen as soon as it loaded, even if it was one used full-screen the last time I visited. – Jordan Running Commented Apr 11, 2018 at 16:13
  • The thing is that, I am the only user here, I do get what you're saying though. – ramonovski Commented Apr 11, 2018 at 21:38
Add a ment  | 

4 Answers 4

Reset to default 2

It's an extreme workaround, but you can make your website a progressive web app and put "display": "fullscreen" in its manifest. Then you can launch your site from the home screen and use it like a fullscreen native app.

Following my experiments and the specs, this isn't doable, from client browser javascript

This api need an user interaction. We can't activate the fullscreen by scripting.

From the fullscreen api specification:

Fullscreen is supported if there is no previously-established user preference, security risk, or platform limitation.

An algorithm is allowed to request fullscreen if one of the following is true:

The algorithm is triggered by user activation.

The algorithm is triggered by a user generated orientation change.

https://fullscreen.spec.whatwg/#model

About activation events:

An algorithm is triggered by user activation if any of the following conditions is true:

The task in which the algorithm is running is currently processing an activation behavior whose click event's isTrusted attribute is true.

The task in which the algorithm is running is currently running the event listener for an event whose isTrusted attribute is true and whose type is one of:

change

click

dblclick

mouseup

pointerup

reset

submit

touchend

https://html.spec.whatwg/multipage/interaction.html#triggered-by-user-activation

We can't trigger fullscreens from scripts, or if so, the script must be triggered by the user.

Including simulating a click won't works, this is regular behavior, made to protect user experience.

With some reflexion, we can't agree more on this, imagine any ads page can launch full screens, the web would be a hell to browse!

You told in ment: «I am the only user here»

What you can do if using unix: (( probably alternatives exists in other os )).

Using midori (a lightweight webkit browser), this will start a real fullscreen.

midori -e Fullscreen -a myurl.html

There is no ways to start firefox or chromium in a fullscreen state from the mand line, to my knowledge.

But what is doable is to trigger a F11 click at system level, focusing on the good window, just after the page launch. ((sendkey in android adb shell?))

xdotool can do that.

Here is a pipe mand line that will launch firefox with myurl.html, search for the most recent firefox window id, then trigger the F11 key on this window.. (Press F11 again to exit)

firefox myurl.html && xdotool search --name firefox | tail -1 | xdotool key F11

This should be easy to adapt for other browsers.

As last alternative, have a look at electron or nw.js.

take a look at this add on for Firefox, i have not tried it, as I'm posting this from mobile, it's description does say that it can force start in full screen. I'm just quoting their description .

Saves the last state or force start in full screen forever! Simple and plete for this purpose.

Edit : And the link to it https://addons.mozilla/en-US/firefox/addon/mfull/

What about using localStorage like this?

function goFullScreen() {
  if (videoElement.mozRequestFullScreen) {
    localStorage.setItem('fullscreenEnabled', true)
    videoElement.mozRequestFullScreen();
  }
}

window.onload = function () {
  if (localStorage.getItem('fullscreenEnabled') === true) {
    goFullScreen();
  }
};

function toggleFullScreen() {
  if (!document.mozFullScreen) {
    goFullScreen();
  } else {
    if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
      localStorage.setItem('fullscreenEnabled', false)
    }
  }
}

window.addEventListener("click", function(e) {
  toggleFullScreen();
}, false)

本文标签: javascriptHow to save fullscreen state in FirefoxStack Overflow