拒绝在 Dart 中加载 JS
Refuse to load JS in Dart
在我的 Dart 网页中这个脚本:
<script src="https://test.net/test/test.js"></script>
但是当您启动应用程序时,出现以下错误:
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:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
我还在清单中设置了内容安全策略,但没有任何结果:
"content_security_policy": "script-src 'self' https://test.net/test/"
有人知道怎么解决吗?
编辑
这个答案 Loading external javascript in google chrome extension 在 Dart 中不起作用。
清单:
..."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
Exception: The null object does not have a method 'callMethod'.
Dart 不支持脚本动态注入:
Will there be a dynamic code injection for dart?
那么如何更改内容安全策略?
编辑 2
Chrome安装应用报错:
在 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]);
在我的 Dart 网页中这个脚本:
<script src="https://test.net/test/test.js"></script>
但是当您启动应用程序时,出现以下错误:
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:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
我还在清单中设置了内容安全策略,但没有任何结果:
"content_security_policy": "script-src 'self' https://test.net/test/"
有人知道怎么解决吗?
编辑
这个答案 Loading external javascript in google chrome extension 在 Dart 中不起作用。
清单:
..."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
Exception: The null object does not have a method 'callMethod'.
Dart 不支持脚本动态注入:
Will there be a dynamic code injection for dart?
那么如何更改内容安全策略?
编辑 2
Chrome安装应用报错:
在 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]);