javascript - 如何在 Google Chrome 应用程序中使用 <webview> 允许弹出窗口和重定向?

标签 javascript google-chrome google-chrome-app

我有一个 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 。您的应用程序应如下所示:

enter image description here

// 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/

相关文章:

javascript - 谷歌浏览器中是否有 <object> 标签的替代品?

javascript - 在 Chrome 扩展程序下拉列表中显示倒计时

javascript - 列出chrome打包应用中的目录内容

javascript - 在 Angular 中,当我收到一些新数据时如何强制模板重新编译?

javascript - 用给定节点包裹单个节点

javascript - 如果选中复选框,则显示 Bootstrap 模式

javascript - 防止函数在 JavaScript 中多次运行

css - 为什么我的图像在 Firefox 和 Chrome 中太左对齐

javascript - 卸载在沙盒 Chrome 应用程序中不可用

android - 如何在 Android 上的 Chrome 中摆脱 fancybox iframe 边框(或滚动条?)