简体   繁体   English

无法在Chrome扩展程序内xhr文件

[英]Can't xhr file inside of Chrome extension

I'm building out an extension and I'm trying to keep it well structured. 我正在构建扩展,并试图保持其结构良好。 Part of it will use a templating system of some type (Mustache, Handlebars, etc). 它的一部分将使用某种类型的模板系统(小胡子,把手等)。 Note that I'm already aware of having to use a specific non-eval version of what ever library I go with. 请注意,我已经意识到必须使用我所使用的库的特定非评估版本。

The problem is that from within the app.js script (the core logic) I cannot XHR to load the contents of the files in /templates (see below). 问题是从app.js脚本(核心逻辑)中,我无法XHR加载/ templates中文件的内容(请参见下文)。

The load event never fires, and when I examine with onreadystatechange it immediate jumps to state 4 with a status code of 0. (state 4 and status 200 are what we want). load事件永远不会触发,当我使用onreadystatechange进行检查时,它会立即跳转到状态4,状态代码为0。(我们想要的是状态4和状态200)。

Getting the Chrome url for the file works, eg 获取文件的Chrome网址有效,例如

chrome.extension.getURL('templates/view1.html')
//chrome-extension://hdhpjlpbepobcdgnkklnakdpoojaahjg/templates/view1.html 

I suspect there's something in my manifest.json that isn't configured right. 我怀疑manifest.json中有未正确配置的内容。 I've read through the CSP docs but nothing is jumping out at me why accessing files local to the extension should be denied. 我已经阅读了CSP文档,但是没有什么让我惊讶的原因,为什么应该拒绝对扩展名本地文件的访问。 Note that the XHR requests don't throw errors, they just never return data. 请注意,XHR请求不会引发错误,它们永远不会返回数据。

The structure of the app is like this: 该应用程序的结构如下:

/manifest.json
/src
    app.js
    style.css
/libs
    jquery.js
    mustache.js

/templates
    view1.html
    view2.html

Manifest.json Manifest.json

{
  "name": "Test Extension",
  "version": "0.0.1",
  "manifest_version": 2,
  "icons": {
//    "16": "",
//    "48": ""
//    "128": ""
  },
//  "default_locale": "en",
  "permissions": [
    "contentSettings",
    "http://*/*",
    "https://*/*"
  ],
  "content_scripts": [
    {
      "matches": [
        "https://www.google.com/search*"
      ],
      "css": [
        "src/style.css"
      ],
      "js": [
        "src/app.js",
        "libs/jquery.js",
        "libs/mustache.js"
      ],
      "run_at": "document_start"
    }
  ]
}

您需要在清单的web_accessible_resources部分中列出要加载的文件。

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

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