个jQuery的AJAX请求一个HTML文件本身的Chrome扩

个jQuery的AJAX请求一个HTML文件本身的Chrome扩

本文介绍了可一个Chrome扩展的内容脚本,使一个jQuery的AJAX请求一个HTML文件本身的Chrome扩展的一部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Chrome扩展 的意愿内容脚本,在特定页面,额外注内容到页面将功能添加到现有站点。

I am working on a Chrome extension content script that will, on specific pages, inject additional content onto the page to add functionality to an existing site.

我有一个HTML文件,它是在我的Chrome扩展,它包含的模板将添加的内容。我希望我可以使用jQuery来检索HTML模板。我最初尝试一个简单的jQuery AJAX请求,像这样的:

I have an HTML file that is in my Chrome extension that contains the template for content that will be added. I am hoping I can use jQuery to retrieve that HTML template. I initially tried a simple jQuery AJAX request, like this:

var url = "template.html";

$.get(url,function(data, textStatus, jqXHR){
    console.log("Got the template!");
    console.log(data);
},"html");

然而,这将导致以下的错误:没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地'铬扩展:// gmjipglelhnlbakmlobgffnpceaalnnc,因此没有允许访问

However, this results in the following error:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'chrome-extension://gmjipglelhnlbakmlobgffnpceaalnnc' is therefore not allowed access.

这听起来像一个 Chrome扩展权限问题,以及相关的权限似乎是 [方案]:[主持人] / * 权限问题。我的分机号是 gmjipglelhnlbakmlobgffnpceaalnnc ,所以我尝试添加权限铬扩展:// gmjipglelhnlbakmlobgffnpceaalnnc / (其中根据该是preFIX为完全合格的Chrome扩展程序打包资源)。

This sounds like a Chrome Extensions permissions issue, and the relevant permission seems to be the [scheme]:[host]/* permissions issue. My extension ID is gmjipglelhnlbakmlobgffnpceaalnnc, so I tried adding permission for chrome-extension://gmjipglelhnlbakmlobgffnpceaalnnc/ (which according to this is the prefix for fully qualified Chrome Extension packaged resources).

这是没有工作,因为Chrome浏览器告诉我的权限。铬扩展:// gmjipglelhnlbakmlobgffnpceaalnnc /'未知或URL模式的格式不正确

That didn't work, as Chrome told me Permission 'chrome-extension://gmjipglelhnlbakmlobgffnpceaalnnc/' is unknown or URL pattern is malformed.

一个多一点的阅读,我发现有一个特定的设施,在这里扩展产地,要求打包的资源。这一切都很好,但我会preFER使用一个统一的API请求的资源,即要能够使用jQuery拉下我的模板文件,因为我一直在做定期香草的网络应用程序(也就是说,不是Chrome扩展程序)。

A little more reading and I found that there is a specific facility, described here under extension origin, to request packaged resources. That's all well and good, but I would prefer to use a uniform API to request resources, i.e., to be able to use jQuery to pull down my template files, as I've been doing for regular "vanilla" web apps (i.e., not Chrome extensions).

有没有一种方法来配置我的Chrome扩展权限,使我可以通过jQuery的AJAX设施要求扩展名的起源的资源?

Is there a way to configure my Chrome extension permissions so that I can request "extension origin" resources via jQuery's AJAX facilities?

推荐答案

发现我的答案。你必须明确地添加或者个人扩充资源,或者符合您的扩展资源的模式,在清单通过 web_accessible_resources

Found my answer. You have to explicitly add either the individual extension resources, or patterns that match your extension resources, in the manifest via web_accessible_resources.

一旦你做到了这一点,你可以建立基于URL chrome.extension.getURL(resourcePath)

Once you've done this, you can construct the URL using chrome.extension.getURL(resourcePath).

下面是从我的清单中的摘录:

Here's an excerpt from my manifest:

"web_accessible_resources" : [
    "*.html"
]

和这里的code我用请求我的模板文件:

And here's the code I'm using to request my template file:

var url = chrome.extension.getURL("template.html");
$.get(url,function(data, textStatus, jqXHR){
    console.log("Got the template!");
    console.log(data);
},"html");

这篇关于可一个Chrome扩展的内容脚本,使一个jQuery的AJAX请求一个HTML文件本身的Chrome扩展的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:34