Skip to content

Commit

Permalink
Merge pull request #14 from dacher996/main
Browse files Browse the repository at this point in the history
Smaller WebView fixes
  • Loading branch information
kaungsatthe1n authored Mar 16, 2022
2 parents 9aad5f6 + 38e7a2f commit 3cc10b5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 53 deletions.
13 changes: 12 additions & 1 deletion lib/screens/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ class MainScreen extends StatefulWidget {
}

class _MainScreenState extends State<MainScreen> {
late final WebViewManager webViewManagerController;
var currentIndex = 0.obs;
var title = 'Home'.obs;
final webViewManagerController = Get.find<WebViewManager>();

final List<Widget> _pages = [
const HomeScreen(),
RecentListScreen(),
BookMarksScreen(),
];

@override
void initState() {
super.initState();

webViewManagerController = Get.find<WebViewManager>();

// Make sure we only call this once during main screen initialization to
// also properly initialize the webView status
webViewManagerController.getVideoPlayerType();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
31 changes: 11 additions & 20 deletions lib/screens/media_fetch_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import '../services/anime_service.dart';
import '../theme/tako_theme.dart';
import '../utils/constants.dart';
import '../utils/routes.dart';
import '../utils/tako_helper.dart';


class MediaFetchScreen extends StatefulWidget {
const MediaFetchScreen({Key? key}) : super(key: key);
Expand All @@ -22,7 +22,6 @@ class MediaFetchScreen extends StatefulWidget {

class _MediaFetchScreenState extends State<MediaFetchScreen> {
final GlobalKey webViewKey = GlobalKey();
WebViewController? _webViewController;
final webViewManagerController = Get.find<WebViewManager>();
final mediaFetchController = Get.find<MediaQualityManager>();
final _random = Random();
Expand Down Expand Up @@ -73,43 +72,39 @@ class _MediaFetchScreenState extends State<MediaFetchScreen> {
if (snapshot.connectionState == ConnectionState.done) {
return SizedBox.fromSize(
size: Size(screenWidth / 1.5, screenHeight / 1.5),
child: WebView(
child: TakoPlayWebView(
initialUrl: 'https:${snapshot.data}',
javascriptMode: JavascriptMode.unrestricted,
allowsInlineMediaPlayback: true,
onWebViewCreated: (controller) async {
_webViewController = controller;
},
onPageFinished: (_) async {
onLoadingFinished: (_webViewController) async {
mediaUrl = snapshot.data!;
// Ads Block
for (var i = 0; i < 8; i++) {
await _webViewController!
await _webViewController
.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
await _webViewController!
await _webViewController
.runJavascriptReturningResult(
"document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();");
}
// Fetching VidStreaming Url
String rawUrl = await _webViewController!
String rawUrl = await _webViewController
.runJavascriptReturningResult(
"document.getElementsByClassName('jw-video jw-reset')[0].attributes.src.value;");
if (rawUrl == 'null') {
hasError.value = true;
} else {
String url = rawUrl.split('"').toList()[1];
takoDebugPrint(rawUrl);
await _webViewController!

await _webViewController

.runJavascriptReturningResult(
"if(document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].ariaLabel == 'Play'){document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();}");
String resolutionCount = await _webViewController!
String resolutionCount = await _webViewController
.runJavascriptReturningResult(
"document.getElementsByClassName('jw-reset jw-settings-submenu-items')[1].getElementsByClassName('jw-reset-text jw-settings-content-item')['length'];");
for (var j = 0;
j < double.parse(resolutionCount) - 1;
j++) {
String rawResolution = await _webViewController!
String rawResolution = await _webViewController
.runJavascriptReturningResult(
"document.getElementsByClassName('jw-reset jw-settings-submenu-items')[1].getElementsByClassName('jw-reset-text jw-settings-content-item')[$j].textContent;");
String resolution = rawResolution
Expand Down Expand Up @@ -151,10 +146,6 @@ class _MediaFetchScreenState extends State<MediaFetchScreen> {
}
}
},
navigationDelegate: (NavigationRequest request) {
return NavigationDecision.prevent;
},
backgroundColor: const Color(0x00000000),
),
);
} else {
Expand Down
64 changes: 32 additions & 32 deletions lib/screens/webview_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../utils/constants.dart';
import '../widgets/tako_play_web_view.dart';

class WebViewScreen extends StatefulWidget {
const WebViewScreen({Key? key}) : super(key: key);
Expand All @@ -19,11 +20,12 @@ class WebViewScreen extends StatefulWidget {
class _WebViewScreenState extends State<WebViewScreen> {
var isLandScape = true.obs;
final GlobalKey webViewKey = GlobalKey();
WebViewController? _webViewController;

@override
void initState() {
super.initState();
if (Platform.isAndroid) WebView.platform = AndroidWebView();
if (Platform.isIOS) WebView.platform = CupertinoWebView();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
Expand Down Expand Up @@ -84,41 +86,39 @@ class _WebViewScreenState extends State<WebViewScreen> {
children: [
SizedBox.fromSize(
size: Size(screenWidth, screenHeight),
child: WebView(
child: TakoPlayWebView(
initialUrl: Get.arguments['mediaUrl'].toString(),
javascriptMode: JavascriptMode.unrestricted,
allowsInlineMediaPlayback: true,
onWebViewCreated: (controller) async {
_webViewController = controller;
},
onPageFinished: (_) async {
onLoadingFinished: (_webViewController) async {
await Future.delayed(const Duration(seconds: 1));
//
for (var i = 0; i < 8; i++) {
await _webViewController!.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
await _webViewController!.runJavascriptReturningResult(
"document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();");
}
//
for (var i = 0; i < 8; i++) {
await _webViewController!.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
try {
//
for (var i = 0; i < 8; i++) {
await _webViewController.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
await _webViewController.runJavascriptReturningResult(
"document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();");
}
//
for (var i = 0; i < 8; i++) {
await _webViewController.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
}
await _webViewController.runJavascriptReturningResult(
"document.getElementsByClassName('jw-icon jw-icon-inline jw-button-color jw-reset jw-icon-fullscreen')[1].click();");
await _webViewController.runJavascriptReturningResult(
"if(document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].ariaLabel == 'Play'){document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();}");
//
for (var i = 0; i < 8; i++) {
await _webViewController.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
}
} catch (e) {
print(
'An error occurred while parsing data from webView:');
print(e.toString());
rethrow;
}
await _webViewController!.runJavascriptReturningResult(
"document.getElementsByClassName('jw-icon jw-icon-inline jw-button-color jw-reset jw-icon-fullscreen')[1].click();");
await _webViewController!.runJavascriptReturningResult(
"if(document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].ariaLabel == 'Play'){document.getElementsByClassName('jw-icon jw-icon-display jw-button-color jw-reset')[0].click();}");
//
for (var i = 0; i < 8; i++) {
await _webViewController!.runJavascriptReturningResult(
"document.getElementsByTagName('iframe')[$i].style.display='none';");
}
},
navigationDelegate: (NavigationRequest request) {
return NavigationDecision.prevent;
},
backgroundColor: const Color(0x00000000),
),
),
Positioned(
Expand Down
51 changes: 51 additions & 0 deletions lib/widgets/tako_play_web_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

/// [TakoPlayWebView] is a webview specifically tailored for the needs of the
/// Tako Play app. Rather than always creating a webview and assigning it same
/// values each time, this widget can be instantiated without going though that
/// hassle.
class TakoPlayWebView extends StatefulWidget {
const TakoPlayWebView({
required this.initialUrl,
this.onLoadingFinished,
Key? key,
}) : super(key: key);

/// Initial url of the web view
final String initialUrl;

/// Callback triggered once the page has finished loading
final Function(WebViewController)? onLoadingFinished;

@override
State<TakoPlayWebView> createState() => _TakoPlayWebViewState();
}

class _TakoPlayWebViewState extends State<TakoPlayWebView> {
WebViewController? _webViewController;

@override
Widget build(BuildContext context) => WebView(
initialUrl: widget.initialUrl,
javascriptMode: JavascriptMode.unrestricted,
allowsInlineMediaPlayback: true,
backgroundColor: const Color(0x00000000),
onWebViewCreated: (controller) async {
_webViewController = controller;
},
onPageFinished: (_) async {
if (_webViewController == null) return;
widget.onLoadingFinished?.call(_webViewController!);
},
navigationDelegate: (NavigationRequest request) {
// If we are navigating to the destination url, allow it.
if (request.url == widget.initialUrl) {
return NavigationDecision.navigate;
}

// Otherwise, reject any navigation to other web urls
return NavigationDecision.prevent;
},
);
}

0 comments on commit 3cc10b5

Please sign in to comment.