Skip to content

Commit

Permalink
fix #56, some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Mar 12, 2019
1 parent 8db2a6b commit f3f0876
Show file tree
Hide file tree
Showing 24 changed files with 558 additions and 514 deletions.
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

501 changes: 248 additions & 253 deletions .idea/workspace.xml

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
## 1.0.1

- Fixed Unable to load initialFile on iOS #56
- Some code cleanup

## 1.0.0

Breaking changes:
- Fixed [Flutter AndroidX compatibility](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility), the latest version that doesn't use `AndroidX` is `0.6.0`.
- Fixed [Flutter AndroidX compatibility](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility), the latest version that doesn't use `AndroidX` is `0.6.0` (thanks to [juicycleff](https://github.com/juicycleff)).

## 0.6.0

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://git
### Requirements
- Dart sdk: ">=2.1.0-dev.7.1 <3.0.0"
- Flutter: ">=0.10.1 <2.0.0"
- Android: `minSdkVersion 17`
- iOS: `--ios-language swift`

### Note for Android
During the build, if Android fails with `Error: uses-sdk:minSdkVersion 16 cannot be smaller than version 17 declared in library`, it means that you need to update the `minSdkVersion` of your `build.gradle` file to at least `17`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public void onMethodCall(MethodCall call, Result result) {
case "injectScriptCode":
if (webView != null) {
source = call.argument("source").toString();
jsWrapper = "(function(){return JSON.stringify(eval(%s));})();";
webView.injectDeferredObject(source, jsWrapper, result);
webView.injectScriptCode(source, result);
}
else {
result.success("");
Expand All @@ -138,24 +137,21 @@ public void onMethodCall(MethodCall call, Result result) {
case "injectScriptFile":
if (webView != null) {
urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);";
webView.injectDeferredObject(urlFile, jsWrapper, null);
webView.injectScriptFile(urlFile);
}
result.success(true);
break;
case "injectStyleCode":
if (webView != null) {
source = call.argument("source").toString();
jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);";
webView.injectDeferredObject(source, jsWrapper, null);
webView.injectStyleCode(source);
}
result.success(true);
break;
case "injectStyleFile":
if (webView != null) {
urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);";
webView.injectDeferredObject(urlFile, jsWrapper, null);
webView.injectStyleFile(urlFile);
}
result.success(true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,47 +198,47 @@ public void loadUrl(String url, MethodChannel.Result result) {
if (webView != null) {
webView.loadUrl(url, result);
} else {
result.error(LOG_TAG, "Cannot load url " + url, null);
result.error(LOG_TAG, "webView is null", null);
}
}

public void loadUrl(String url, Map<String, String> headers, MethodChannel.Result result) {
if (webView != null) {
webView.loadUrl(url, headers, result);
} else {
result.error(LOG_TAG, "Cannot load url " + url, null);
result.error(LOG_TAG, "webView is null", null);
}
}

public void postUrl(String url, byte[] postData, MethodChannel.Result result) {
if (webView != null) {
webView.postUrl(url, postData, result);
} else {
result.error(LOG_TAG, "Cannot load url " + url, null);
result.error(LOG_TAG, "webView is null", null);
}
}

public void loadData(String data, String mimeType, String encoding, String baseUrl, MethodChannel.Result result) {
if (webView != null) {
webView.loadData(data, mimeType, encoding, baseUrl, result);
} else {
result.error(LOG_TAG, "Cannot load data", null);
result.error(LOG_TAG, "webView is null", null);
}
}

public void loadFile(String url, MethodChannel.Result result) {
if (webView != null) {
webView.loadFile(url, result);
} else {
result.error(LOG_TAG, "Cannot load file " + url, null);
result.error(LOG_TAG, "webView is null", null);
}
}

public void loadFile(String url, Map<String, String> headers, MethodChannel.Result result) {
if (webView != null) {
webView.loadFile(url, headers, result);
} else {
result.error(LOG_TAG, "Cannot load file " + url, null);
result.error(LOG_TAG, "webView is null", null);
}
}

Expand Down Expand Up @@ -420,13 +420,28 @@ public HashMap<String, Object> getOptions() {
return optionsMap;
}

public void injectDeferredObject(String source, String jsWrapper, MethodChannel.Result result) {
public void injectScriptCode(String source, MethodChannel.Result result) {
if (webView != null)
webView.injectDeferredObject(source, jsWrapper, result);
webView.injectScriptCode(source, result);
else
result.success("");
}

public void injectScriptFile(String urlFile) {
if (webView != null)
webView.injectScriptFile(urlFile);
}

public void injectStyleCode(String source) {
if (webView != null)
webView.injectStyleCode(source);
}

public void injectStyleFile(String urlFile) {
if (webView != null)
webView.injectStyleFile(urlFile);
}

public HashMap<String, Object> getCopyBackForwardList() {
if (webView != null)
return webView.getCopyBackForwardList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,25 +212,21 @@ public void run() {
break;
case "injectScriptCode":
source = call.argument("source").toString();
jsWrapper = "(function(){return JSON.stringify(eval(%s));})();";
injectDeferredObject(uuid, source, jsWrapper, result);
injectScriptCode(uuid, source, result);
break;
case "injectScriptFile":
urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(uuid, urlFile, jsWrapper, null);
injectScriptFile(uuid, urlFile);
result.success(true);
break;
case "injectStyleCode":
source = call.argument("source").toString();
jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(uuid, source, jsWrapper, null);
injectStyleCode(uuid, source);
result.success(true);
break;
case "injectStyleFile":
urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);";
injectDeferredObject(uuid, urlFile, jsWrapper, null);
injectStyleFile(uuid, urlFile);
result.success(true);
break;
case "show":
Expand Down Expand Up @@ -307,12 +303,33 @@ public void run() {

}

private void injectDeferredObject(String uuid, String source, String jsWrapper, final Result result) {
private void injectScriptCode(String uuid, String source, final Result result) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectDeferredObject(source, jsWrapper, result);
inAppBrowserActivity.injectScriptCode(source, result);
} else {
Log.d(LOG_TAG, "Can't inject code into the system browser");
Log.d(LOG_TAG, "webView is null");
}
}

private void injectScriptFile(String uuid, String urlFile) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectScriptFile(urlFile);
}
}

