Html 带有谷歌API的Chrome清单

Html 带有谷歌API的Chrome清单,html,google-chrome-extension,google-calendar-api,Html,Google Chrome Extension,Google Calendar Api,我需要一些关于如何获得chrome清单的建议,以便在服务器和应用程序之间进行GoogleAPI对话。当我直接指向应用程序时,应用程序加载良好(不是作为扩展) 但是,我的问题是,当我将其作为扩展加载时,会出现以下错误: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy

我需要一些关于如何获得chrome清单的建议,以便在服务器和应用程序之间进行GoogleAPI对话。当我直接指向应用程序时,应用程序加载良好(不是作为扩展)

但是,我的问题是,当我将其作为扩展加载时,会出现以下错误:

Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' https://apis.google.com/".
该应用程序是为了与谷歌的日历API接口而构建的

这就是我的HTML头的外观(本地):


根据有关配置的文档,以及更具体的

通过在策略中添加“不安全评估”,可以放松针对eval()及其相关函数的策略,如setTimeout(字符串)、setInterval(字符串)和新函数的策略[…]

但是,我们强烈建议不要这样做。这些函数是臭名昭著的XSS攻击向量

(强调矿山)

因此,将“不安全评估”添加到内容安全策略中可能是一个解决方案(但在其他方面肯定不是一个好的解决方案)


使用普通可能是一种更好的方法(如有必要,将
with credentials
属性设置为
true
)。

以下是我的扩展示例。在本例中,我将调用驱动器“垃圾”API

            var xhr = new window['JSONHttpRequest']();

            xhr.open("POST", "https://www.googleapis.com/drive/v2/files/" + id + "/trash", true);
            xhr.setRequestHeader('Authorization', 'Bearer ' + token); // token comes from chrome.identity
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.sendJSON(dmark);

我使用的不是原始的XMLHttpRequest,而是一个JS库,它在XMLHttpRequest对象周围创建一个JSON包装器。

问题出在哪里?当您将其用作扩展时会发生什么情况(与将其用作web应用时相反)?它与Google Calendars API的接口如何?@ExpertSystem我编辑了这个问题并陈述了问题。应用程序拉入日历事件并列出它们。在这一点上,日历url是一个硬编码的公共日历。在我的扩展中,我直接使用XMLhttprequest调用GoogleAPI。这很简单。@pinoyyid您能详细介绍一下您的解决方案吗?这对我来说是全新的。我了解XMLhttprequest的功能,但我在请求中称之为“客户端”,我根本不使用client.js。而是直接从javascript调用底层RESTAPI。我发布了一个带有代码碎片的答案,这一点很好,但正如你暗示的那样,我正试图避免“不安全评估”,因为我正在访问用户数据,因此不希望他们的信息处于危险之中。其他方法是通过
窗口安全使用和传递数据。postMessage
。还有XMLHttpRequest。我喜欢XMLHttpRequest方法,但正如下面提到的@pinoyyid注释,我不太确定如何实现它。请尝试pinoyyid的答案(如果您不想使用库,请尝试使用“raw”
XMLHttpRequest
)。我正在尝试实现原始XHTML HttpRequest。你能看看这是否正确吗?我在这里更新了你的小提琴。xhr.open之后出现了一个伪=。此版本失败403访问未配置,这可能是由于密钥无效,或未注册以使用日历API。
<link href='reset.css' rel="stylesheet" type="text/css">
     <link href='style.css' rel="stylesheet" type="text/css">
    <link href='animate-custom.css' rel="stylesheet" type="text/css">
**<script src="https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en.lm_l25KfNhA.O/m=client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AItRSTM1DJ5OrFPvETO8O24EqSIF_JCoKQ/cb=gapi.loaded_0" async=""></script>**
     <script type="text/javascript" src="jquery.min.js"></script>
     <script src="https://apis.google.com/js/client.js?onload=handleClientLoad" type="text/javascript"></script> 
    <script src="main.js" type="text/javascript"></script>
    <script type="text/javascript" src="moment_langs.js"></script>
{
  "manifest_version": 2,
  "name": "My Dashboard",
  "version": "1.2",
  "content_security_policy": "script-src 'self' https://apis.google.com/; object-src 'self'",
  "permissions": [
    "webRequest",
    "*://*.googleapis.com/*",
    "*://*.apis.google.com/*"
  ],


    "chrome_url_overrides" : {
    "newtab": "index.html"
  }
}
            var xhr = new window['JSONHttpRequest']();

            xhr.open("POST", "https://www.googleapis.com/drive/v2/files/" + id + "/trash", true);
            xhr.setRequestHeader('Authorization', 'Bearer ' + token); // token comes from chrome.identity
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.sendJSON(dmark);