如何调整已存在窗口大小并在右侧打开新的Chrome窗口

huangapple go评论57阅读模式
英文:

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

问题

目前我正在开发谷歌Chrome扩展程序。我尝试使用以下代码:

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

我需要将已存在的窗口调整大小为屏幕的一半,并在另一侧打开新窗口。

英文:

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

答案1

得分: 2

这段代码首先使用 chrome.windows.getCurrent 获取当前窗口,然后将其宽度更新为屏幕宽度的一半。然后,它会创建一个新窗口,高度与原始窗口相同,但宽度为屏幕宽度的一半,从原始窗口的右侧开始。

英文:
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.

答案2

得分: 0

尝试使用以下代码来调整现有窗口的大小:(基于我对 https://developer.chrome.com/docs/extensions/reference/windows/ 的阅读)

chrome.windows.getCurrent(window => window.width /= 2);
英文:

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);

答案3

得分: -1

以上函数能够正常工作的关键是:

chrome.windows.update(currentWindow.id, {width: currentWindow.width / 2});

但是如果窗口状态为maximizedfullscreen,它将无法正常工作,所以你还需要考虑这一点并执行以下操作:

chrome.windows.update(currentWindow.id, {state: "normal", width: 900})

英文:

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})

huangapple
  • 本文由 发表于 2023年2月10日 13:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定