首页 > 解决方案 > Chrome 扩展:拒绝加载脚本,因为它违反了以下内容安全策略指令:“script-src 'self'”

问题描述

我正在尝试将游戏作为扩展程序添加到 Chrome 网上应用店,但我遇到了一些问题。游戏是用 Unity3D 制作的。

错误:

拒绝加载脚本“blob:chrome-extension://laacabdjcfgafjkeclplanjohdbpapgn/88f06275-2339-4218-a7c4-ab954cbaafdc”,因为它违反了以下内容安全策略指令:“script-src 'self'”。请注意,'script-src-elem' 没有明确设置,因此 'script-src' 用作后备。

触发错误的代码:

function f() {
        return d("frameworkUrl").then(function(e) {
            var t = URL.createObjectURL(new Blob([e], {
                type: "application/javascript"
            }));
            return new Promise(function(e, n) {
                var r = document.createElement("script");
                r.src = t, r.onload = function() {
                    var n = unityFramework;
                    unityFramework = null, r.onload = null, URL.revokeObjectURL(t), e(n)
                }, document.body.appendChild(r), c.deinitializers.push(function() {
                    document.body.removeChild(r)
                })
            })
        })
    }

确切的行:

 }, document.body.appendChild(r), c.deinitializers.push(function() {

图片: 开发者模式下 Chrome 扩展仪表板出现错误

该扩展使用Manifest Version 3。没有使用外部脚本/资源。我尝试了几天来解决这个问题,但我无法做到。

有没有人尝试过这样的事情并且可以帮助我?或者,也许你有一个想法……我愿意接受。

谢谢!:)

标签: unity3dgoogle-chrome-extensionmanifestcontent-security-policymanifest.json

解决方案


您的代码尝试将外部脚本添加t到扩展页面。出于安全原因,它在清单 V3 中在概念上是被禁止的。实际上,CSP 指令script-src 'self'禁止这样做。

https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#content-security-policy

您也不能在内容脚本中执行外部代码。

https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings

如果要执行外部代码,可以在沙箱页面中执行。

https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/


推荐阅读