简体   繁体   English

拒绝在Dart中加载JS

[英]Refuse to load JS in Dart

In my web page for Dart amount this script: 在我的Dart网页中,该脚本为:

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

but when you launch the application it gives me the following error: 但是,当您启动该应用程序时,它给了我以下错误:

Refused to load the script ' https://test.net/test/test.js ' because it violates the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". 拒绝加载脚本“ https://test.net/test/test.js ”,因为它违反了以下内容安全策略指令:“ default-src'self'chrome-extension-resource:”。 Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback. 请注意,未明确设置“ script-src”,因此将“ default-src”用作备用。

I also set the Content Security Policy in manifest without any result: 我还将清单中的内容安全策略设置为没有任何结果:

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

Does anyone know how to fix? 有谁知道如何解决?

Edit 编辑

This answer Loading external javascript in google chrome extension in Dart does not work. 此答案无法在Dart 中的google chrome扩展加载外部javascript

Manifest: 表现:

..."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"]
}
]...

Code: 码:

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

Result: 结果:

null 空值

Exception: The null object does not have a method 'callMethod'. 例外:空对象没有方法'callMethod'。

Dart does not support script injection dynamically: Dart不动态支持脚本注入:

Will there be a dynamic code injection for dart? 飞镖会有动态代码注入吗?

So how do you change the Content Security Policy? 那么,如何更改内容安全策略?

Edit 2 编辑2

Chrome reports this error when I install the app: 当我安装应用程序时,Chrome报告此错误:

在此处输入图片说明

In a package app for Chrome you can not use the fields "content_scripts" , "content_security_policy" and "tabs" . 在适用于Chrome的打包应用中,您不能使用"content_scripts""content_security_policy""tabs"字段。

Dart also does not support script injection dynamically. Dart也不动态支持脚本注入。 As explained here: 如此处所述:

Will there be a dynamic code injection for dart? 飞镖会有动态代码注入吗?

To use JS code must save it locally, import it into your html code: 要使用JS代码,必须将其保存在本地,将其导入到您的html代码中:

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

and use the library dart:js to interact with the code js, like: 并使用库dart:js与代码js进行交互,例如:

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

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

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