Skip to content

Commit

Permalink
updated web support
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Apr 27, 2022
1 parent 3bad02d commit 68f25d0
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
build/
.fvm/

flutter_driver_tests.log
flutter_driver_tests.log
tool/chromedriver
tool/chromedriver.*
14 changes: 8 additions & 6 deletions example/lib/in_app_webiew_example.screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:collection';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Expand Down Expand Up @@ -114,16 +115,17 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
children: [
InAppWebView(
key: webViewKey,
// initialUrlRequest:
// URLRequest(url: Uri.parse("https://flutter.dev")),
initialUrlRequest:
URLRequest(url: Uri.parse("https://flutter.dev")),
URLRequest(url: Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html')),
// initialFile: "assets/index.html",
initialUserScripts: UnmodifiableListView<UserScript>([]),
initialSettings: settings,
// contextMenu: contextMenu,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) async {
onWebViewCreated: (controller) {
webViewController = controller;
print(await controller.getUrl());
},
onLoadStart: (controller, url) async {
setState(() {
Expand All @@ -149,10 +151,10 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
"javascript",
"about"
].contains(uri.scheme)) {
if (await canLaunch(url)) {
if (await canLaunchUrl(uri)) {
// Launch the App
await launch(
url,
await launchUrl(
uri,
);
// and cancel the request
return NavigationActionPolicy.CANCEL;
Expand Down
2 changes: 1 addition & 1 deletion example/web/heavy-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>flutter_inappwebview_example</title>
<title>heavy page</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion example/web/page-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>flutter_inappwebview_example</title>
<title>page 2</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion example/web/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>flutter_inappwebview_example</title>
<title>page</title>
<link rel="manifest" href="manifest.json">
</head>
<body style="min-height: 3000px;min-width: 3000px;">
Expand Down
156 changes: 144 additions & 12 deletions lib/assets/web/web_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ window.flutter_inappwebview = {
event.stopPropagation();
return false;
},
prepare: function (settings) {
prepare: function(settings) {
webView.settings = settings;
var iframe = document.getElementById(iframeId);

Expand Down Expand Up @@ -117,7 +117,7 @@ window.flutter_inappwebview = {
};

var originalPrint = iframe.contentWindow.print;
iframe.contentWindow.print = function () {
iframe.contentWindow.print = function() {
var iframeUrl = iframe.src;
try {
iframeUrl = iframe.contentWindow.location.href;
Expand Down Expand Up @@ -194,7 +194,7 @@ window.flutter_inappwebview = {
try {

if (!webView.settings.javaScriptCanOpenWindowsAutomatically) {
iframe.contentWindow.open = function () {
iframe.contentWindow.open = function() {
throw new Error('JavaScript cannot open windows automatically');
};
}
Expand Down Expand Up @@ -231,12 +231,12 @@ window.flutter_inappwebview = {
});
}
},
setSettings: function (newSettings) {
setSettings: function(newSettings) {
var iframe = webView.iframe;
try {
if (webView.settings.javaScriptCanOpenWindowsAutomatically != newSettings.javaScriptCanOpenWindowsAutomatically) {
if (!newSettings.javaScriptCanOpenWindowsAutomatically) {
iframe.contentWindow.open = function () {
iframe.contentWindow.open = function() {
throw new Error('JavaScript cannot open windows automatically');
};
} else {
Expand Down Expand Up @@ -294,7 +294,7 @@ window.flutter_inappwebview = {

webView.settings = newSettings;
},
reload: function () {
reload: function() {
var iframe = webView.iframe;
if (iframe != null && iframe.contentWindow != null) {
try {
Expand All @@ -305,7 +305,7 @@ window.flutter_inappwebview = {
}
}
},
goBack: function () {
goBack: function() {
var iframe = webView.iframe;
if (iframe != null) {
try {
Expand All @@ -315,7 +315,7 @@ window.flutter_inappwebview = {
}
}
},
goForward: function () {
goForward: function() {
var iframe = webView.iframe;
if (iframe != null) {
try {
Expand All @@ -325,7 +325,7 @@ window.flutter_inappwebview = {
}
}
},
goForwardOrForward: function (steps) {
goForwardOrForward: function(steps) {
var iframe = webView.iframe;
if (iframe != null) {
try {
Expand All @@ -335,7 +335,7 @@ window.flutter_inappwebview = {
}
}
},
evaluateJavascript: function (source) {
evaluateJavascript: function(source) {
var iframe = webView.iframe;
var result = null;
if (iframe != null) {
Expand All @@ -345,7 +345,7 @@ window.flutter_inappwebview = {
}
return result;
},
stopLoading: function (steps) {
stopLoading: function(steps) {
var iframe = webView.iframe;
if (iframe != null) {
try {
Expand All @@ -355,7 +355,7 @@ window.flutter_inappwebview = {
}
}
},
getUrl: function () {
getUrl: function() {
var iframe = webView.iframe;
var url = iframe.src;
try {
Expand All @@ -365,6 +365,138 @@ window.flutter_inappwebview = {
}
return url;
},
getTitle: function() {
var iframe = webView.iframe;
var title = null;
try {
title = iframe.contentDocument.title;
} catch (e) {
console.log(e);
}
return title;
},
injectJavascriptFileFromUrl: function(urlFile, scriptHtmlTagAttributes) {
var iframe = webView.iframe;
try {
var d = iframe.contentDocument;
var script = d.createElement('script');
for (var key of Object.keys(scriptHtmlTagAttributes)) {
if (scriptHtmlTagAttributes[key] != null) {
script[key] = scriptHtmlTagAttributes[key];
}
}
if (script.id != null) {
script.onload = function() {
window.flutter_inappwebview.nativeCommunication('onInjectedScriptLoaded', webView.viewId, [script.id]);
}
script.onerror = function() {
window.flutter_inappwebview.nativeCommunication('onInjectedScriptError', webView.viewId, [script.id]);
}
}
script.src = urlFile;
if (d.body != null) {
d.body.appendChild(script);
}
} catch (e) {
console.log(e);
}
},
injectCSSCode: function(source) {
var iframe = webView.iframe;
try {
var d = iframe.contentDocument;
var style = d.createElement('style');
style.innerHTML = source;
if (d.head != null) {
d.head.appendChild(style);
}
} catch (e) {
console.log(e);
}
},
injectCSSFileFromUrl: function(urlFile, cssLinkHtmlTagAttributes) {
var iframe = webView.iframe;
try {
var d = iframe.contentDocument;
var link = d.createElement('link');
for (var key of Object.keys(cssLinkHtmlTagAttributes)) {
if (cssLinkHtmlTagAttributes[key] != null) {
link[key] = cssLinkHtmlTagAttributes[key];
}
}
link.type = 'text/css';
var alternateStylesheet = "";
if (cssLinkHtmlTagAttributes.alternateStylesheet) {
alternateStylesheet = "alternate ";
}
link.rel = alternateStylesheet + "stylesheet";
link.href = urlFile;
if (d.head != null) {
d.head.appendChild(link);
}
} catch (e) {
console.log(e);
}
},
scrollTo: function(x, y, animated) {
var iframe = webView.iframe;
try {
if (animated) {
iframe.contentWindow.scrollTo({top: y, left: x, behavior: 'smooth'});
} else {
iframe.contentWindow.scrollTo(x, y);
}
} catch (e) {
console.log(e);
}
},
scrollBy: function(x, y, animated) {
var iframe = webView.iframe;
try {
if (animated) {
iframe.contentWindow.scrollBy({top: y, left: x, behavior: 'smooth'});
} else {
iframe.contentWindow.scrollBy(x, y);
}
} catch (e) {
console.log(e);
}
},
printCurrentPage: function() {
var iframe = webView.iframe;
try {
iframe.contentWindow.print();
} catch (e) {
console.log(e);
}
},
getContentHeight: function() {
var iframe = webView.iframe;
try {
return iframe.contentDocument.documentElement.scrollHeight;
} catch (e) {
console.log(e);
}
return null;
},
getSelectedText: function() {
var iframe = webView.iframe;
try {
var txt;
var w = iframe.contentWindow;
if (w.getSelection) {
txt = w.getSelection().toString();
} else if (w.document.getSelection) {
txt = w.document.getSelection().toString();
} else if (w.document.selection) {
txt = w.document.selection.createRange().text;
}
return txt;
} catch (e) {
console.log(e);
}
return null;
},
};

return webView;
Expand Down
Loading

0 comments on commit 68f25d0

Please sign in to comment.