【问题标题】:Possible to use AngularJS for a Chrome extension's options page?可以将 AngularJS 用于 Chrome 扩展的选项页面吗?
【发布时间】:2012-06-21 22:44:31
【问题描述】:

对于我的 Chrome 扩展的选项页面,我想使用 Angular.js(仅用于选项页面,不适用于扩展的背景 JS 或内容脚本),但将其包含在我的页面中并执行以下操作:

<!doctype html>
<html ng-app>
<head>
  <title>Shortkeys Options</title>
  <script src="../js/jquery.min.js" type="text/javascript"></script>
  <script src="../js/angular.min.js" type="text/javascript"></script>
  <script src="../js/options.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../css/options.css" />
</head>

<body>
  <div ng-controller="OptionsCtrl">
    <div ng-repeat="key in keys"></table>
  </div>
</body>
</html>

...在开发工具控制台中抛出此错误并且没有运行:

Error: Code generation from strings disallowed for this context

我认为这是因为 Angular 试图将标签写入页面或分配内联事件处理程序或运行内联 JS 的东西,is not allowed in Chrome extensions,那么有什么办法解决这个问题吗?例如,我可以告诉 Angular 避免使用内联 JS 吗?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension angularjs


    【解决方案1】:

    您可以通过指定在 CSP 兼容模式下运行的角度来使用 manifest_version: 2。只需将ng-csp 属性添加到面板页面中的&lt;html&gt;

    所以你的 HTML 应该是这样的:

    <!doctype html>
    <html ng-csp ng-app>
    <head>
      <title>Shortkeys Options</title>
      <script src="../js/jquery.min.js" type="text/javascript"></script>
      <script src="../js/angular.min.js" type="text/javascript"></script>
      <script src="../js/options.js" type="text/javascript"></script>
      <link rel="stylesheet" href="../css/options.css" />
    </head>
    
    <body>
      <div ng-controller="OptionsCtrl">
        <div ng-repeat="key in keys"></table>
      </div>
    </body>
    </html>
    

    【讨论】:

    • 这在 Chrome 打包应用程序中如何工作?我读了它是不允许的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2023-02-02
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多