拒绝在Dart中加载JS

时间:2015-06-12 18:02:09

标签: javascript dart google-chrome-app content-security-policy

在我的Dart数量网页中,这个脚本:

<script src="https://test.net/test/test.js"></script>  

但是当你启动应用程序时,它会给我以下错误:

  

拒绝加载脚本&#39; https://test.net/test/test.js&#39;因为它违反了以下内容安全策略指令:&#34; default-src&#39; self&#39;铬扩展资源:&#34 ;.请注意&#39; script-src&#39;没有明确设置,所以&#39; default-src&#39;被用作后备。

我还在清单中设置了内容安全策略,但没有任何结果:

"content_security_policy": "script-src 'self' https://test.net/test/"

有谁知道如何解决?

修改

Dart中的这个答案Loading external javascript in google chrome extension不起作用。

清单:

..."permissions":["tabs","https://test.net/test/"],
"content_security_policy": "script-src 'self' https://test.net/test/; object-src 'self'",
"content_scripts": [
{
  "js": ["https://test.net/test/test.js"]
}
]...

代码:

JsObject spark=context['spark'];
print(spark);
spark.callMethod('login',["{username: 'test@email.com', password: 'test'}"]);

结果:

  

     

异常:null对象没有方法&#39; callMethod&#39;。

Dart不支持动态脚本注入:

  

Will there be a dynamic code injection for dart?

那么如何更改内容安全策略?

修改2

Chrome安装应用时会报告此错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

在适用于Chrome的套餐应用中,您无法使用字段"content_scripts""content_security_policy""tabs"

Dart也不支持动态脚本注入。如下所述:

  

Will there be a dynamic code injection for dart?

要使用JS代码,必须在本地保存,将其导入html代码:

<script src="test.js"></script>

并使用库dart:js与代码js交互,例如:

  test=context['test'];
  test.callMethod("login",["var1","var2",...,callback]);
相关问题