Skip to content

Commit

Permalink
add JS API method: loadUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Sep 3, 2020
1 parent b5ac378 commit a6853a2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,23 @@ Minor improvement to the [WebView GM library](https://github.com/wbayer/webview-
- `extras` is a list of String name/value pairs
* example:
- `('android.intent.action.VIEW', 'http://example.com/video.mp4', 'video/mp4', 'referUrl', 'http://example.com/videos.html')`
- `GM_loadUrl(url, ...headers)`
* loads a URL into the WebView with additional HTTP request headers
* where:
- `url` is a String URL
- `headers` is a list of String name/value pairs
* example:
- `('http://example.com/iframe_window.html', 'Referer', 'http://example.com/parent_window.html')`
- `GM_exit()`
* causes [WebMonkey](https://github.com/warren-bank/Android-WebMonkey) to close

#### Caveats

* userscripts only run in the top window
- they are __not__ loaded into iframes
* issue [#1](https://github.com/warren-bank/Android-WebMonkey/issues/1)
- a userscript will __not__ run when its `@name` begins with a numeric character

#### Legal:

* copyright: [Warren Bank](https://github.com/warren-bank)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void onCreate(Bundle savedInstanceState) {
WebViewGm webViewGm = scriptBrowser.getWebView();
String secret = webViewGm.getWebViewClient().getSecret();

WmJsApi jsApi = new WmJsApi(secret, this);
WmJsApi jsApi = new WmJsApi(secret, this, webViewGm);

webViewGm.addJavascriptInterface(jsApi.getGlobalJsApi(), WmJsApi.GlobalJsApiNamespace);
((WmScriptStore) scriptStore).addScript(jsApi.getWrappedJsApi());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
import android.os.Build;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import java.util.HashMap;

public class WmJsApi {

public static final String TAG = "WebViewGmApi";

public String secret;
public Activity activity;
public WebView webview;
public boolean useES6;

public WmJsApi(String secret, Activity activity) {
public WmJsApi(String secret, Activity activity, WebView webview) {
this.secret = secret;
this.activity = activity;
this.webview = webview;
this.useES6 = (Build.VERSION.SDK_INT >= 21); // use ES5 in Android <= 4.4 because WebView is outdated and cannot be updated
}

Expand Down Expand Up @@ -89,6 +94,40 @@ else if ((type != null) && (type.length() > 0)) {
}
}

@JavascriptInterface
public void loadUrl(String secret, String url, String[] headers) {
if (!WmJsApi.this.secret.equals(secret)) {
Log.e(WmJsApi.TAG, "Call to \"loadUrl\" did not supply correct secret");
return;
}
activity.runOnUiThread(new Runnable() {
public void run() {
try {
if ((headers != null) && (headers.length >= 2)) {
int length = (headers.length % 2 == 0)
? headers.length
: (headers.length - 1)
;

HashMap<String, String> httpHeaders = new HashMap<String, String>();

for (int i=0; i < length; i+=2) {
httpHeaders.put(headers[i], headers[i+1]);
}

webview.loadUrl(url, httpHeaders);
}
else {
webview.loadUrl(url);
}
}
catch(Exception e) {
Log.e(WmJsApi.TAG, "Call to \"loadUrl\" did not supply valid input and raised the following error", e);
}
}
});
}

@JavascriptInterface
public void exit(String secret) {
if (!WmJsApi.this.secret.equals(secret)) {
Expand Down Expand Up @@ -119,6 +158,10 @@ public String getWrappedJsApi() {
? ("var GM_startIntent" + " = function(action, data, type, ...extras) { " + jsBridgeName + ".startIntent(" + defaultSignature + ", action, data, type, extras);" + " };\n")
: ("var GM_startIntent" + " = function(action, data, type) { " + jsBridgeName + ".startIntent(" + defaultSignature + ", action, data, type, Array.prototype.slice.call(arguments, 3));" + " };\n")
;
jsApi += (useES6)
? ("var GM_loadUrl" + " = function(url, ...headers) { " + jsBridgeName + ".loadUrl(" + defaultSignature + ", url, headers);" + " };\n")
: ("var GM_loadUrl" + " = function(url) { " + jsBridgeName + ".loadUrl(" + defaultSignature + ", url, Array.prototype.slice.call(arguments, 1));" + " };\n")
;
jsApi += "var GM_exit" + " = function() { " + jsBridgeName + ".exit(" + defaultSignature + ");" + " };\n";

return jsApi;
Expand Down
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("001000408", 10)
releaseVersion = '001.00.04-08API'
releaseVersionCode = Integer.parseInt("001000508", 10)
releaseVersion = '001.00.05-08API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 8
targetSdkVersion = 28
Expand Down
13 changes: 13 additions & 0 deletions tests/0005-load-url.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ==UserScript==
// @name test: GM_startIntent
// @namespace WebViewWM
// @match *://*
// @run-at document-start
// ==/UserScript==

// ==========================================
// https://httpbin.org/#/operations/Request%20inspection/get_headers
// ==========================================

if (unsafeWindow.location.hostname !== "httpbin.org")
GM_loadUrl(/* url= */ "https://httpbin.org/headers", /* headers: */ "Refer", "https://WebMonkey.com/", "User-Agent", "WebMonkey", "X-Requested-With", "WebMonkey");
File renamed without changes.

0 comments on commit a6853a2

Please sign in to comment.