From b1bed909976529ce67d147f659ec220f6b8956b3 Mon Sep 17 00:00:00 2001 From: fkloft Date: Sat, 4 Jan 2014 23:13:37 +0100 Subject: [PATCH] Use special domain with standard http(s) instead of custom protocol (Avoids warning about insecure content when using https) --- .../cradle/iitc_mobile/IITC_FileManager.java | 13 +++-- .../iitc_mobile/IITC_WebViewClient.java | 48 ++++++++++--------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java index b28bb735..1d805852 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java @@ -27,6 +27,8 @@ public class IITC_FileManager { "script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));\n" + "(document.body || document.head || document.documentElement).appendChild(script);"; + public static final String DOMAIN = ".iitcm.localhost"; + public static HashMap getScriptInfo(String js) { HashMap map = new HashMap(); String header = ""; @@ -183,16 +185,19 @@ public class IITC_FileManager { return getScriptInfo(stream).get("version"); } - public WebResourceResponse getResponse(String url) { - Uri uri = Uri.parse(url); - + public WebResourceResponse getResponse(Uri uri) { String host = uri.getHost(); + if (!host.endsWith(DOMAIN)) + return EMPTY; + + host = host.substring(0, host.length() - DOMAIN.length()); + if ("script".equals(host)) return getScript(uri); if ("user-plugin".equals(host)) return getUserPlugin(uri); - Log.e("could not generate response for url: " + url); + Log.e("could not generate response for url: " + uri); return EMPTY; } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index 885ba36c..0cee4d0a 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -22,8 +22,8 @@ public class IITC_WebViewClient extends WebViewClient { private static final ByteArrayInputStream STYLE = new ByteArrayInputStream( "body, #dashboard_container, #map_canvas { background: #000 !important; }" .getBytes()); - private static final ByteArrayInputStream EMPTY = new ByteArrayInputStream( - "".getBytes()); + private static final ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); + private static final String DOMAIN = IITC_FileManager.DOMAIN; private String mIitcPath; private boolean mIitcInjected = false; @@ -63,7 +63,7 @@ public class IITC_WebViewClient extends WebViewClient { private void loadScripts(IITC_WebView view) { List scripts = new LinkedList(); - scripts.add("script/total-conversion-build.user.js"); + scripts.add("script" + DOMAIN + "/total-conversion-build.user.js"); // get the plugin preferences SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mIitc); @@ -74,9 +74,9 @@ public class IITC_WebViewClient extends WebViewClient { String plugin = entry.getKey(); if (plugin.endsWith(".user.js") && entry.getValue().toString().equals("true")) { if (plugin.startsWith(mIitcPath)) { - scripts.add("user-plugin" + plugin); + scripts.add("user-plugin" + DOMAIN + plugin); } else { - scripts.add("script/plugins/" + plugin); + scripts.add("script" + DOMAIN + "/plugins/" + plugin); } } } @@ -87,7 +87,7 @@ public class IITC_WebViewClient extends WebViewClient { } String js = "(function(){['" + TextUtils.join("','", scripts) + "'].forEach(function(src) {" + - "var script = document.createElement('script');script.src = 'iitcm://'+src;" + + "var script = document.createElement('script');script.src = '//'+src;" + "(document.body || document.head || document.documentElement).appendChild(script);" + "});})();"; @@ -104,11 +104,11 @@ public class IITC_WebViewClient extends WebViewClient { // ((IITC_Mobile) mContext).onReceivedLoginRequest(this, view, realm, account, args); } - // Check every external resource if it’s okay to load it and maybe replace - // it - // with our own content. This is used to block loading Niantic resources - // which aren’t required and to inject IITC early into the site. - // via http://stackoverflow.com/a/8274881/1684530 + /** + * Check every external resource if it's okay to load it and maybe replace it with our own content. + * This is used to block loading Niantic resources which aren’t required and to inject IITC early into the site. + * via http://stackoverflow.com/a/8274881/1684530 + */ @Override public WebResourceResponse shouldInterceptRequest(final WebView view, String url) { // if any tiles are requested, handle it with IITC_TileManager @@ -125,14 +125,14 @@ public class IITC_WebViewClient extends WebViewClient { e.printStackTrace(); return super.shouldInterceptRequest(view, url); } - } else if (url.contains("/css/common.css")) { + } + + if (url.contains("/css/common.css")) { + // return custom stylesheet return new WebResourceResponse("text/css", "UTF-8", STYLE); - // } else if (url.contains("gen_dashboard.js")) { - // // define initialize function to get rid of JS ReferenceError on intel page's 'onLoad' - // String gen_dashboard_replacement = "window.initialize = function() {}"; - // return new WebResourceResponse("text/javascript", "UTF-8", - // new ByteArrayInputStream(gen_dashboard_replacement.getBytes())); - } else if (url.contains("/css/ap_icons.css") + } + + if (url.contains("/css/ap_icons.css") || url.contains("/css/map_icons.css") || url.contains("/css/common.css") || url.contains("/css/misc_icons.css") @@ -142,12 +142,16 @@ public class IITC_WebViewClient extends WebViewClient { || url.contains("/css/portalrender_mobile.css") || url.contains("js/analytics.js") || url.contains("google-analytics.com/ga.js")) { + // don't load stylesheets return new WebResourceResponse("text/plain", "UTF-8", EMPTY); - } else if (url.startsWith("iitcm:")) { - return mIitc.getFileManager().getResponse(url); - } else { - return super.shouldInterceptRequest(view, url); } + + Uri uri = Uri.parse(url); + if (uri.getHost().endsWith(DOMAIN) && + ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme()))) + return mIitc.getFileManager().getResponse(uri); + + return super.shouldInterceptRequest(view, url); } // start non-ingress-intel-urls in another app...