您好,我尝试将Flutter中webviewcontroller.injectScriptFile(String urlFile)
软件包的flutter_inappbrowser
的{{1}}注入js文件到打开的InAppBrowser网站。实际上,我应该在js文件中插入的函数应该在注入时自动运行,但不是。我的js文件可能没有什么问题,因此它必须是我尝试通过路径或其他方式注入文件的方式。
以下是flutter_inappbrowser软件包的文档:https://pub.dev/packages/flutter_inappbrowser
我尝试了以下操作:
1.我将整个资产文件夹添加到pubspec.yaml中,以供flutter识别
2.我的js文件的语言环境路径为assets/js/example.js
,因此我尝试将此路径的参数准确地提供给字符串。
3.我还尝试更改我的js文件,使其具有正常功能并导出。并使用webviewcontroller.injectScriptCode("functionFromJsFile()")
调用此函数,但错误为functionFromJsFile() is not defined
EXAMPLE.JS:
(function() {
alert("Test");
})();
在扩展INAPPBROWSER的类内注入文件的代码(自己的类按预期工作。仅onLoadStop中的给定行无效。)
class CustomInAppBrowser extends InAppBrowser {
@override
Future onLoadStop(String url) async {
await this.webViewController.injectScriptFile("assets/js/example.js");
}
}
打开安装程序的代码:
CustomInAppBrowser inAppBrowser = CustomInAppBrowser();
inAppBrowser.open(url: "https://www.example.com", options: {
"useShouldOverrideUrlLoading": true,
"useOnLoadResource": true,
"domStorageEnabled": true,
"clearCache": true,
});
我实际上希望当我随后调用该函数以打开InAppBrowser时,InAppBrowser会打开给定的网站并向我发出警报。除来自给定js文件的警报外,其他所有东西都起作用,但是js文件甚至没有注入,或者参数urlFile
出现问题,在我的情况下为"assets/js/example.js"