private void injectStyleCode(String uuid, String source) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectStyleCode(source);
}
}

private void injectStyleFile(String uuid, String urlFile) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectStyleFile(urlFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,26 @@ public void onReceiveValue(String s) {
});
}

public void injectScriptCode(String source, MethodChannel.Result result) {
String jsWrapper = "(function(){return JSON.stringify(eval(%s));})();";
injectDeferredObject(source, jsWrapper, result);
}

public void injectScriptFile(String urlFile) {
String jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(urlFile, jsWrapper, null);
}

public void injectStyleCode(String source) {
String jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(source, jsWrapper, null);
}

public void injectStyleFile(String urlFile) {
String jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);";
injectDeferredObject(urlFile, jsWrapper, null);
}

public HashMap<String, Object> getCopyBackForwardList() {
WebBackForwardList currentList = copyBackForwardList();
int currentSize = currentList.getSize();
Expand Down
6 changes: 3 additions & 3 deletions example/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
One of three columns
<a href="page-1.html">PAGE 1</a>
</div>
<div class="col-sm">
One of three columns
<a href="page-2.html">PAGE 2</a>
</div>
<div class="col-sm">
One of three columns
<a href="page-3.html">PAGE 3</a>
</div>
</div>
</div>
Expand Down
31 changes: 31 additions & 0 deletions example/assets/page-1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 1</h1>
</div>
</div>
</div>
<script>
console.log("page 1");
</script>
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions example/assets/page-2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 2</h1>
</div>
</div>
</div>
<script>
console.log("page 2");
</script>
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions example/assets/page-3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 3</h1>
</div>
</div>
</div>
<script>
console.log("page 3");
</script>
</div>
</body>
</html>
4 changes: 0 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
25A517508F43E58C47090625 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8D91E403808A7540F18B75D /* Pods_Runner.framework */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -39,7 +38,6 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -88,7 +86,6 @@
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
9740EEBA1CF902C7004384FC /* Flutter.framework */,
Expand Down Expand Up @@ -209,7 +206,6 @@
EDC1147F21735BC200D2247A /* Main.storyboard in Resources */,
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Loading

0 comments on commit f3f0876

Please sign in to comment.