Search code examples
javascriptgoogle-chrome-extensiongoogle-chrome-appchrome-extension-manifest-v3

How to resize already exising window and open new chrome window on the right side


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


Solution

  • 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.