Chrome应用程序:如何更新在Chrome应用程序主窗口中创建的辅助窗口元素的内容

Chrome App : How to update the content of an element of a secondary window created in the main window of a Chrome App?

本文关键字:Chrome 应用程序 窗口 创建 元素 何更新 更新      更新时间:2023-09-26

我想创建一个简单的chrome应用程序,它可以启动一个包含2个按钮的窗口"window.html"。

1/ #btn1 creates a new window, loading "video.html". A video player,
playing "file1.webm".
2/ #btn2 updtates the source of the video from "file1.webm" to
"file2.webm".

第一部分很琐碎:)

第二部分很棘手。

有可能吗

你可以在下面找到我的文件。

谢谢:)

<!DOCTYPE html>
<html >
<head>
	<title>Chrome : Multiple Window</title>
	<link href="./css/main.css" rel="stylesheet">
	<script src="./js/jquery.min.js"></script>
	<script src="./js/test.js"></script>
</head>
<body>
	<button id="btn1" type="button" >Launch video Player</button>
	<button id="btn2" type="button" >Update Video</button>
</body>
</html>

$(document).ready(function() {
	$("#btn1").click(function(){
	chrome.app.window.create('video_window.html', {"width":1280, "height": 720});
});
$("#btn2").click(function(){
		$('#myvideo video source').attr('src', './video/avatar.webm');
	});
});

<!DOCTYPE html>
<html>
<head>
	<link href="./css/video.css" rel="stylesheet">
</head>
<body>
	<div class="wrapper">
		<video id="myvideo" autoplay loop>
			<source src="./video/the_master.webm" type="video/webm">
			</video>
		</div>
	</body>
</html>

chrome.app.window.create接受一个回调,该回调将与创建的窗口一起调用。您可以存储对此窗口的引用,然后直接在其上执行函数,或者使用window.postMessage与之通信

var videoWindow = null;
$("#btn1").click(function() {
  chrome.app.window.create('video_window.html', {
    "width": 1280,
    "height": 720
  }, function(window) {
    videoWindow = window.contentWindow;
  });
});
$("#btn2").click(function() {
  videoWindow.doSomething('./video/avatar.webm');
});

另一种选择是使用chrome运行时API进行通信:

chrome.runtime.sendMessage("do-stuff")
chrome.runtime.onMessage.addListener(function(e) {
    // do stuff
})