我有一个 Google Chrome 应用程序,它使用 <webview>
组件。
<webview>
内,在访客的 URL 处,使用由 OAuth 客户端管理的弹出窗口和重定向来处理用户身份验证,以响应登录按钮 onclick
事件。
我的问题是,当我单击登录按钮时,没有任何反应。即,没有弹出窗口。没有重定向。没有登录。
如何修复此问题以允许 OAuth 实现所需的弹出窗口和重定向?
窗口.html<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<webview id="webview" src="https://mywebapp.com"></webview>
</body>
</html>
背景.js
chrome.app.runtime.onLaunched.addListener(function() {
runApp();
});
function runApp() {
chrome.app.window.create('window.html', {
state: 'fullscreen'
});
}
var webview = null;
function isSafeUrl(url) {
// This is Hello world test for now; permissions code will go here later
return true;
}
function onPermissionRequest(e) {
e.request.allow();
// This is Hello world test for now; permissions code will go here later
}
function onDomReady() {
webview = document.getElementById('webview');
webview.addEventListener('permissionrequest', onPermissionRequest);
}
document.addEventListener('DOMContentLoaded', onDomReady);
最佳答案
您的脚本需要分为两部分。
后台脚本在与 window.html
不同的文档中运行,Event page 。您的应用程序应如下所示:
// This should be split off to app.js
var webview = null;
function isSafeUrl(url) {
// This is Hello world test for now; permissions code will go here later
return true;
}
function onPermissionRequest(e) {
e.request.allow();
// This is Hello world test for now; permissions code will go here later
}
function onDomReady() {
webview = document.getElementById('webview');
webview.addEventListener('permissionrequest', onPermissionRequest);
}
document.addEventListener('DOMContentLoaded', onDomReady);
然后包括 <script>
新脚本的标记为 window.html
.
由于 Chrome 应用程序中不存在“webview 窗口”这样的概念,因此它会比允许所有内容更为复杂。
你需要捕获the newwindow
event并通过创建新的 <webview>
来处理它,可能在另一个窗口中。这不是很容易,但如果您寻找的话,可能已经有实现了。
要真正进行身份验证,您需要确保两个 webview share a partition .
..如您所见,它比 iframe 复杂得多。
但是!我确实相信您完全走上了一条错误的道路。如果您的任务是获取 OAuth token ,您应该考虑 chrome.identity
API ,具体来说launchWebAuthFlow
.
关于javascript - 如何在 Google Chrome 应用程序中使用 <webview> 允许弹出窗口和重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246111/