【问题标题】:Using Polymer in a Dart Chrome App在 Dart Chrome 应用程序中使用 Polymer
【发布时间】:2014-05-17 10:41:12
【问题描述】:

我正在尝试使用 Dart 和 Polymer 构建一个应用程序。但是聚合物脚本似乎在 web_components/platform.js:32 中使用了 eval()。有没有人设法做到这一点?我尝试更改 CSP,但这首先有所帮助。

谁有一个可行的例子?

问候和感谢 罗伯特

编辑

manifest.json:

{
  "name": "Animatr app",
  "version": "1",

  "author": "Robert Hartung",

  "manifest_version": 2,

  "icons": {"128": "animatr_icon.png"},

  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },

  "content_security_policy": "script-src 'self' unsafe-inline;"
}

main.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Animatr app</title>
    <link rel="stylesheet" href="animatr_chrome_app.css">
    <link rel="import" href="packages/polymer/polymer.html">
  </head>
  <body>

    <script src="packages/chrome/bootstrap.js" defer></script>
    <script src="my_chrome_app.dart" type="application/dart;component=1"></script>    
  </body>
</html>

错误:

Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback. (chrome-extension://ofkfcbfhgkoglbgldcdokficikimdjji/packages/web_components/platform.js:32)
Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

pubspec.yaml:

name: animatr_app
description: A sample Chrome packaged application
dependencies:
  chrome: any
  polymer: any
transformers:
- chrome
- polymer:
    entry_points:
    - app/animatr_chrome_app.html
    csp: 'true'

应用无法正确识别聚合物元素。

【问题讨论】:

  • 能否请您添加更多信息(错误消息,pubspec.yaml,包结构,manifest.json,...)
  • 它在 Dartium 上运行良好吗? (无需构建到 js)
  • 我只尝试过纯飞镖和铬。从飞镖编辑器启动应用程序。
  • 我编辑了我的帖子以向您展示代码。
  • 我不知道细节,但是需要一个名为 Vulcanizer 的 Polymer 工具来使 Polymer CSP 兼容。我认为这个想法是将所有组件连接到一个包含中,以避免需要动态包含更多 JavaScript。

标签: dart google-chrome-app dart-polymer


【解决方案1】:

我一直在努力解决同样的问题,试图将简单的向导生成的聚合物应用程序(带有计数器的应用程序)作为 chrome 打包应用程序运行。 我终于设法至少运行了它的 javascript 构建版本,试图了解 csp 问题。 由于许多原因,它听起来无法运行 native,因为打包的应用程序是从文件系统加载的,而不是通过 pub serve 加载的。从web/build 加载未打包的扩展 并没有按原样工作。解决方案是从生成的 html 文件中加载 xxx.html_bootstrap.dart.precompiled.js 而不是 xxx.html_bootstrap.js 我所做的是:

  • 删除 chrome 转换器,因为聚合物转换器已经将 dart 脚本引用替换为 javascript 脚本引用
  • 在聚合物变压器选项中添加 csp: true(尽管没有它确实可以工作)
  • 为了避免手动更改生成的 html 来加载 precompiled.js 版本,我编写了一个简单的转换器。
  • 然后我可以在 Chrome 中加载(不必是 Dartium,因为它是 javascript)一旦我运行 pub build(需要发布模式),我就可以从 web/build 加载解压扩展程序

我现在只是收到这个警告:

Deprecation: Automatic generation of output for Content Security
Policy is deprecated and will be removed with the next development
release. Use the --csp option to generate CSP restricted output.

这意味着我可能必须尽快找到新的破解方法...

【讨论】:

    【解决方案2】:

    您可以查看 https://github.com/dart-lang/spark/tree/master/ide 以获取 Dart Chrome-Packaged-App 示例。他们还大量使用聚合物。

    也许这与https://github.com/Polymer/polymer/issues/252有关

    这似乎是相关的 Dart 错误https://code.google.com/p/dart/issues/detail?id=17409

    【讨论】:

    • 这肯定是相关的,谢谢 Günter!让我们看看我们是否能找到解决方案:)
    猜你喜欢
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多