MozillaZine

Bookmarklet to fit window to contents

User Help for Mozilla Firefox
user2018
 
Posts: 43
Joined: September 23rd, 2018, 11:07 am

Post Posted May 17th, 2020, 1:54 am

I was searching for a bookmarklet that is able to adjust the window size of the browser to the actual contents in the current webpage. I saw some bookmarklets that adjust the size to a given one, but no solution that is able to adjust to the size needed to show the current content in a compact way (without leaving unused space in the window). For example at https://www.20minutes.fr/ if you window's size is large you will see a large amount of unused space on the right and left, that you can avoid my manually resizing the window.

ElTxolo

User avatar
 
Posts: 2532
Joined: July 30th, 2007, 9:35 am
Location: Localhost

Post Posted May 17th, 2020, 7:10 am

user2018 wrote:I was searching for a bookmarklet that is able to adjust the window size of the browser to the actual contents in the current webpage. I saw some bookmarklets that adjust the size to a given one, but no solution that is able to adjust to the size needed to show the current content in a compact way (without leaving unused space in the window). For example at https://www.20minutes.fr/ if you window's size is large you will see a large amount of unused space on the right and left, that you can avoid my manually resizing the window.


    Image

    Bookmarklet Fit to Width

    Code: Select all
    javascript:(function(){function%20t(f){a=d.createNodeIterator(d,1,f,false);while(a.nextNode()){}}var%20d=document;t(function(e){x=e.offsetLeft;l=e.offsetParent;while(l!=null){x+=l.offsetLeft;l=l.offsetParent}var%20w=d.documentElement.clientWidth-x;var%20s=e.style;if(s.marginLeft)w-=s.marginLeft;if(s.marginRight)w-=s.marginRight;if(s.paddingLeft)w-=s.paddingLeft;if(s.paddingRight)w-=s.paddingRight;if(s.borderSize)w-=s.borderSize;w-=d.defaultView.innerWidth-d.documentElement.offsetWidth;if(e.tagName=='IMG'){h=e.clientHeight*w/e.clientWidth;s.maxHeight=h}s.maxWidth=w+'px'})})();




  • Before:

    Image

  • After:

    Image










    Cheers!!! Image
How to Ask Questions The Smart Way - How to Report Bugs Effectively ;)
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20200426 SeaMonkey/2.53.2
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20200604 SeaMonkey/2.53.4

user2018
 
Posts: 43
Joined: September 23rd, 2018, 11:07 am

Post Posted May 17th, 2020, 8:27 am

Thank you very much, that is an interesting approach. However, I was looking for the opposite kind of solution: instead of enlarging the content to fit the width of the window, I would like to adjust the width of the window according to the actual content.

jscher2000

User avatar
 
Posts: 10933
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted May 17th, 2020, 2:27 pm

There are some restrictions on scripts resizing windows in Firefox. Click the little box under Firefox for a compatibility note:

https://developer.mozilla.org/docs/Web/API/Window/resizeTo#Browser_compatibility

user2018
 
Posts: 43
Joined: September 23rd, 2018, 11:07 am

Post Posted May 18th, 2020, 12:53 am

Thank you for the tip. Then I think that it could be possible if the bookmarklet opens the current tab again in a new window (with window.open) and then resizes it.

ElTxolo

User avatar
 
Posts: 2532
Joined: July 30th, 2007, 9:35 am
Location: Localhost

Post Posted May 18th, 2020, 6:37 am

Thank you very much ...

    You're welcome!! ;)
How to Ask Questions The Smart Way - How to Report Bugs Effectively ;)
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20200426 SeaMonkey/2.53.2
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20200604 SeaMonkey/2.53.4

jscher2000

User avatar
 
Posts: 10933
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted May 18th, 2020, 9:34 am

user2018 wrote:Thank you for the tip. Then I think that it could be possible if the bookmarklet opens the current tab again in a new window (with window.open) and then resizes it.

I haven't tried that myself, but it sounds like it is worth a shot if that wouldn't a problem on that site.

user2018
 
Posts: 43
Joined: September 23rd, 2018, 11:07 am

Post Posted May 29th, 2020, 6:10 am

I found this, which works well even though it is not a bookmarklet:
resizemybrowser.com

I'm not sure if I am on the right path because even if I achieve transforming that idea into a bookmarklet then there is the issue of how could I determine the right width size for the given contents in the window. Any idea or suggestion is welcome.

Return to Firefox Support


Who is online

Users browsing this forum: Bing [Bot] and 5 guests