在Manifest V2下的Google Chrome扩展程序中使用YouTube的PlayerAPI可以正常工作,但会产生大量警告

Sea*_*son 5 javascript iframe youtube-api google-chrome-extension youtube-javascript-api

在v2下使用Google Chrome扩展程序中的YouTube播放器API时,我收到了一些非严重错误消息.

我的content_security_policy中有以下定义:

"content_security_policy": 
    "script-src 'self' https://www.youtube.com/player_api https://s.ytimg.com/yt/jsbin/www-widgetapi-vfl9Ni0ki.js; object-src 'self'"
Run Code Online (Sandbox Code Playgroud)

第一个引用player_api依赖于widgetapi.如果您导航到player_api网址并查看其内容,这一点很清楚:

if (!window['YT']) {var YT = {};}if (!YT.Player) {(function(){var s = 'https:' + '//s.ytimg.com/yt/jsbin/www-widgetapi-vflU5wlSl.js';var a = document.createElement('script');a.src = s;a.async = true;var b = document.getElementsByTagName('script')[0];b.parentNode.insertBefore(a, b);YT.embed_template = "\u003ciframe width=\"425\" height=\"344\" src=\"\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e";})();}
Run Code Online (Sandbox Code Playgroud)

这一切都很好.我的内容安全政策有效,YouTube视频加载成功.然而,在幕后,我看到了很多警告:

在此输入图像描述

我的理解是,无法删除或隐藏消息"不安全的JavaScript尝试使用URL访问框架...".如果有人知道如何压制此消息,我会非常感兴趣!

但是,我对"无法发布消息到..."错误没有任何了解.一个快速的谷歌出现了一些遇到类似问题的人,但我没有看到任何解决方案.widgetAPI已经缩小了 - 很难看到实际发生了什么.

我本来应该收到这些错误吗?有没有办法减轻错误?或者甚至压制它们(因为它们似乎没有真正破坏任何东西)?

Sea*_*son 3

通过使用 $(window).load() 而不是 $(document).ready 包装 YTPlayer 实例化代码,可以解决无法将消息发布到http://www.youtube.com的问题。

Uncaught TypeError: Cannot call method 'apply' of null 通过提供 onReady、onStateChange 和 onError 函数解决。您不能为这些回调之一提供 null。