From 6f2f1302be7ba01b47ef5e18ec241c3e14c61968 Mon Sep 17 00:00:00 2001 From: Stefan Breunig Date: Sun, 10 Mar 2013 17:10:31 +0100 Subject: [PATCH] =?UTF-8?q?include=20IITC=20in=20the=20app=E2=80=99s=20res?= =?UTF-8?q?ources=20and=20load=20it=20from=20there?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IITC can only be updated with the app from now on, as the remote-loading code has been removed. The new injection method intercepts all resources being requested by the web page and either injects our code or and empty string. This should stop IITCM from requesting them at all, saving band- width and reducing the boot up time dramatically. --- mobile/.gitignore | 1 - mobile/assets/iitc.js | 1 + .../com/cradle/iitc_mobile/IITC_WebView.java | 10 +-- .../iitc_mobile/IITC_WebViewClient.java | 73 +++++++++++++++---- 4 files changed, 63 insertions(+), 22 deletions(-) create mode 120000 mobile/assets/iitc.js diff --git a/mobile/.gitignore b/mobile/.gitignore index c6e43131..a36f859a 100644 --- a/mobile/.gitignore +++ b/mobile/.gitignore @@ -1,7 +1,6 @@ .classpath .project .settings/ -assets/ bin/ gen/ libs/ diff --git a/mobile/assets/iitc.js b/mobile/assets/iitc.js new file mode 120000 index 00000000..fce3d449 --- /dev/null +++ b/mobile/assets/iitc.js @@ -0,0 +1 @@ +../../iitc-debug.user.js \ No newline at end of file diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java index 88b18db1..9440df04 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java @@ -15,7 +15,7 @@ public class IITC_WebView extends WebView { private IITC_WebViewClient webclient; // init web view - private void iitc_init() { + private void iitc_init(Context c) { settings = this.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); @@ -31,7 +31,7 @@ public class IITC_WebView extends WebView { } }); - webclient = new IITC_WebViewClient(); + webclient = new IITC_WebViewClient(c); this.setWebViewClient(webclient); } @@ -39,19 +39,19 @@ public class IITC_WebView extends WebView { public IITC_WebView(Context context) { super(context); - iitc_init(); + iitc_init(context); } public IITC_WebView(Context context, AttributeSet attrs) { super(context, attrs); - iitc_init(); + iitc_init(context); } public IITC_WebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - iitc_init(); + iitc_init(context); } //---------------------------------------------------------------- diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index d36180ed..7d6e04ec 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -1,13 +1,50 @@ package com.cradle.iitc_mobile; +import android.content.Context; import android.net.http.SslError; import android.util.Log; import android.webkit.CookieManager; import android.webkit.SslErrorHandler; +import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; +import java.io.InputStream; +import java.io.StringBufferInputStream; + public class IITC_WebViewClient extends WebViewClient { + private static final StringBufferInputStream style = new StringBufferInputStream( + "body, #dashboard_container, #map_canvas { background: #000 !important; }"); + private static final StringBufferInputStream empty = new StringBufferInputStream(""); + + private static WebResourceResponse iitcjs; + + public IITC_WebViewClient(Context c) throws java.io.IOException { + loadIITC_JS(c); + } + + private static void loadIITC_JS(Context c) throws java.io.IOException { + InputStream input; + input = c.getAssets().open("iitc.js"); + + int size = input.available(); + byte[] buffer = new byte[size]; + input.read(buffer); + input.close(); + String js = new String(buffer); + // need to wrap the mobile iitc.js version in a document ready. IITC + // expects to be injected after the DOM has been loaded completely. + // Since the mobile client injects IITC by replacing the gen_dashboard + // file, IITC runs to early. The document.ready delays IITC long enough + // so it boots correctly. + js = "$(document).ready(function(){" + js + "});"; + + iitcjs = new WebResourceResponse( + "text/javascript", + "UTF-8", + new StringBufferInputStream(js) + ); + }; // enable https @Override @@ -15,23 +52,27 @@ public class IITC_WebViewClient extends WebViewClient { handler.proceed() ; }; - // injecting IITC when page is loaded + // 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 void onPageFinished(WebView web, String Url) { - Log.d("loading finish", web.getUrl()); - if (web.getUrl().contains("ingress.com/intel") && !web.getUrl().contains("accounts")) { - // first check for cookies, than inject javascript - // this enables the user to login if necessary - CookieManager cm = CookieManager.getInstance(); - final String cookie = cm.getCookie("https://www.ingress.com/intel"); - if(cookie != null) { - web.loadUrl("javascript: (function() { " - + "var script=document.createElement('script');" - + "script.type='text/javascript';" - + "script.src='https://iitcserv.appspot.com/iitc-nightly/iitc-nightly-latest.user.js';" - + "document.getElementsByTagName('head').item(0).appendChild(script);" - + "})()"); - } + public WebResourceResponse shouldInterceptRequest (final WebView view, String url) { + if(url.contains("/css/common.css")) { + return new WebResourceResponse("text/css", "UTF-8", style); + } else if(url.contains("gen_dashboard.js")) { + return this.iitcjs; + } else if(url.contains("/css/ap_icons.css") + || url.contains("/css/map_icons.css") + || url.contains("/css/misc_icons.css") + || url.contains("/css/style_full.css") + || url.contains("/css/style_mobile.css") + || url.contains("/css/portalrender.css") + || url.contains("js/analytics.js") + || url.contains("google-analytics.com/ga.js")) { + return new WebResourceResponse("text/plain", "UTF-8", empty); + } else { + return super.shouldInterceptRequest(view, url); } } }