mirror of
https://github.com/haorendashu/nowser.git
synced 2025-12-17 01:44:19 +01:00
webview_cef (linux webview) not works in other platforms
This commit is contained in:
@@ -1,420 +1,420 @@
|
||||
import 'dart:convert';
|
||||
// import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nostr_sdk/event.dart';
|
||||
import 'package:nostr_sdk/utils/string_util.dart';
|
||||
import 'package:nowser/component/webview/web_info.dart';
|
||||
import 'package:nowser/const/app_type.dart';
|
||||
import 'package:nowser/const/auth_type.dart';
|
||||
import 'package:nowser/main.dart';
|
||||
import 'package:nowser/provider/permission_check_mixin.dart';
|
||||
import 'package:webview_cef/webview_cef.dart';
|
||||
import 'package:webview_cef/src/webview_inject_user_script.dart';
|
||||
// import 'package:flutter/material.dart';
|
||||
// import 'package:nostr_sdk/event.dart';
|
||||
// import 'package:nostr_sdk/utils/string_util.dart';
|
||||
// import 'package:nowser/component/webview/web_info.dart';
|
||||
// import 'package:nowser/const/app_type.dart';
|
||||
// import 'package:nowser/const/auth_type.dart';
|
||||
// import 'package:nowser/main.dart';
|
||||
// import 'package:nowser/provider/permission_check_mixin.dart';
|
||||
// import 'package:webview_cef/webview_cef.dart';
|
||||
// import 'package:webview_cef/src/webview_inject_user_script.dart';
|
||||
|
||||
import '../../data/app.dart';
|
||||
// import '../../data/app.dart';
|
||||
|
||||
class WebViewLinuxComponent extends StatefulWidget {
|
||||
WebInfo webInfo;
|
||||
// class WebViewLinuxComponent extends StatefulWidget {
|
||||
// WebInfo webInfo;
|
||||
|
||||
Function(WebInfo, WebViewController) onWebViewCreated;
|
||||
// Function(WebInfo, WebViewController) onWebViewCreated;
|
||||
|
||||
Function(WebInfo, WebViewController, String?) onTitleChanged;
|
||||
// Function(WebInfo, WebViewController, String?) onTitleChanged;
|
||||
|
||||
Function(WebInfo, WebViewController, String?) onUrlChanged;
|
||||
// Function(WebInfo, WebViewController, String?) onUrlChanged;
|
||||
|
||||
Function(WebInfo, WebViewController) onLoadStop;
|
||||
// Function(WebInfo, WebViewController) onLoadStop;
|
||||
|
||||
WebViewLinuxComponent(
|
||||
this.webInfo,
|
||||
this.onWebViewCreated,
|
||||
this.onTitleChanged,
|
||||
this.onUrlChanged,
|
||||
this.onLoadStop,
|
||||
);
|
||||
// WebViewLinuxComponent(
|
||||
// this.webInfo,
|
||||
// this.onWebViewCreated,
|
||||
// this.onTitleChanged,
|
||||
// this.onUrlChanged,
|
||||
// this.onLoadStop,
|
||||
// );
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return _WebViewLinuxComponent();
|
||||
}
|
||||
}
|
||||
// @override
|
||||
// State<StatefulWidget> createState() {
|
||||
// return _WebViewLinuxComponent();
|
||||
// }
|
||||
// }
|
||||
|
||||
class _WebViewLinuxComponent extends State<WebViewLinuxComponent>
|
||||
with PermissionCheckMixin {
|
||||
late WebViewController controller;
|
||||
// class _WebViewLinuxComponent extends State<WebViewLinuxComponent>
|
||||
// with PermissionCheckMixin {
|
||||
// late WebViewController controller;
|
||||
|
||||
double progress = 0;
|
||||
// double progress = 0;
|
||||
|
||||
Set<JavascriptChannel> javascriptChannels = {};
|
||||
// Set<JavascriptChannel> javascriptChannels = {};
|
||||
|
||||
InjectUserScripts injectScript = InjectUserScripts();
|
||||
// InjectUserScripts injectScript = InjectUserScripts();
|
||||
|
||||
String url = "";
|
||||
// String url = "";
|
||||
|
||||
String title = "";
|
||||
// String title = "";
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// @override
|
||||
// void initState() {
|
||||
// super.initState();
|
||||
|
||||
initInjectScript();
|
||||
initJSHandle();
|
||||
// initInjectScript();
|
||||
// initJSHandle();
|
||||
|
||||
controller = WebviewManager().createWebView(
|
||||
loading: const Text("Loading"),
|
||||
injectUserScripts: injectScript);
|
||||
controller.setWebviewListener(WebviewEventsListener(
|
||||
onTitleChanged: (title) {
|
||||
title = title;
|
||||
widget.onTitleChanged(widget.webInfo, controller, title);
|
||||
},
|
||||
onUrlChanged: (url) {
|
||||
url = url;
|
||||
widget.onUrlChanged(widget.webInfo, controller, url);
|
||||
},
|
||||
onConsoleMessage: (int level, String message, String source, int line) {
|
||||
print("$level $source $line $message");
|
||||
},
|
||||
// onLoadStart: (controller, url) {
|
||||
// },
|
||||
onLoadEnd: (controller, url) {
|
||||
widget.onLoadStop(widget.webInfo, controller);
|
||||
},
|
||||
));
|
||||
// controller = WebviewManager().createWebView(
|
||||
// loading: const Text("Loading"),
|
||||
// injectUserScripts: injectScript);
|
||||
// controller.setWebviewListener(WebviewEventsListener(
|
||||
// onTitleChanged: (title) {
|
||||
// title = title;
|
||||
// widget.onTitleChanged(widget.webInfo, controller, title);
|
||||
// },
|
||||
// onUrlChanged: (url) {
|
||||
// url = url;
|
||||
// widget.onUrlChanged(widget.webInfo, controller, url);
|
||||
// },
|
||||
// onConsoleMessage: (int level, String message, String source, int line) {
|
||||
// print("$level $source $line $message");
|
||||
// },
|
||||
// // onLoadStart: (controller, url) {
|
||||
// // },
|
||||
// onLoadEnd: (controller, url) {
|
||||
// widget.onLoadStop(widget.webInfo, controller);
|
||||
// },
|
||||
// ));
|
||||
|
||||
controller.initialize(widget.webInfo.url).then((v) {
|
||||
controller.setJavaScriptChannels(javascriptChannels);
|
||||
setState(() {
|
||||
inited = true;
|
||||
});
|
||||
// controller.initialize(widget.webInfo.url).then((v) {
|
||||
// controller.setJavaScriptChannels(javascriptChannels);
|
||||
// setState(() {
|
||||
// inited = true;
|
||||
// });
|
||||
|
||||
// controller.openDevTools();
|
||||
});
|
||||
}
|
||||
// // controller.openDevTools();
|
||||
// });
|
||||
// }
|
||||
|
||||
var inited = false;
|
||||
// var inited = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
child: inited ? controller.webviewWidget : controller.loadingWidget,
|
||||
);
|
||||
}
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return Container(
|
||||
// child: inited ? controller.webviewWidget : controller.loadingWidget,
|
||||
// );
|
||||
// }
|
||||
|
||||
Future<void> nip07Reject(String resultId, String contnet) async {
|
||||
var script = "window.nostr.reject(\"$resultId\", \"${contnet}\");";
|
||||
await controller.executeJavaScript(script);
|
||||
}
|
||||
// Future<void> nip07Reject(String resultId, String contnet) async {
|
||||
// var script = "window.nostr.reject(\"$resultId\", \"${contnet}\");";
|
||||
// await controller.executeJavaScript(script);
|
||||
// }
|
||||
|
||||
void initJSHandle() {
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSgetPublicKey",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSgetPublicKey $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
// void initJSHandle() {
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSgetPublicKey",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSgetPublicKey $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.GET_PUBLIC_KEY,
|
||||
(app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) {
|
||||
print("confirm get pubkey");
|
||||
var pubkey = app.pubkey;
|
||||
var script = "window.nostr.callback(\"$resultId\", \"$pubkey\");";
|
||||
controller.executeJavaScript(script);
|
||||
});
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSsignEvent",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSsignEvent $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
var content = jsonObj["msg"];
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.GET_PUBLIC_KEY,
|
||||
// (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) {
|
||||
// print("confirm get pubkey");
|
||||
// var pubkey = app.pubkey;
|
||||
// var script = "window.nostr.callback(\"$resultId\", \"$pubkey\");";
|
||||
// controller.executeJavaScript(script);
|
||||
// });
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSsignEvent",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSsignEvent $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
// var content = jsonObj["msg"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
try {
|
||||
var eventObj = jsonDecode(content);
|
||||
var eventKind = eventObj["kind"];
|
||||
if (eventKind is int) {
|
||||
checkPermission(context, AppType.WEB, code, AuthType.SIGN_EVENT,
|
||||
eventKind: eventKind, authDetail: content, (app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) async {
|
||||
var tags = eventObj["tags"];
|
||||
Event? event = Event(app.pubkey!, eventObj["kind"], tags ?? [],
|
||||
eventObj["content"],
|
||||
createdAt: eventObj["created_at"]);
|
||||
event = await signer.signEvent(event);
|
||||
if (event == null) {
|
||||
return;
|
||||
}
|
||||
// try {
|
||||
// var eventObj = jsonDecode(content);
|
||||
// var eventKind = eventObj["kind"];
|
||||
// if (eventKind is int) {
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.SIGN_EVENT,
|
||||
// eventKind: eventKind, authDetail: content, (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) async {
|
||||
// var tags = eventObj["tags"];
|
||||
// Event? event = Event(app.pubkey!, eventObj["kind"], tags ?? [],
|
||||
// eventObj["content"],
|
||||
// createdAt: eventObj["created_at"]);
|
||||
// event = await signer.signEvent(event);
|
||||
// if (event == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
var eventResultStr = jsonEncode(event.toJson());
|
||||
// TODO this method to handle " may be error
|
||||
eventResultStr = eventResultStr.replaceAll("\"", "\\\"");
|
||||
var script =
|
||||
"window.nostr.callback(\"$resultId\", JSON.parse(\"$eventResultStr\"));";
|
||||
controller.executeJavaScript(script);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
nip07Reject(resultId, "Sign fail");
|
||||
}
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSgetRelays",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSgetRelays $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
// var eventResultStr = jsonEncode(event.toJson());
|
||||
// // TODO this method to handle " may be error
|
||||
// eventResultStr = eventResultStr.replaceAll("\"", "\\\"");
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", JSON.parse(\"$eventResultStr\"));";
|
||||
// controller.executeJavaScript(script);
|
||||
// });
|
||||
// }
|
||||
// } catch (e) {
|
||||
// nip07Reject(resultId, "Sign fail");
|
||||
// }
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSgetRelays",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSgetRelays $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.GET_RELAYS, (app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) {
|
||||
// TODO handle getRelays
|
||||
// var app = appProvider.getApp(AppType.WEB, code);
|
||||
// if (app != null) {
|
||||
// var relayMaps = {};
|
||||
// var relayAddrs = relayProvider.relayAddrs;
|
||||
// for (var relayAddr in relayAddrs) {
|
||||
// relayMaps[relayAddr] = {"read": true, "write": true};
|
||||
// }
|
||||
// var resultStr = jsonEncode(relayMaps);
|
||||
// resultStr = resultStr.replaceAll("\"", "\\\"");
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", JSON.parse(\"$resultStr\"));";
|
||||
// webViewController!.evaluateJavascript(source: script);
|
||||
// }
|
||||
});
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSnip04encrypt",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSnip04encrypt $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
var msg = jsonObj["msg"];
|
||||
if (msg != null && msg is Map) {
|
||||
var pubkey = msg["pubkey"];
|
||||
var plaintext = msg["plaintext"];
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.GET_RELAYS, (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) {
|
||||
// // TODO handle getRelays
|
||||
// // var app = appProvider.getApp(AppType.WEB, code);
|
||||
// // if (app != null) {
|
||||
// // var relayMaps = {};
|
||||
// // var relayAddrs = relayProvider.relayAddrs;
|
||||
// // for (var relayAddr in relayAddrs) {
|
||||
// // relayMaps[relayAddr] = {"read": true, "write": true};
|
||||
// // }
|
||||
// // var resultStr = jsonEncode(relayMaps);
|
||||
// // resultStr = resultStr.replaceAll("\"", "\\\"");
|
||||
// // var script =
|
||||
// // "window.nostr.callback(\"$resultId\", JSON.parse(\"$resultStr\"));";
|
||||
// // webViewController!.evaluateJavascript(source: script);
|
||||
// // }
|
||||
// });
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSnip04encrypt",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSnip04encrypt $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
// var msg = jsonObj["msg"];
|
||||
// if (msg != null && msg is Map) {
|
||||
// var pubkey = msg["pubkey"];
|
||||
// var plaintext = msg["plaintext"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.NIP04_ENCRYPT,
|
||||
(app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) async {
|
||||
var resultStr = await signer.encrypt(pubkey, plaintext);
|
||||
if (StringUtil.isBlank(resultStr)) {
|
||||
return;
|
||||
}
|
||||
var script =
|
||||
"window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
controller.executeJavaScript(script);
|
||||
});
|
||||
}
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSnip04decrypt",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSnip04decrypt $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
var msg = jsonObj["msg"];
|
||||
if (msg != null && msg is Map) {
|
||||
var pubkey = msg["pubkey"];
|
||||
var ciphertext = msg["ciphertext"];
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.NIP04_ENCRYPT,
|
||||
// (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) async {
|
||||
// var resultStr = await signer.encrypt(pubkey, plaintext);
|
||||
// if (StringUtil.isBlank(resultStr)) {
|
||||
// return;
|
||||
// }
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
// controller.executeJavaScript(script);
|
||||
// });
|
||||
// }
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSnip04decrypt",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSnip04decrypt $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
// var msg = jsonObj["msg"];
|
||||
// if (msg != null && msg is Map) {
|
||||
// var pubkey = msg["pubkey"];
|
||||
// var ciphertext = msg["ciphertext"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.NIP04_DECRYPT,
|
||||
(app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) async {
|
||||
var app = appProvider.getApp(AppType.WEB, code);
|
||||
if (app != null) {
|
||||
var resultStr = await signer.decrypt(pubkey, ciphertext);
|
||||
if (StringUtil.isBlank(resultStr)) {
|
||||
return;
|
||||
}
|
||||
var script =
|
||||
"window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
controller.executeJavaScript(script);
|
||||
}
|
||||
});
|
||||
}
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSnip44encrypt",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSnip44encrypt $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
var msg = jsonObj["msg"];
|
||||
if (msg != null && msg is Map) {
|
||||
var pubkey = msg["pubkey"];
|
||||
var plaintext = msg["plaintext"];
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.NIP04_DECRYPT,
|
||||
// (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) async {
|
||||
// var app = appProvider.getApp(AppType.WEB, code);
|
||||
// if (app != null) {
|
||||
// var resultStr = await signer.decrypt(pubkey, ciphertext);
|
||||
// if (StringUtil.isBlank(resultStr)) {
|
||||
// return;
|
||||
// }
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
// controller.executeJavaScript(script);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSnip44encrypt",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSnip44encrypt $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
// var msg = jsonObj["msg"];
|
||||
// if (msg != null && msg is Map) {
|
||||
// var pubkey = msg["pubkey"];
|
||||
// var plaintext = msg["plaintext"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.NIP44_ENCRYPT,
|
||||
(app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) async {
|
||||
var resultStr = await signer.nip44Encrypt(pubkey, plaintext);
|
||||
if (StringUtil.isBlank(resultStr)) {
|
||||
return;
|
||||
}
|
||||
var script =
|
||||
"window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
controller.executeJavaScript(script);
|
||||
});
|
||||
}
|
||||
},)
|
||||
);
|
||||
javascriptChannels.add(JavascriptChannel(name: "NowserJSnip44decrypt",
|
||||
onMessageReceived: (javascriptMessage) async {
|
||||
var jsMsg = javascriptMessage.message;
|
||||
print("NowserJSnip44decrypt $jsMsg");
|
||||
var jsonObj = jsonDecode(jsMsg);
|
||||
var resultId = jsonObj["resultId"];
|
||||
var msg = jsonObj["msg"];
|
||||
if (msg != null && msg is Map) {
|
||||
var pubkey = msg["pubkey"];
|
||||
var ciphertext = msg["ciphertext"];
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.NIP44_ENCRYPT,
|
||||
// (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) async {
|
||||
// var resultStr = await signer.nip44Encrypt(pubkey, plaintext);
|
||||
// if (StringUtil.isBlank(resultStr)) {
|
||||
// return;
|
||||
// }
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
// controller.executeJavaScript(script);
|
||||
// });
|
||||
// }
|
||||
// },)
|
||||
// );
|
||||
// javascriptChannels.add(JavascriptChannel(name: "NowserJSnip44decrypt",
|
||||
// onMessageReceived: (javascriptMessage) async {
|
||||
// var jsMsg = javascriptMessage.message;
|
||||
// print("NowserJSnip44decrypt $jsMsg");
|
||||
// var jsonObj = jsonDecode(jsMsg);
|
||||
// var resultId = jsonObj["resultId"];
|
||||
// var msg = jsonObj["msg"];
|
||||
// if (msg != null && msg is Map) {
|
||||
// var pubkey = msg["pubkey"];
|
||||
// var ciphertext = msg["ciphertext"];
|
||||
|
||||
String? code = await getCode();
|
||||
if (code == null) {
|
||||
return;
|
||||
}
|
||||
// String? code = await getCode();
|
||||
// if (code == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
checkPermission(context, AppType.WEB, code, AuthType.NIP44_DECRYPT,
|
||||
(app) {
|
||||
nip07Reject(resultId, "Forbid");
|
||||
}, (app, signer) async {
|
||||
var resultStr = await signer.nip44Decrypt(pubkey, ciphertext);
|
||||
if (StringUtil.isBlank(resultStr)) {
|
||||
return;
|
||||
}
|
||||
var script =
|
||||
"window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
controller.executeJavaScript(script);
|
||||
});
|
||||
}
|
||||
},)
|
||||
);
|
||||
}
|
||||
// checkPermission(context, AppType.WEB, code, AuthType.NIP44_DECRYPT,
|
||||
// (app) {
|
||||
// nip07Reject(resultId, "Forbid");
|
||||
// }, (app, signer) async {
|
||||
// var resultStr = await signer.nip44Decrypt(pubkey, ciphertext);
|
||||
// if (StringUtil.isBlank(resultStr)) {
|
||||
// return;
|
||||
// }
|
||||
// var script =
|
||||
// "window.nostr.callback(\"$resultId\", \"$resultStr\");";
|
||||
// controller.executeJavaScript(script);
|
||||
// });
|
||||
// }
|
||||
// },)
|
||||
// );
|
||||
// }
|
||||
|
||||
void initInjectScript() {
|
||||
injectScript.add(UserScript("""
|
||||
window.nostr = {
|
||||
_call(channel, message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var resultId = "callbackResult_" + Math.floor(Math.random() * 100000000);
|
||||
var arg = {"resultId": resultId};
|
||||
if (message) {
|
||||
arg["msg"] = message;
|
||||
}
|
||||
// var argStr = JSON.stringify(arg);
|
||||
// window.flutter_inappwebview
|
||||
// .callHandler(channel, argStr);
|
||||
channel(arg);
|
||||
window.nostr._requests[resultId] = {resolve, reject}
|
||||
});
|
||||
},
|
||||
_requests: {},
|
||||
callback(resultId, message) {
|
||||
window.nostr._requests[resultId].resolve(message);
|
||||
},
|
||||
reject(resultId, message) {
|
||||
window.nostr._requests[resultId].reject(message);
|
||||
},
|
||||
async getPublicKey() {
|
||||
return window.nostr._call(NowserJSgetPublicKey);
|
||||
},
|
||||
async signEvent(event) {
|
||||
return window.nostr._call(NowserJSsignEvent, JSON.stringify(event));
|
||||
},
|
||||
async getRelays() {
|
||||
return window.nostr._call(NowserJSgetRelays);
|
||||
},
|
||||
nip04: {
|
||||
async encrypt(pubkey, plaintext) {
|
||||
return window.nostr._call(NowserJSnip04encrypt, {"pubkey": pubkey, "plaintext": plaintext});
|
||||
},
|
||||
async decrypt(pubkey, ciphertext) {
|
||||
return window.nostr._call(NowserJSnip04decrypt, {"pubkey": pubkey, "ciphertext": ciphertext});
|
||||
},
|
||||
},
|
||||
nip44: {
|
||||
async encrypt(pubkey, plaintext) {
|
||||
return window.nostr._call(NowserJSnip44encrypt, {"pubkey": pubkey, "plaintext": plaintext});
|
||||
},
|
||||
async decrypt(pubkey, ciphertext) {
|
||||
return window.nostr._call(NowserJSnip44decrypt, {"pubkey": pubkey, "ciphertext": ciphertext});
|
||||
},
|
||||
},
|
||||
};
|
||||
""", ScriptInjectTime.LOAD_START));
|
||||
// injectScript.add(UserScript("console.log(window.nostr);", ScriptInjectTime.LOAD_END));
|
||||
}
|
||||
// void initInjectScript() {
|
||||
// injectScript.add(UserScript("""
|
||||
// window.nostr = {
|
||||
// _call(channel, message) {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// var resultId = "callbackResult_" + Math.floor(Math.random() * 100000000);
|
||||
// var arg = {"resultId": resultId};
|
||||
// if (message) {
|
||||
// arg["msg"] = message;
|
||||
// }
|
||||
// // var argStr = JSON.stringify(arg);
|
||||
// // window.flutter_inappwebview
|
||||
// // .callHandler(channel, argStr);
|
||||
// channel(arg);
|
||||
// window.nostr._requests[resultId] = {resolve, reject}
|
||||
// });
|
||||
// },
|
||||
// _requests: {},
|
||||
// callback(resultId, message) {
|
||||
// window.nostr._requests[resultId].resolve(message);
|
||||
// },
|
||||
// reject(resultId, message) {
|
||||
// window.nostr._requests[resultId].reject(message);
|
||||
// },
|
||||
// async getPublicKey() {
|
||||
// return window.nostr._call(NowserJSgetPublicKey);
|
||||
// },
|
||||
// async signEvent(event) {
|
||||
// return window.nostr._call(NowserJSsignEvent, JSON.stringify(event));
|
||||
// },
|
||||
// async getRelays() {
|
||||
// return window.nostr._call(NowserJSgetRelays);
|
||||
// },
|
||||
// nip04: {
|
||||
// async encrypt(pubkey, plaintext) {
|
||||
// return window.nostr._call(NowserJSnip04encrypt, {"pubkey": pubkey, "plaintext": plaintext});
|
||||
// },
|
||||
// async decrypt(pubkey, ciphertext) {
|
||||
// return window.nostr._call(NowserJSnip04decrypt, {"pubkey": pubkey, "ciphertext": ciphertext});
|
||||
// },
|
||||
// },
|
||||
// nip44: {
|
||||
// async encrypt(pubkey, plaintext) {
|
||||
// return window.nostr._call(NowserJSnip44encrypt, {"pubkey": pubkey, "plaintext": plaintext});
|
||||
// },
|
||||
// async decrypt(pubkey, ciphertext) {
|
||||
// return window.nostr._call(NowserJSnip44decrypt, {"pubkey": pubkey, "ciphertext": ciphertext});
|
||||
// },
|
||||
// },
|
||||
// };
|
||||
// """, ScriptInjectTime.LOAD_START));
|
||||
// // injectScript.add(UserScript("console.log(window.nostr);", ScriptInjectTime.LOAD_END));
|
||||
// }
|
||||
|
||||
Future<String?> getCode() async {
|
||||
if (StringUtil.isBlank(url)) {
|
||||
url = widget.webInfo.url;
|
||||
}
|
||||
// Future<String?> getCode() async {
|
||||
// if (StringUtil.isBlank(url)) {
|
||||
// url = widget.webInfo.url;
|
||||
// }
|
||||
|
||||
if (StringUtil.isNotBlank(url)) {
|
||||
var uri = Uri.parse(url);
|
||||
return uri.host;
|
||||
}
|
||||
// if (StringUtil.isNotBlank(url)) {
|
||||
// var uri = Uri.parse(url);
|
||||
// return uri.host;
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<App> getApp(int appType, String code) async {
|
||||
String? name = title;
|
||||
String? image;
|
||||
// var favicons = await webViewController!.getFavicons();
|
||||
// if (favicons.isNotEmpty) {
|
||||
// image = favicons.first.url.toString();
|
||||
// }
|
||||
return App(appType: appType, code: code, name: name, image: image);
|
||||
}
|
||||
// @override
|
||||
// Future<App> getApp(int appType, String code) async {
|
||||
// String? name = title;
|
||||
// String? image;
|
||||
// // var favicons = await webViewController!.getFavicons();
|
||||
// // if (favicons.isNotEmpty) {
|
||||
// // image = favicons.first.url.toString();
|
||||
// // }
|
||||
// return App(appType: appType, code: code, name: name, image: image);
|
||||
// }
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
controller.dispose();
|
||||
}
|
||||
// @override
|
||||
// void dispose() {
|
||||
// super.dispose();
|
||||
// controller.dispose();
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
|
||||
|
||||
import 'package:webview_cef/webview_cef.dart';
|
||||
// import 'package:webview_cef/webview_cef.dart';
|
||||
|
||||
import 'webview_controller_interface.dart';
|
||||
// import 'webview_controller_interface.dart';
|
||||
|
||||
class WebviewLinuxController extends WebviewControllerInterface {
|
||||
// class WebviewLinuxController extends WebviewControllerInterface {
|
||||
|
||||
WebViewController controller;
|
||||
// WebViewController controller;
|
||||
|
||||
WebviewLinuxController(this.controller);
|
||||
// WebviewLinuxController(this.controller);
|
||||
|
||||
@override
|
||||
Future<void> reload() async {
|
||||
await controller.reload();
|
||||
}
|
||||
// @override
|
||||
// Future<void> reload() async {
|
||||
// await controller.reload();
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<void> goBack() async {
|
||||
await controller.goBack();
|
||||
}
|
||||
// @override
|
||||
// Future<void> goBack() async {
|
||||
// await controller.goBack();
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<bool> canGoBack() async {
|
||||
return true;
|
||||
}
|
||||
// @override
|
||||
// Future<bool> canGoBack() async {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<void> goForward() async {
|
||||
await controller.goForward();
|
||||
}
|
||||
// @override
|
||||
// Future<void> goForward() async {
|
||||
// await controller.goForward();
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<Uri?> getUrl() async {
|
||||
try {
|
||||
if (url != null) {
|
||||
return Uri.parse(url!);
|
||||
}
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
// @override
|
||||
// Future<Uri?> getUrl() async {
|
||||
// try {
|
||||
// if (url != null) {
|
||||
// return Uri.parse(url!);
|
||||
// }
|
||||
// } catch (e) {}
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<String?> getFavicon() async {
|
||||
return null;
|
||||
}
|
||||
// @override
|
||||
// Future<String?> getFavicon() async {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<void> loadUrl(String url) async {
|
||||
await controller.loadUrl(url);
|
||||
}
|
||||
// @override
|
||||
// Future<void> loadUrl(String url) async {
|
||||
// await controller.loadUrl(url);
|
||||
// }
|
||||
|
||||
@override
|
||||
Future<String?> getTitle() async {
|
||||
return title;
|
||||
}
|
||||
// @override
|
||||
// Future<String?> getTitle() async {
|
||||
// return title;
|
||||
// }
|
||||
|
||||
String? title;
|
||||
// String? title;
|
||||
|
||||
String? url;
|
||||
// String? url;
|
||||
|
||||
void setTitle(String title) {
|
||||
title = title;
|
||||
}
|
||||
// void setTitle(String title) {
|
||||
// title = title;
|
||||
// }
|
||||
|
||||
void setUrl(String url) {
|
||||
url = url;
|
||||
}
|
||||
// void setUrl(String url) {
|
||||
// url = url;
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
@@ -35,7 +35,6 @@ import 'package:provider/provider.dart';
|
||||
import 'package:quick_actions/quick_actions.dart';
|
||||
import 'package:receive_intent/receive_intent.dart' as receiveIntent;
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import 'package:webview_cef/webview_cef.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import 'const/base.dart';
|
||||
@@ -107,9 +106,10 @@ Future<void> main() async {
|
||||
print(e);
|
||||
}
|
||||
|
||||
if (Platform.isLinux) {
|
||||
WebviewManager().initialize(userAgent: Base.USER_AGENT);
|
||||
}
|
||||
// linux webview, only open in linux platform
|
||||
// if (Platform.isLinux) {
|
||||
// WebviewManager().initialize(userAgent: Base.USER_AGENT);
|
||||
// }
|
||||
|
||||
await doInit();
|
||||
|
||||
|
||||
@@ -80,35 +80,35 @@ class _IndexWebComponent extends State<IndexWebComponent> {
|
||||
webProvider.onLoadStop(webInfo);
|
||||
});
|
||||
} else {
|
||||
webComp = WebViewLinuxComponent(webInfo, (webInfo, controller) {
|
||||
webInfo.controller = WebviewLinuxController(controller);
|
||||
webProvider.updateWebInfo(webInfo);
|
||||
}, (webInfo, controller, title) {
|
||||
if (webInfo.controller is WebviewLinuxController &&
|
||||
StringUtil.isNotBlank(title)) {
|
||||
(webInfo.controller as WebviewLinuxController).setTitle(title!);
|
||||
webInfo.title = title;
|
||||
webProvider.updateWebInfo(webInfo);
|
||||
}
|
||||
}, (webInfo, controller, url) {
|
||||
if (webInfo.controller is WebviewLinuxController &&
|
||||
StringUtil.isNotBlank(url)) {
|
||||
if (url!.startsWith("https")) {
|
||||
webInfo.isSecure = true;
|
||||
} else {
|
||||
webInfo.isSecure = false;
|
||||
}
|
||||
// webComp = WebViewLinuxComponent(webInfo, (webInfo, controller) {
|
||||
// webInfo.controller = WebviewLinuxController(controller);
|
||||
// webProvider.updateWebInfo(webInfo);
|
||||
// }, (webInfo, controller, title) {
|
||||
// if (webInfo.controller is WebviewLinuxController &&
|
||||
// StringUtil.isNotBlank(title)) {
|
||||
// (webInfo.controller as WebviewLinuxController).setTitle(title!);
|
||||
// webInfo.title = title;
|
||||
// webProvider.updateWebInfo(webInfo);
|
||||
// }
|
||||
// }, (webInfo, controller, url) {
|
||||
// if (webInfo.controller is WebviewLinuxController &&
|
||||
// StringUtil.isNotBlank(url)) {
|
||||
// if (url!.startsWith("https")) {
|
||||
// webInfo.isSecure = true;
|
||||
// } else {
|
||||
// webInfo.isSecure = false;
|
||||
// }
|
||||
|
||||
print("url change! $url");
|
||||
(webInfo.controller as WebviewLinuxController).setUrl(url!);
|
||||
webInfo.url = url;
|
||||
}
|
||||
}, (webInfo, controller) async {
|
||||
webInfo.controller ??= WebviewLinuxController(controller);
|
||||
var title = await webInfo.controller!.getTitle();
|
||||
webInfo.title = title;
|
||||
webProvider.onLoadStop(webInfo);
|
||||
});
|
||||
// print("url change! $url");
|
||||
// (webInfo.controller as WebviewLinuxController).setUrl(url!);
|
||||
// webInfo.url = url;
|
||||
// }
|
||||
// }, (webInfo, controller) async {
|
||||
// webInfo.controller ??= WebviewLinuxController(controller);
|
||||
// var title = await webInfo.controller!.getTitle();
|
||||
// webInfo.title = title;
|
||||
// webProvider.onLoadStop(webInfo);
|
||||
// });
|
||||
}
|
||||
|
||||
return Container(
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
|
||||
#include <screen_retriever_linux/screen_retriever_linux_plugin.h>
|
||||
#include <webview_cef/webview_cef_plugin.h>
|
||||
#include <window_manager/window_manager_plugin.h>
|
||||
|
||||
void fl_register_plugins(FlPluginRegistry* registry) {
|
||||
@@ -18,9 +17,6 @@ void fl_register_plugins(FlPluginRegistry* registry) {
|
||||
g_autoptr(FlPluginRegistrar) screen_retriever_linux_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverLinuxPlugin");
|
||||
screen_retriever_linux_plugin_register_with_registrar(screen_retriever_linux_registrar);
|
||||
g_autoptr(FlPluginRegistrar) webview_cef_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "WebviewCefPlugin");
|
||||
webview_cef_plugin_register_with_registrar(webview_cef_registrar);
|
||||
g_autoptr(FlPluginRegistrar) window_manager_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "WindowManagerPlugin");
|
||||
window_manager_plugin_register_with_registrar(window_manager_registrar);
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
flutter_secure_storage_linux
|
||||
screen_retriever_linux
|
||||
webview_cef
|
||||
window_manager
|
||||
)
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import path_provider_foundation
|
||||
import screen_retriever_macos
|
||||
import shared_preferences_foundation
|
||||
import sqflite_darwin
|
||||
import webview_cef
|
||||
import window_manager
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
@@ -25,6 +24,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
ScreenRetrieverMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverMacosPlugin"))
|
||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
|
||||
WebviewCefPlugin.register(with: registry.registrar(forPlugin: "WebviewCefPlugin"))
|
||||
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
|
||||
}
|
||||
|
||||
@@ -1137,14 +1137,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.3"
|
||||
webview_cef:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: webview_cef
|
||||
sha256: "2e660bf593dc1168beb7b765dd477a703e0b29968669f017e178c2c716f6437f"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.2.2"
|
||||
win32:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -58,7 +58,7 @@ dependencies:
|
||||
flutter_cache_manager: ^3.4.1
|
||||
# searchfield: 1.2.0
|
||||
searchfield: ^1.2.7
|
||||
webview_cef: ^0.2.2
|
||||
# webview_cef: ^0.2.2
|
||||
flutter_font_icons: ^2.2.7
|
||||
|
||||
dev_dependencies:
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <flutter_inappwebview_windows/flutter_inappwebview_windows_plugin_c_api.h>
|
||||
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
|
||||
#include <screen_retriever_windows/screen_retriever_windows_plugin_c_api.h>
|
||||
#include <webview_cef/webview_cef_plugin_c_api.h>
|
||||
#include <window_manager/window_manager_plugin.h>
|
||||
|
||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
@@ -19,8 +18,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
|
||||
ScreenRetrieverWindowsPluginCApiRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("ScreenRetrieverWindowsPluginCApi"));
|
||||
WebviewCefPluginCApiRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("WebviewCefPluginCApi"));
|
||||
WindowManagerPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
|
||||
flutter_inappwebview_windows
|
||||
flutter_secure_storage_windows
|
||||
screen_retriever_windows
|
||||
webview_cef
|
||||
window_manager
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user