简体   繁体   English

如何使用 angular 5 构建 chrome 扩展

[英]how to use angular 5 to build chrome extension

I am trying to build a chrome extension using angular 5. I created a basic angular app using Angular Material and it worked great as an angular app (I used ng build to check).我正在尝试使用 angular 构建一个 chrome 扩展 5. 我使用Angular Material创建了一个基本的 angular 应用程序,它作为 angular 应用程序运行良好(我使用ng build进行检查)。 I tried to move the application to be a chrome extension so I used ng build to create the code and I added the following manifest我试图将应用程序移动为 chrome 扩展,所以我使用ng build创建代码并添加了以下清单

{
   "name": "Getting Started Example",
   "version": "1.0",
   "description": "Build an Extension!",
   "background": {
      "scripts": ["runtime.js","polyfills.js","styles.js","vendor.js","main.js"],
      "persistent": false
    },
    "page_action": {
       "default_popup": "index.html"
     },
    "manifest_version": 2
}

the background scripts are the scripts the angular compiler created and are used to render the app.后台脚本是 angular 编译器创建的脚本,用于呈现应用程序。 I upload my extension but I got this error我上传了我的扩展,但我收到了这个错误

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".未捕获的 EvalError:拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下内容安全策略指令中允许的脚本源:“script-src 'self' blob:文件系统:chrome-extension-resource:”。

what does it mean?这是什么意思? how can I run my angular application as a chrome extension?如何将我的 angular 应用程序作为 chrome 扩展运行?

You need to add this line to the manifest.json for that error to go away:您需要将此行添加到 manifest.json 中,以便将错误更改为 go:

 "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

and remove the following line of code, because that isn't needed:并删除以下代码行,因为不需要:

"scripts": ["runtime.js","polyfills.js","styles.js","vendor.js","main.js"]

To setup a proper workflow is a different story.设置适当的工作流程是另一回事。 Every time you make a change you need to rebuild the project manually.每次进行更改时,您都需要手动重建项目。 This can be some what of a hassle.这可能有点麻烦。

I have created a CLI to support such a workflow (auto watch files, build for production and zip it etc)我创建了一个 CLI 来支持这样的工作流程(自动监视文件、为生产构建和 zip 等)

see: https://github.com/larscom/ng-chrome-extension for more information.有关详细信息,请参阅: https://github.com/larscom/ng-chrome-extension

You no need to add JS files in the script just add reffernce of index.html file of your dist folder It will automatically add all required JS/CSS files from there.您无需在脚本中添加 JS 文件,只需添加 dist 文件夹的 index.html 文件的引用,它会自动从那里添加所有必需的 JS/CSS 文件。

so Just remove this line of code所以只需删除这行代码

"scripts": ["runtime.js","polyfills.js","styles.js","vendor.js","main.js"],

For more information refer here -有关更多信息,请参阅此处 -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM