0

Currently i'm developing google chrome extension. I tried using the below code

chrome.action.onClicked.addListener(async () => {
      chrome.windows.create({
        url,
        type: "panel",
        height:650,
        width: 400,
        focused: true,
        left:1000,
        top:100
      });
  });

I need to resize already existing window to half of the screen and open the new window on the other side

Sowmiya C
  • 21
  • 4
  • If the OS is Windows, it is necessary to correct width, height and left. This tutorial does just that. [How to make Chrome Extension 17 Window Size](https://youtu.be/7zMdJlQpY24) – Norio Yamamoto Feb 10 '23 at 06:15

3 Answers3

2
chrome.browserAction.onClicked.addListener(async () => {
  chrome.windows.getCurrent((currentWindow) => {
    chrome.windows.update(currentWindow.id, {
      width: currentWindow.width / 2,
    });
    chrome.windows.create({
      url,
      type: "panel",
      height: 650,
      width: currentWindow.width / 2,
      focused: true,
      left: currentWindow.left + currentWindow.width / 2,
      top: 100,
    });
  });
});

This will first get the current window using chrome.windows.getCurrent and then update its width to half the screen width. After that, it will create a new window with the same height as the original window, but with half the width of the screen, starting from the right side of the original window.

Mert
  • 97
  • 2
0

Try using this to resize the existing window: (based on my reading of https://developer.chrome.com/docs/extensions/reference/windows/)

chrome.windows.getCurrent(window => window.width /= 2);
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
-1

The key to making the above function work = chrome.windows.update(currentWindow.id, {width: currentWindow.width / 2});

is that it won't work if the state is maximized or fullscreen so you also have to account for that and do: chrome.windows.update(currentWindow.id, {state: "normal", width: 900})

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '23 at 20:37