0

晚上好

我是浏览器扩展领域的新手,所以这对某些人来说似乎微不足道,但是......

我一生都无法弄清楚如何在 chrome 扩展中使用“unpkg”。我一直在涉足内容安全策略 (CSP),但我就是不知道如何让它发挥作用。我正在使用清单版本 3,它甚至可能不兼容,但我不知道切换到清单版本 2 是否会成为解决方案,因为许多其他问题和问题随后开始出现。

我曾尝试使用沙盒模式,但我不知道如何测试它,所以我认为我没有正确设置它。

在下面的示例中,我还尝试在<meta>标签的 HTML 头中添加 CSP,但我认为这不起作用。

这是主要错误: 在此处输入图像描述

我尝试将 unpkg 链接(https://unpkg.com/ml5@latest/dist/ml5.min.js)的内容复制/粘贴到一个新文件中,这会产生: 在此处输入图像描述

无论我对 manifest.json 中的 content-security-policy 做什么,这始终是结果。

清单.json

{
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "2.1",
"manifest_version": 3,
"background": {
    "service_worker": "background.js"
},
"permissions": ["storage"],
"content-security-policy": { 
    "extension_pages": "script-src 'self'; object-src 'self'",
    "sandbox": "allow-scripts; script-src 'self' 'unsafe-eval' https://unpkg.com/*"
},
"action": {
    "default_popup": "main.html",
    "default_icon": {
        "16": "/images/get_started16.png",
        "32": "/images/get_started32.png",
        "48": "/images/get_started48.png",
        "128": "/images/get_started128.png"
    }
},
"icons": {
    "16": "/images/get_started16.png",
    "32": "/images/get_started32.png",
    "48": "/images/get_started48.png",
    "128": "/images/get_started128.png"
}

}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sentiment analysis</title>
    <link rel="stylesheet" href="./Resources/css/index.css">
</head>
<body>
    <div>
        <script src="https://unpkg.com/ml5@latest/dist/ml5.min.js"></script>
        <script src="ml5.js"></script>
        <script src="main.js"></script> 
    </div>
</body>
</html>

项目的其余部分在这个 repo 中。

我根本不是一个熟练的程序员,所以所有这些绝对是业余的。我所需要的只是让它工作,而不是让它变得安全或美丽(可能会在以后出现......:D)。

作为记录,我将所有这些都放在本地 javascript 文件中,正是这个 CSP 东西困扰着我。

这是本地 Github 存储库但请注意一些 NSFW 语言,因为我正在尝试在互联网上反转否定句。大多数在 main.html 中。

新清单:

    {
    "name": "Getting Started Example",
    "description": "Build an Extension!",
    "version": "2.1",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "permissions": ["storage"],
    "content-security-policy": { 
        "extension_pages": "script-src 'self'  'unsafe-eval'; object-src 'self'  'unsafe-eval'",
        "sandbox": "allow-scripts; script-src 'self' 'unsafe-eval' https://unpkg.com/*"
    },
    "action": {
        "default_popup": "main.html",
        "default_icon": {
            "16": "/images/get_started16.png",
            "32": "/images/get_started32.png",
            "48": "/images/get_started48.png",
            "128": "/images/get_started128.png"
        }
    },
    "icons": {
        "16": "/images/get_started16.png",
        "32": "/images/get_started32.png",
        "48": "/images/get_started48.png",
        "128": "/images/get_started128.png"
    }
}
4

0 回答 0