【问题标题】:How make chrome.windows.create show the window on the same display?如何让 chrome.windows.create 在同一显示器上显示窗口?
【发布时间】:2021-11-17 10:55:00
【问题描述】:

我正在制作一个 chrome 扩展程序,它会在单击注入页面的按钮时打开一个弹出窗口。 除了窗口显示在主显示器上这一事实之外,一切都完美无缺,而不是调用弹出窗口的浏览器窗口打开。 我该如何处理?使用带有自定义显示排列的 Mac(如果相关)。我希望窗口只引用它起源的窗口的边界。

据我了解,这涉及到一些精细的显示像素计算,但如何在这种情况下获得显示尺寸? 我希望你明白我在说什么,并且我包含了所有相关的细节。我在下面包含了用于打开窗口的代码:

    chrome.windows.getCurrent((tabWindow) => {
        const popupWidth = 400;
        const popupHeight = 400;
        const leftOffset = (tabWindow.width/2)-(popupWidth/2);
        const topOffset = (tabWindow.height/3)-(popupHeight/2); 

        var infoPopupUrl = "someurl";

        chrome.windows.create(
            {
                'url': chrome.runtime.getURL(infoPopupUrl),
                'type': "popup",
                'width': popupWidth,
                'height': popupHeight,
                'left': Math.round(leftOffset),
                'top': Math.round(topOffset),
                'focused': true
            }
        );
    });

感谢大家的帮助。

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

    此链接:https://stackoverflow.com/a/25830431/3218811

    引导我在 screen 对象中找到答案。有availLeftavailTop 属性表示屏幕排列的确切关系。在 Mac 上工作,我必须测试其他平台,但我想它是 Chrome 中的标准:

            const leftOffset = (tabWindow.width/2)-(popupWidth/2)+screen.availLeft;        
            const topOffset = (tabWindow.height/3)-(popupHeight/2)+screen.availTop; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 2011-10-14
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多