Javascript 在Chrome扩展中使用AngularJS

Javascript 在Chrome扩展中使用AngularJS,javascript,google-chrome-extension,angularjs,Javascript,Google Chrome Extension,Angularjs,我想在Chrome扩展中使用AngularJS,但我有如下错误: Error: SECURITY_ERR: DOM Exception 18 Error: An attempt was made to break through the security policy of the user agent. at new listConnection manifest.json: { "name": "Chrome auditor connection", "version":

我想在Chrome扩展中使用AngularJS,但我有如下错误:

Error: SECURITY_ERR: DOM Exception 18
Error: An attempt was made to break through the security policy of the user agent.
    at new listConnection
manifest.json:

 {
  "name": "Chrome auditor connection",
  "version": "1",
  "icons": { "48": "icone.png",
            "128": "icone.png" },
  "description": "Chrome-auditor-connection is an extension for Google Chrome browser. It ensures that nobody connects to your browser with your profile.",
  "background": {
    "scripts": [
      "chrome_ex_oauthsimple.js",
      "chrome_ex_oauth.js",
      "background.js"
    ]
  },
  "browser_action": {
    "default_title": "Chrome auditor connection",
    "default_icon": "icone.png",
    "default_popup": "index.html"
  },
  "permissions": [
    "storage"
  ],
  "web_accessible_resources": ["index.html"],
  "sandbox": {
     "pages": ["index.html","index.js","angular.min.js"]
  },
  "manifest_version": 2
}
function listConnection( $scope) {      
    $scope.connections = JSON.parse(localStorage['connectionHistory']);
}
index.js:

 {
  "name": "Chrome auditor connection",
  "version": "1",
  "icons": { "48": "icone.png",
            "128": "icone.png" },
  "description": "Chrome-auditor-connection is an extension for Google Chrome browser. It ensures that nobody connects to your browser with your profile.",
  "background": {
    "scripts": [
      "chrome_ex_oauthsimple.js",
      "chrome_ex_oauth.js",
      "background.js"
    ]
  },
  "browser_action": {
    "default_title": "Chrome auditor connection",
    "default_icon": "icone.png",
    "default_popup": "index.html"
  },
  "permissions": [
    "storage"
  ],
  "web_accessible_resources": ["index.html"],
  "sandbox": {
     "pages": ["index.html","index.js","angular.min.js"]
  },
  "manifest_version": 2
}
function listConnection( $scope) {      
    $scope.connections = JSON.parse(localStorage['connectionHistory']);
}
我认为JSON.parse()被Chrome“内容安全策略”(CSP)阻止了


您有什么解决方案吗?

您的index.js是按照manifest.json文件中的定义进行沙盒处理的。
“页面将无法访问扩展或应用程序API”
因此它无法访问本地存储。
删除沙盒或使用类似的方法处理沙盒页面。

代码中存在的问题 到目前为止,我在代码共享方面看到了一些问题

  • 您的
    index.html
    正被用作
    默认弹出窗口
    可访问的web\u资源
    沙盒页面
    。它在功能上是不正确的,您想要开发什么
  • 为什么要将
    angular.min.js
    放在沙箱位置
  • 是由
    index.html
    使用的
    index.js
    ,如果是,则无需显式列出它
  • 存储是每个域的一个对象,您的
    localstorage
    不同于沙盒和其他页面

    • 有两个答案供您选择:

    • ng csp
      添加到您的
      标记中,如下
      。看到这个了吗
    • 将此行添加到
      manifest.json
      文件

    • “内容安全策略”:“脚本src‘self’‘不安全评估’;对象src‘self’”

      本地存储不是扩展或应用程序API,它有自己的
      本地存储
      ,顺便说一句,这个链接有什么帮助?@Sudarshan-Chrome Sandbox StorageAPI是一个库,它使用postMessage将Chrome存储数据从非沙盒包装传递到沙盒iframe,沙盒iframe既不能使用localStorage也不能使用Chrome存储。Alexander建议使用storageAPI,因为它提供了一种抽象,允许在将网页移植到沙盒iframe时替换window.localStorage,而无需重写代码。然而,我不确定它是否能与数组表示法一起工作,因为我在设计它时没有考虑到这一点。因此询问者需要使用
      getItem
      方法。希望这有帮助!