【问题标题】:[CRX]: didn't allow my js file in the extension[CRX]:扩展名中不允许我的 js 文件
【发布时间】:2012-10-12 05:56:24
【问题描述】:

这是我的应用程序代码:

manifest.json 文件:

{
 "name": "YouTradeFx Debugger",
 "version": "1.0",
 "manifest_version": 2,
 "description": "This appliction allow YoutradeFX R&D team to debugging thier applications, by using few tools",
 "browser_action": {
   "default_popup": "app.html"
 }
}

app.html 文件:

<html>
<head>
 <title>Source of the application</title>
 <script src="jquery.js"></script>
 <script src="app.js"></script>
</head>
<body style="width: 350px;">
  <span style="display: none;">
   <button id="Http">Send Requests</button>
    <button id="Cookie">Add Lead Params</button>
    <button id="Crm">CRM Faliure</button>
</span>
 <div id="Content">
 <table id="cons">
  <tr>
   <td>Please your username:</td>
   <td><input type="text" id="names" name="user"></td>
  </tr>
  <tr>
  <td><input type="submit" name="send" value="SEND"></td>
 </tr>
 </table>
</div>
</body>
</html>

app.js 文件是:

$(document).ready(function(){

 var HoldUser = $("input#names").val();
 $.get("https://www.mywebsite.com/ChromeExt/crm_buffer.php?uid="+HoldUser,function(data){
   $("div#cons").html(data);
 });

});

但我收到错误消息“拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' chrome-extension-resource:”。”一直……!

为什么不工作?我做错了什么?

【问题讨论】:

  • 错误信息非常明显:您在某处使用内联代码。 app.js 或 crm_buffer.php 是否包含任何内联代码?例如onclick="foo();"&lt;script&gt;...code...&lt;/script&gt;?
  • 不,它只包含一个应该返回的字符串“hello world”...

标签: google-chrome-extension google-chrome-devtools


【解决方案1】:

您正在从 https://www.mywebsite.com/ChromeExt/crm_buffer.php?uid="+HoldUser 加载 HTML(我假设),然后将其注入您的 app.html 页面。当 HTML 被注入时,它被解析并开始加载所有提到的资源(&lt;script&gt;&lt;link&gt;&lt;object&gt;&lt;img&gt; 等)。由于您可能在那里有一些 JavaScript 文件,并且由于默认的 Chrome 扩展程序策略不允许扩展程序从远程位置运行脚本,因此您会收到安全违规错误。

您可以通过让网站的响应更像 API 调用、返回 JSON 或文本而不是 HTML 来解决此问题。如果mywebsite.com 不是您的,或者您不想在那里更改任何内容,您可以将它在CSP 中加载的脚本列入白名单,或解析$.get 中返回的data 变量并删除所有&lt;script&gt; 等之前注入它。

【讨论】:

    【解决方案2】:

    您正在获取页面 https://www.mywebsite.com/ChromeExt/crm_buffer.php?uid="+HoldUser,因此您可能在该页面上有点击、鼠标按下等事件页面。此外,我在 ma​​nifest.json 上看不到任何权限。这也可能会阻止请求。您可以通过将以下行添加到 ma​​nifest.json 来轻松测试这一点>。如果这不起作用,请检查 mywebsite.com 以查看是否损坏。

    "permissions": [
        "http://*/*",
        "https://*/*",
    ],
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2012-03-09
      • 2019-03-18
      • 2017-11-13
      • 2017-08-19
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多