使用Firefox 57量子和Web扩展API,我想制作一个能够在多个屏幕上运行的扩展程序。此扩展名将用于显示多个屏幕仪表板。 关于它的想法是如此简单。如果检测到两个或多个屏幕,则每个Firefox启动,然后在全屏模式下为每个监视器打开一个新窗口。我可以全屏进行全屏,但是面临在另一个显示器上打开的问题,并检测附加了多少监视器。
使用browser.windows
api创建另一个新选项卡,以下是一些摘要代码:
getCurrentWindow().then((currentWindow) => {
let mirror1 = browser.windows.create({
url: "http://172.20.12.211:8080/ivi",
state: "fullscreen"
});
mirror1.then(() => {
browser.windows.remove(currentWindow.id);
var updateInfo = {
state: "fullscreen"
};
let mirror2 = browser.windows.create({
url: "http://172.20.12.211:8080/ivi",
state: "fullscreen"
});
mirror2.then((nextWindow) => {
var updateInfo = {
left: 1366
};
browser.windows.update(nextWindow.id, updateInfo);
});
});
});
显然,我上面的解决方案对两个显示器进行了硬编码,并将
left
中,是否有任何API或更好的方法来检测附着多少监视器并检查其分辨率? Web扩展API是否具有用于检测多个监视器和分辨率值的功能?如果没有,有什么可能的解决方法?
没有容易可用的扩展API可以回答您的问题,但是有足够的构建块可以另一种方式回答问题。
当前窗口相对于显示器的位置可通过:
window.screenX
window.screenY
screen.width
-Https://developer.mozilla.org/en-us/docs/web/api/screen/widthscreen.height
screen.left
-Https://developer.mozilla.org/en-us/docs/web/api/screen/leftscreen.left
-Https://developer.mozilla.org/en-us/docs/web/api/screen/top
screen.top
privacy.resistFingerprinting
扩展API查询。
如果您创建一个虚拟窗口并在屏幕上移动它,则可以使用上述API来推断有关屏幕的信息,并最终构建可用显示和尺寸的完整视图。这是打印上述信息的示例扩展名。您可以单击按钮打开窗口,然后手动移动窗口。
可以通过打开一个非常狭窄的弹出窗口来自动化这一点,然后用chrome.windows.get
chrome.windows.update
manifest.json
{
"name": "Display info",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Open popup to display info"
}
}
background.js
chrome.browserAction.onClicked.addListener(function() {
chrome.windows.create({
url: chrome.runtime.getURL('/display.html'),
});
});
display.html
<meta charset="utf-8">
<body style="white-space:pre">
<script src="display.js"></script>
display.js
setInterval(function() {
document.body.textContent =
'\nWindow offset: ' + window.screenX + ',' + window.screenY +
'\nScreen size : ' + screen.width + 'x' + screen.height +
'\nScreen offset: ' + screen.left + ',' + screen.top;
chrome.windows.get(chrome.windows.WINDOW_ID_CURRENT, function(win) {
document.body.insertAdjacentText('beforeend',
'\nExtension window offset : ' + win.left + ',' + win.top);
});
}, 200);