我可以在 Chrome 扩展程序中检测全屏吗?

yar*_*onf 1 fullscreen google-chrome-extension content-script

我有一个 Chrome 扩展程序(特别是“内容脚本”),我想在其中检测我正在监视/更改的页面是否处于全屏状态。我已经尝试了几个 API 以及“screenfull”库,但到目前为止都没有运气。有任何想法吗?

谢谢你的帮助!

Rob*_*b W 5

如果要检测页面是否使用了 Fullscreen API进入全屏模式,只需勾选document.webkitIsFullscreen

如果您想要一种可靠地检测全屏模式的通用方法,chrome.windowsAPI 是您唯一的选择。由于此API对内容脚本不可用,因此您需要使用消息传递API与后台事件页面进行交互

示例:内容脚本

function isFullScreen(callback) {
    chrome.runtime.sendMessage('getScreenState', function(result) {
        callback(result === 'fullscreen');
    });
}
// Example: Whenever you want to know the state:
isFullScreen(function(isFullScreen) {
    alert('Window is ' + (isFullScreen ? '' : 'not ') + 'in full screen mode.');
});
Run Code Online (Sandbox Code Playgroud)

示例:背景/事件页面

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if (message === 'getScreenState') {
        chrome.windows.get(sender.tab.windowId, function(chromeWindow) {
            // "normal", "minimized", "maximized" or "fullscreen"
            sendResponse(chromeWindow.state);
        });
        return true; // Signifies that we want to use sendResponse asynchronously
    }
});
Run Code Online (Sandbox Code Playgroud)