-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from Manuito83/develop
Merge for v3.6.2
- Loading branch information
Showing
8 changed files
with
108 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | ||
import 'package:html/parser.dart'; | ||
|
||
class WebViewUtils { | ||
/// Checks for a specific element by [selector] in the HTML from [webViewController] | ||
/// It will attempt to find the element(s) up to [maxSeconds] total, checking every [intervalSeconds] | ||
/// | ||
/// If [returnElements] is true, it returns a tuple-like result: | ||
/// { | ||
/// 'document': dom.Document, | ||
/// 'elements': List<dom.Element> | ||
/// } | ||
/// | ||
/// Otherwise, it only returns the [dom.Document] | ||
/// | ||
/// If the element is never found, returns null | ||
static Future<Map<String, dynamic>?> waitForElement({ | ||
required InAppWebViewController webViewController, | ||
required String selector, | ||
int maxSeconds = 6, | ||
int intervalSeconds = 1, | ||
bool returnElements = false, | ||
}) async { | ||
final int attempts = (maxSeconds / intervalSeconds).ceil(); | ||
|
||
for (int attempt = 0; attempt < attempts; attempt++) { | ||
await Future.delayed(Duration(seconds: intervalSeconds)); | ||
|
||
final html = await webViewController.getHtml(); | ||
final document = parse(html); | ||
final elements = document.querySelectorAll(selector); | ||
|
||
if (elements.isNotEmpty) { | ||
if (returnElements) { | ||
return {'document': document, 'elements': elements}; | ||
} else { | ||
return {'document': document}; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters