Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"cordova": {
"id": "cordova-plugin-webview-proxy",
"platforms": [
"ios"
"ios",
"android"
]
},
"repository": {
Expand All @@ -18,7 +19,8 @@
"cordova",
"webview",
"wkwebview",
"ios"
"ios",
"android"
],
"scripts": {
"test": "paramedic.."
Expand Down
14 changes: 13 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
<description>Proxy HTTP(S) requests through native code to make CORS and Cookies work in cordova-ios</description>
<license>MIT</license>
<engines>
<engine name="cordova-android" version=">=10.0.0" />
<engine name="cordova-ios" version=">=6.2.0" />
</engines>
<repo>https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy.git</repo>
<issue>https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy/issues</issue>
<keywords>cordova,webview,wkwebview,ios</keywords>
<keywords>cordova,webview,wkwebview,ios,android</keywords>

<js-module src="www/WebviewProxy.js" name="WebviewProxy">
<clobbers target="WebviewProxy"/>
Expand All @@ -29,4 +30,15 @@
</config-file>
<source-file src="src/ios/WebviewProxy.m" />
</platform>

<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="WebviewProxy">
<param name="android-package" value="com.gi.cordova.plugin.webviewproxy.WebviewProxy"/>
<param name="onload" value="true" />
</feature>
</config-file>
<source-file src="src/android/WebviewProxy.java" target-dir="src/com/gi/cordova/plugin/webviewproxy"/>
<framework src="androidx.webkit:webkit:1.3.0" type="gradleReference" />
Comment thread
NiklasMerz marked this conversation as resolved.
</platform>
</plugin>
61 changes: 61 additions & 0 deletions src/android/WebviewProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.gi.cordova.plugin.webviewproxy;

import android.util.Log;
import android.webkit.WebResourceResponse;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaPluginPathHandler;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import androidx.webkit.WebViewAssetLoader;

public class WebviewProxy extends CordovaPlugin {

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);

Log.d("WebviewProxy", "Proxy init");
}

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
return false;
}

public CordovaPluginPathHandler getPathHandler() {
WebViewAssetLoader.PathHandler pathHandler = new WebViewAssetLoader.PathHandler() {
@Override
public WebResourceResponse handle(String path) {
if (path.startsWith("_https_proxy_") || path.startsWith("_http_proxy_")) {
try {
URL url = new URL(path.replace("_https_proxy_", "https://").replace("_http_proxy_", "http://"));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String mimeType = urlConnection.getContentType();
return new WebResourceResponse(mimeType, null, in);
} finally {
//urlConnection.disconnect();
}
} catch(Exception e) {
Log.e("WebviewProxy", e.getMessage());
}
}
return null;
}
};

Log.d("WebviewProxy", "Add proxy");
return new CordovaPluginPathHandler(pathHandler);
}

}

24 changes: 14 additions & 10 deletions www/WebviewProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ function WebviewProxy() {
}

WebviewProxy.prototype.convertProxyUrl = function (path) {
if (!path || !window.CDV_ASSETS_URL) {
return path;
}
if (path.startsWith('http://')) {
return window.CDV_ASSETS_URL + '/_http_proxy_' + encodeURIComponent(path.replace('http://', ''));
}
if (path.startsWith('https://')) {
return window.CDV_ASSETS_URL + '/_https_proxy_' + encodeURIComponent(path.replace('https://', ''));
}
return path;
if (!path) {
return path;
}
if (!window.CDV_ASSETS_URL) {
//Android 10.x origin
window.CDV_ASSETS_URL = location.origin
}
if (path.startsWith('http://')) {
return window.CDV_ASSETS_URL + '/_http_proxy_' + encodeURIComponent(path.replace('http://', ''));
}
if (path.startsWith('https://')) {
return window.CDV_ASSETS_URL + '/_https_proxy_' + encodeURIComponent(path.replace('https://', ''));
}
return path;
}

module.exports = new WebviewProxy();