我有一个应用程序,它基本上只是内部公司网站的包装器。这个想法是在我的 Chromebook 上自己的窗口中轻松加载网站,这样当 RAM 变低时它就不会卸载。
我有一个非常简单的应用程序,它有一个使用整个应用程序窗口的 WebView。问题是,每当我从窗口切换回来时,webview 就会失去焦点。这特别烦人,因为它是一个聊天应用程序,我想在 alt-tab 键回到窗口后立即开始交谈。
每次窗口获得焦点时,我都会关注 webview,但是 Window(来自 chrome.windows)和 AppWindow(来自 chrome.app.window)之间的断开连接使得这不简单。我需要的事件只存在于 Window 对象中,但我只能明确获得当前的 AppWindow。理论上,当应用程序首次启动时,我可以得到当前活动的窗口,但这似乎很老套且不可靠。
索引.html
<!DOCTYPE html>
<html>
<head>
<title>Chat App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<webview src="https://example.com/" id="chat"></webview>
</body>
</html>
背景.js
chrome.app.runtime.onLaunched.addListener(function(launchData) {
chrome.app.window.create(
'index.html',
{
id: 'chat'
}
);
});
样式.css
让 webview 占用整个窗口有点棘手;我不得不使用一些多余的 CSS 属性来让它正常工作。
html, body {
margin: 0;
padding: 0;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
#chat {
border: 0 none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}