我已经累了三个月注入窗口。以太坊在扑动webview但无济于事,我发现我只能在页面加载端注入javascript,但一旦页面加载完成,窗口。以太坊将不可用的dapp,有没有办法我可以注入窗口。以太坊页面开始…
what I have try
WebView(
navigationDelegate: (action) {
if (action.isForMainFrame) {
return NavigationDecision.navigate;
}
return NavigationDecision.prevent;
},
onProgress: (precent) {
setState(() {
browserLoadingPercent = precent / 100;
});
},
onWebViewCreated: (controller) async {
_controller = controller;
},
initialUrl: widget.data ?? dappBrowserInitialUrl,
debuggingEnabled: true,
onPageStarted: (url) async {
dappBrowser.text = url;
if (urlLoaded == '') {
urlLoaded = url;
}
},
onPageFinished: (url) async {
return;
try {
if (urlLoaded == '') {
return;
} else {
urlLoaded = '';
}
await _controller.runJavascript(widget.sweetAlert);
await _controller.runJavascript(widget.web3);
await _controller.runJavascript(widget.provider);
var favicon =
await _controller.runJavascriptReturningResult('''
var getFavicon = function(){
var favicon = undefined;
var nodeList = document.getElementsByTagName("link");
for (var i = 0; i < nodeList.length; i++)
{
if((nodeList[i].getAttribute("rel") == "icon")||(nodeList[i].getAttribute("rel") == "shortcut icon"))
{
favicon = nodeList[i].getAttribute("href");
}
}
return new URL(favicon, document.baseURI).href;
}
getFavicon();
''');
favicon = favicon.split('"').join('').split("'").join('');
await showDialog(
barrierDismissible: false,
context: context,
builder: (_) {
return SimpleDialog(
title: Column(
children: [
if (true)
Container(
height: 100.0,
width: 100.0,
padding: const EdgeInsets.only(bottom: 8.0),
child: Image.network(favicon),
),
Text(url),
],
),
contentPadding:
const EdgeInsets.fromLTRB(16.0, 12.0, 16.0, 16.0),
children: [
if (url.isNotEmpty)
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text('Connection to ${url}'),
),
Row(
children: [
Expanded(
child: TextButton(
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: Theme.of(context)
.colorScheme
.secondary,
),
onPressed: () async {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
var ethEnabledBlockChain =
<Widget>[];
for (String i
in getBlockChains().keys) {
ethEnabledBlockChain.add(InkWell(
onTap: () async {
var count = 0;
var seedPhrase =
(await SharedPreferences
.getInstance())
.getString(
'mmemomic');
await _controller
.runJavascript('''
window.web3 = new Web3(
new HDWalletProvider({
mnemonic: {
phrase:
"${seedPhrase}",
},
providerOrUrl: "${getBlockChains()[i]['rpc']}",
chainId: ${getBlockChains()[i]['chainId']},
})
);
''');
Navigator.popUntil(context,
(route) {
return count++ == 2;
});
},
child: buildRow(
getBlockChains()[i]
['image'] !=
null
? getBlockChains()[i]
['image']
: 'assets/ethereum_logo.png',
i),
));
}
return Dialog(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
40)),
elevation: 16,
child: Container(
child: ListView(
shrinkWrap: true,
children: <Widget>[
SizedBox(height: 20),
Center(
child: Text(
'Select BlockChains')),
SizedBox(height: 20),
]..addAll(
ethEnabledBlockChain),
),
));
});
},
child: Text('APPROVE'),
),
),
const SizedBox(width: 16.0),
Expanded(
child: TextButton(
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: Theme.of(context)
.colorScheme
.secondary,
),
onPressed: () {
Navigator.pop(context);
},
child: Text('REJECT'),
),
),
],
),
],
);
},
);
// await _controller.runJavascript(
// 'document.documentElement.innerHTML = document.documentElement.innerHTML');
// await _controller.runJavascript(widget.reEnableJavascript);
// await _controller.runJavascript('''
// var load_event = document.createEvent("HTMLEvents");
// load_event.initEvent("DOMContentLoaded", true, true);
// window.document.dispatchEvent(load_event);
// ''');
// await _controller.runJavascript('''
// var load_event = document.createEvent("HTMLEvents");
// load_event.initEvent("load", true, true);
// window.document.dispatchEvent(load_event);
// ''');
await _controller.runJavascript('''
(async function () {
const account = (await web3.eth.getAccounts())[0];
console.log('web3 dapp browser: ' + account);
})();''');
print('end of function');
} catch (e) {
print(e);
}
},
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
gestureRecognizers: gestureRecognizers,
)```
嵌入式dApp浏览器是Web3手机应用设计中的一个反模式。
相反,如果你想在你的移动应用程序上拥有以太坊交易功能,你应该使用WalletConnect协议。