package com.creativetrends.app.simplicity.webview;

import android.content.Context;
import android.util.Base64;
import android.util.Log;
import android.webkit.WebView;

import java.io.InputStream;


@SuppressWarnings (value="unused")
public class CSSInjection {

    static void injectInstaTheme(Context context, WebView view) {
        instagramLight(context, view);
        Log.d("Plugin enabled", "on " + view.getUrl());
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    private static void instagramLight(Context context, WebView view) {
        try {
            InputStream inputStream = context.getAssets().open("instagram.css");
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
            view.loadUrl("javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var style = document.createElement('style');" +
                    "style.type = 'text/css';" +
                    "style.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(style)" +
                    "})()");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void injectPhotos(WebView webView){
        webView.loadUrl("javascript:var targetNode = document.querySelector('body');\nvar config = { attributes: true, childList: true, subtree: true };\nvar callback = function(mutationsList, observer) {\nvar classname_home = document.querySelectorAll(\"._7ab_\");\nfor (var i = 0; i < classname_home.length; i++) {\nif(classname_home[i].parentNode.hasAttribute(\"href\") || classname_home[i].parentNode.parentNode.hasAttribute(\"href\")) {\nclassname_home[i].parentNode.removeAttribute(\"href\") || classname_home[i].parentNode.parentNode.removeAttribute(\"href\");\nclassname_home[i].addEventListener('click', function(event) {\nvar targetElement = event.target || event.srcElement;\nvar hr = targetElement.querySelector('div i');\nvar style = hr.currentStyle || window.getComputedStyle(hr, false);\nvar bi = style.backgroundImage.slice(4, -1).replace(/\"/g, \"\");\nPhotos.getImage(bi);})}}\nvar classname = document.getElementsByClassName(\"_i81\");\nfor (var i = 0; i < classname.length; i++) {\nvar dd = classname[i].getAttribute(\"data-sheets\");\nif (!dd) {\nclassname[i].setAttribute(\"data-sheets\", \"simple\");\nclassname[i].addEventListener('click', function(event) {\nvar get = this.querySelector('img');\nvar base = get.src;\nPhotos.getImage(base);});}}};\nvar observer = new MutationObserver(callback);\nobserver.observe(targetNode, config);\n");
        Log.d("Plugin enabled", "on " + webView.getUrl());
    }




  @SuppressWarnings("ResultOfMethodCallIgnored")
    public static void injectDarkMode(Context context, WebView view) {
        try {
            InputStream inputStream = context.getAssets().open("darkmode.css");
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
            view.loadUrl("javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var style = document.createElement('style');" +
                    "style.type = 'text/css';" +
                    "style.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(style)" +
                    "})()");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@@ -1394,1372 +1394,8 @@
 w)%7B%0A
-        webView.loadUrl(%22javascript:var targetNode = document.querySelector('body');%5Cnvar config = %7B attributes: true, childList: true, subtree: true %7D;%5Cnvar callback = function(mutationsList, observer) %7B%5Cnvar classname_home = document.querySelectorAll(%5C%22._7ab_%5C%22);%5Cnfor (var i = 0; i %3C classname_home.length; i++) %7B%5Cnif(classname_home%5Bi%5D.parentNode.hasAttribute(%5C%22href%5C%22) %7C%7C classname_home%5Bi%5D.parentNode.parentNode.hasAttribute(%5C%22href%5C%22)) %7B%5Cnclassname_home%5Bi%5D.parentNode.removeAttribute(%5C%22href%5C%22) %7C%7C classname_home%5Bi%5D.parentNode.parentNode.removeAttribute(%5C%22href%5C%22);%5Cnclassname_home%5Bi%5D.addEventListener('click', function(event) %7B%5Cnvar targetElement = event.target %7C%7C event.srcElement;%5Cnvar hr = targetElement.querySelector('div i');%5Cnvar style = hr.currentStyle %7C%7C window.getComputedStyle(hr, false);%5Cnvar bi = style.backgroundImage.slice(4, -1).replace(/%5C%22/g, %5C%22%5C%22);%5CnPhotos.getImage(bi);%7D)%7D%7D%5Cnvar classname = document.getElementsByClassName(%5C%22_i81%5C%22);%5Cnfor (var i = 0; i %3C classname.length; i++) %7B%5Cnvar dd = classname%5Bi%5D.getAttribute(%5C%22data-sheets%5C%22);%5Cnif (!dd) %7B%5Cnclassname%5Bi%5D.setAttribute(%5C%22data-sheets%5C%22, %5C%22simple%5C%22);%5Cnclassname%5Bi%5D.addEventListener('click', function(event) %7B%5Cnvar get = this.querySelector('img');%5Cnvar base = get.src;%5CnPhotos.getImage(base);%7D);%7D%7D%7D;%5Cnvar observer = new MutationObserver(callback);%5Cnobserver.observe(targetNode, config);%5Cn%22);%0A
     

Update CSSInjection.java