Search code examples
ubuntufirefox-addonfullscreenfirefox-addon-webextensions

Is window.state = 'fullscreen' from a background script supposed to work?


Working with Xubuntu 14.04 and Firefox 45.0.1 here

I'm trying to automatically put a browser window into a fullscreen state from within a background script, if location.hash == "#fullscreen".

This is requested from a privileged web page's script by doing a postMessage() that a content script listens for, which in turn delegates this request to the background script.

Everything works as expected, including the expected console.log() values in background.js (see the relevant source code snippets below) ... except, the window will not turn fullscreen; nothing actually happens, no console warning about requiring a user initiated event either, which I would receive had I tried something similar from the web page itself (which is why I turned to creating this extension, in the first place). Trying w.state = 'minimized', for instance, doesn't do anything either.

Questions:

  1. Is the Firefox WebExtensions API supposed to support window.state changes (already)?

  2. If so, is the Firefox WebExtensions API supposed to be privileged enough to instigate fullscreen without explicit user interaction?

  3. If so, should I be allowed to do this from the context I'm trying to do this?

  4. Could (X)ubuntu or any Firefox preference be the culprit, perhaps?


Relevant manifest.json data:

"background": {
  "scripts": ["background.js"]
},

"content_scripts": [
  {
    "matches": ["*://privilegeduri/*"],
    "js": ["jquery-1.11.3.min.js", "content.js"],
    "run_at": "document_start"
  }
],

// I've tried without "fullscreen" as well
"permissions": [
  "tabs",
  "fullscreen", // no mention of this on MDN, but I tried it anyway
  "webNavigation"
]

The privileged web page script:

if( location.hash == '#fullscreen' ) {
  if( hasExtension() ) { // function that evaluates whether my extension is installed
    window.postMessage( {
      action: 'requestFullscreen'
    }, 'http://privilegeduri' );
  }
}

The content.js script:

function receiveMessage( e ) {
  if( e.source === window && e.origin === 'http://privilegeduri' ) {
    switch( e.data.action ) {
      case 'requestFullscreen':
        chrome.runtime.sendMessage( e.data );
      break;
    }
  }
}

window.addEventListener( 'message', receiveMessage );

The background.js script:

function receiveMessage( message, sender, sendResponse ) {
  if( sender.id === chrome.runtime.id && sender.url.match( /^http:\/\/privilegeduri/ ) ) {
    switch( message.action ) {
      case 'requestFullscreen':
        browser.windows.get( sender.tab.windowId, {}, function( w ) {
          console.log( w.state ); // outputs 'normal'
          w.state = 'fullscreen';
          console.log( w.state ); // outputs the expected value 'fullscreen'
        } );
      break;
    }
  }
}

chrome.runtime.onMessage.addListener( receiveMessage );

Solution

  • Argh... I just came across the windows.update() method and when I tried to set the state through the updateInfo parameter object it did give a notice:

    Type error for parameter updateInfo (Property "state" is unsupported by Firefox) for windows.update

    That's a pitty.