Skip to content
Advertisement

How to resize chrome extension window in javascript?

I am trying to resize my chrome extensions window but I do not know how to do it.

JavaScript
JavaScript

manifest.json { “name”: “Test”, “version”: “1.0”, “description”: “This extension”, “author”: “Test”, “browser_action”: { “default_popup”: “index.html”, “default_title”: “WRB” }, “manifest_version”: 2 }

Advertisement

Answer

To resize the popup window you just need to resize your content. The best way to do that is by editing the width and/or height of your <body> element.

For example:

JavaScript

If that isn’t working make sure you have a DOCTYPE declaration as per this question.

EDIT:

Now that I see the code you are using here is an updated solution.

JavaScript
JavaScript

It looks like the problem you were having is that the documentElement (Your <html> tag) was never resizing when the rest of the content shrunk. I just added document.documentElement.style.height = "10rem"; to the callback function to also decrease the size of the document.

In addition but not related to this specific issue, I would not recommend using document.write() because it will cause you problems later down the line especially with overwriting your original page content. To fix this I appended your HTML tags to the innerHTML of your document’s <body>. I also moved the <script> tag for your popup script to the body so that it runs only after the body has been loaded.

How to Find A Problematic Element

Since you mentioned that while this works on the sample code, it does not in your actual code here is how you can go about finding the problematic element(s) in your actual project.

  1. Open your popup window.
  2. Right click within the window and click inspect.
  3. This will bring up dev tools.
  4. Hover over your elements in the elements panel, they will be highlighted.
  5. Look for an element that is taking up more space than your desired height. It is best to start from the outermost element and work your way in.
  6. Once you identify an element that is causing an issue, try changing the size of that element on the click of the button to test if the window resizes with it.
  7. If it works, good news, you found your problem. If it doesn’t, repeat steps 4-6 with the next outermost element until you find the element that is causing problems.

Here is a GIF showcasing how to open and view elements in dev tools:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement