Chrome应用程序打开一个页面到另一个页面

Chrome Apps Open one Page into another Page

本文关键字:一个 另一个 应用程序 Chrome      更新时间:2023-09-26

我正在开发HTML5 chrome应用程序

我的问题是:我必须打开另一个页面点击链接

锚标记(<a href="page1.html" target="_blank">)也不工作

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
    <link href="styles/main.css" rel="stylesheet">
</head>
<body>
    <h1>Hello, World!</h1>
    <a href="page1.html" target="_blank">Click Me</a>
</body>
</html>

background.js:

chrome.app.runtime.onLaunched.addListener(function() {
  var screenWidth = screen.availWidth;
  var screenHeight = screen.availHeight;
  var width = 500;
  var height = 300;
  chrome.app.window.create('index.html', {
    id: "helloWorldID",
    bounds: {
      width: width,
      height: height,
      left: Math.round((screenWidth-width)/2),
      top: Math.round((screenHeight-height)/2)
    },
  });
});

如何打开page1.html从应用程序窗口的链接?

有几种方法可以在Chrome应用程序中创建新窗口。首先是简单的window.open,这是你为正常的网页内容所做的(即,你在正常的浏览器选项卡中打开的东西)。这就像一个本机应用程序打开浏览器窗口,使用任何注册的应用程序来处理。html文件扩展名,也就是说,它将在用户的默认浏览器中打开web内容。

接下来是您用来创建应用程序初始窗口的相同的chrome.app.window.create。因此,在您希望启动新窗口打开的链接或按钮上设置一个侦听器,并在其中放置create()调用。这是你在你的情况下想要的,因为你不是试图显示常规的网页内容,而是显示嵌入在你的应用程序中的HTML。

最后,如果你想要窗口。打开的行为,但需要知道它会在Chrome浏览器中打开与应用程序相同的配置文件,见chrome.browser.openTab,其进度你可以跟随https://code.google.com/p/chromium/issues/detail?id=358315。(更新:我刚刚和rpaquay谈过,他说这个API在开发频道中是实时的。)

为了完整起见,我还应该提到Chrome应用程序窗口中的正常web内容,您需要,或者在中打包/嵌入资源,这需要webview:partitions:accessible_resources(详见codereview.chromium.org/308903002)。我想最后一部分还没有上线,所以名字可能会改变。