我目前正在开发一个 Chrome 扩展程序,我试图在 Crunchyroll 上跳到特定的分钟,具体取决于它是填充物还是大炮。奇怪的是,当我手动检查“Player0”元素(媒体播放器)并通过开发工具扩展它时,我的代码按预期工作。但是,如果不手动检查和扩展元素,就会发生错误。
我很想知道这种行为的根本原因。有人可以告诉我为什么检查元素使我的代码正常工作,否则就会失败?
通过控制台执行时,虽然doom中所有元素都已加载,但如果不手动输入inspect element并扩展则不起作用
镀铬控制台:
document.getElementById('player0').currentTime = 200
错误:
VM207:1 Uncaught TypeError: Cannot set properties of null (setting 'currentTime')
at <anonymous>:1:48
(anonymous) @ VM207:1
我正在寻找的元素确实存在,并且可以通过 html 中的 id="player0" 找到:
<video id="player0" playsinline="" src="blob:https://static.crunchyroll.com/e6cc3673-0c06-4d6f-a8eb-b67a66445bcc" style="display: flex; height: 100%; height: 100%; height: 100%; width: 100%;"></video>
编辑: 根据我的观察,该视频嵌入在
<iframe>
中,并且由于“政策和安全”,您不允许从其他来源访问它。这意味着我没有办法快进时间。有没有单独的方法来实现这一目标?我遇到的错误是:
Uncaught DOMException: Failed to read a named property 'document' from 'Window': Blocked a frame with origin "https://www.crunchyroll.com" from accessing a cross-origin frame.
at <anonymous>:1:28
您可以阅读更多关于同源政策
您正在尝试使用以下方式访问父文档中的播放器...
document.getElementById('player0').currentTime = 200
...但是你说播放器位于该文档的 iframe 内,因此它应该...
Iframe_ID.contentWindow.document.player0.currentTime = 200;
为了让您更轻松地操作播放器,请为其设置一个标识符,如下所示:
var V=Iframe_ID.contentWindow.document.player0;
现在你可以...
V.play();
V.pause();
V.stop();
V.currentTime = 200;
等等...