Search code examples
javascriptgmailgreasemonkey

Is there a way to prevent Gmail from resizing my browser window?


Some email messages resize my entire browser window when I open them in Gmail. This happens in both Chrome and Firefox. It seems as though specific messages trigger this behavior. The window also resizes if I leave and then return to a tab with one of these messages open, or if I have the Chrome DevTools panel open.

I would like to disallow this behavior from the client-side. Is there a known way to prevent a web page from resizing a browser window?

I have looked for CSS properties and functions that might affect window size, finding none.

The Window.resizeTo() JavaScript method looked promising. Checking via the page inspector, resizeTo does appear within the page. Testing this this answer about redefining Window.resizeTo() to do nothing did not have any effect. Perhaps the page is not be using this method, and is resizing the window in another way.

Searching the page source for "resize" turned up two hits. Neither looks anything like resizeTo. Both are embedded in the middle of minimized JavaScript that I was unable to reverse engineer. Even so, I am not sure what client-side code injection (e.g., using Greasemonkey) would prevent just the "relevant" scrips from running or otherwise block their effects.


Solution

  • There are two ways to capture a change in window size and it's gotta start from that (detection) :

    window.onresize = function(){
    
    // your script goes here
    
    };
    

    or another method...

    function ResizeEvent(){
    
    // your script goes here
    
    }
    
    window.addEventListener('resize',ResizeEvent);
    

    If this is in an iframe then the first one becomes...

    frame_ID.contentWindow.onresize = function(){};
    

    You get the drift...

    Btw, what do you want to do inside those listeners once they have detected a change?

    Maybe a...

     window.resizeTo(screen.width-500,screen.height-500);
    

    which becomes...

    frame_ID.contentWindow.onresize = function(){
    
     this.resizeTo(screen.width-500,screen.height-500); // remove the 500 for full screen
    
    };