btate / BTAndroidWebViewSelection

Example user selection for android web views.
313 stars 126 forks source link

import js files #6

Closed coder4567 closed 11 years ago

coder4567 commented 12 years ago

hy, I would like to thank u for this excellent webview selection, saved my life. but I'm facing a small issue with the import. When I use loadDataWithBaseURL the javascript import is not working, any idea why? I need to use loadDataWithBaseURL cause I'm making some modification to the html before loading it. Thank u in advance for your time,

btate commented 12 years ago

Can I see some code?

Thanks, Brandon

On Nov 2, 2012, at 3:55 AM, coder4567 notifications@github.com wrote:

hy, I would like to thank u for this excellent webview selection, saved my life. but I'm facing a small issue with the import. When I use loadDataWithBaseURL the javascript import is not working, any idea why? I need to use loadDataWithBaseURL cause I'm making some modification to the html before loading it. Thank u in advance for your time,

— Reply to this email directly or view it on GitHub.

coder4567 commented 12 years ago

this is the method used:

public void loadSpineItemAtIndex(int spIndex) { readingWV.loadData("", "text/html", "UTF-8"); spineLoading.show();

    final int spine = spIndex;
    // common unzip directory
    new Thread(new Runnable() {
        @Override
        public void run() {
            setChapterTitle("");
            SpineItemRef spItem = bookSpine.getSpineItemRefWithOrder(spine);
            ManifestItem manItem = bookManifest.getManifestItemWithId(spItem.getIdref());

            String itemPath = bookContentsPath + "/" + manItem.getHref();

            itemURL = "file://" + itemPath;
            templateString = "";
            File itemFile = new File(itemPath);
            setPagingDimensions();

            try {
                org.jsoup.nodes.Document htmlDoc = Jsoup.parse(itemFile, "UTF-8");

                Elements heads = htmlDoc.getElementsByTag("head");
                Element head = heads.get(0);

                String headString = head.toString();
                headString = headString.substring("<head>".length());
                int index = headString.indexOf("</head>");
                headString = headString.substring(0, index);

                String titleString = "";
                try{
                    //attempt to get chapter title from <title> in <head>
                    Element title = head.getElementsByTag("title").get(0);
                    titleString = title.text();
                } catch(Exception ex) {
                    ex.printStackTrace();
                }

                //get full path references to css files and edit them to be displayed correctly 
                Elements links = head.getElementsByTag("link");
                for (Element link : links) {
                    String type = link.attr("type");
                    if (type.equalsIgnoreCase("text/css")) {
                        String href = link.attr("href");
                        if (href.startsWith("../")) {
                            href = href.substring(3);
                        }
                        String path = bookContentsPath + "/" + href;
                        path = URLDecoder.decode(path);
                        editCSSFileAtPath(path);
                    }
                }
                headString = headString + "<script src='jquery.js'></script>"+"<script src='rangy-core.js'></script>"+"<script src='rangy-serializer.js'></script>"+"<script src='android.selection.js'></script>";

                Elements bodys = htmlDoc.getElementsByTag("body");
                Element body = bodys.get(0);

                String bodyString = body.toString();
                bodyString = bodyString.substring("<body".length());
                int aIndex = bodyString.indexOf(">");
                bodyString = bodyString.substring(aIndex+1);
                int bIndex = bodyString.indexOf("</body>");
                bodyString = bodyString.substring(0, bIndex);

                bodyString = bodyString.replaceAll("\n", " ");

                //creating final html string to load into webview
                templateString = getAssetsFileContents(getString(R.string.template_file));
                templateString = templateString.replaceFirst("headplaceholder", headString);

                String finalBody = "";
                finalBody = "<div id='iReaderDiv'>" + bodyString+ "</div>";

                templateString = templateString.replaceFirst("bodyplaceholder", finalBody);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //////////////////////////////////////////////////////////////////////////////////
            readingWV.post(new Runnable() {
                @Override
                public void run() {
                    readingWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", null);
                    goToPage(readingWV.getCurrentPage());
                    spineLoading.hide();
                }
            });
        }
    }).start();
coder4567 commented 12 years ago

public void loadSpineItemAtIndex(int spIndex) { readingWV.loadData("", "text/html", "UTF-8"); spineLoading.show();

    final int spine = spIndex;
    // common unzip directory
    new Thread(new Runnable() {
        @Override
        public void run() {
            setChapterTitle("");
            SpineItemRef spItem = bookSpine.getSpineItemRefWithOrder(spine);
            ManifestItem manItem = bookManifest.getManifestItemWithId(spItem.getIdref());

            String itemPath = bookContentsPath + "/" + manItem.getHref();

            itemURL = "file://" + itemPath;
            templateString = "";
            content = "";
            File itemFile = new File(itemPath);
            setPagingDimensions();

            try {
                org.jsoup.nodes.Document htmlDoc = Jsoup.parse(itemFile, "UTF-8");

                Elements heads = htmlDoc.getElementsByTag("head");
                Element head = heads.get(0);

                String headString = head.toString();
                headString = headString.substring("<head>".length());
                int index = headString.indexOf("</head>");
                headString = headString.substring(0, index);

                String titleString = "";
                try{
                    //attempt to get chapter title from <title> in <head>
                    Element title = head.getElementsByTag("title").get(0);
                    titleString = title.text();
                } catch(Exception ex) {
                    ex.printStackTrace();
                }

                //get full path references to css files and edit them to be displayed correctly 
                Elements links = head.getElementsByTag("link");
                for (Element link : links) {
                    String type = link.attr("type");
                    if (type.equalsIgnoreCase("text/css")) {
                        String href = link.attr("href");
                        if (href.startsWith("../")) {
                            href = href.substring(3);
                        }
                        String path = bookContentsPath + "/" + href;
                        path = URLDecoder.decode(path);
                        editCSSFileAtPath(path);
                    }
                }
                headString = headString + "<script src='jquery.js'></script>"+"<script src='rangy-core.js'></script>"+"<script src='rangy-serializer.js'></script>"+"<script src='android.selection.js'></script>";

                Elements bodys = htmlDoc.getElementsByTag("body");
                Element body = bodys.get(0);

                String bodyString = body.toString();
                bodyString = bodyString.substring("<body".length());
                int aIndex = bodyString.indexOf(">");
                bodyString = bodyString.substring(aIndex+1);
                int bIndex = bodyString.indexOf("</body>");
                bodyString = bodyString.substring(0, bIndex);

                bodyString = bodyString.replaceAll("\n", " ");

                //creating final html string to load into webview
                templateString = getAssetsFileContents(getString(R.string.template_file));
                templateString = templateString.replaceFirst("headplaceholder", headString);

                String finalBody = "";
                finalBody = "<div id='iReaderDiv'>" + bodyString+ "</div>";

                templateString = templateString.replaceFirst("bodyplaceholder", finalBody);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //////////////////////////////////////////////////////////////////////////////////
            readingWV.post(new Runnable() {
                @Override
                public void run() {
                    readingWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", null);

                    goToPage(readingWV.getCurrentPage());
                    spineLoading.hide();
                }
            });
        }
    }).start();
coder4567 commented 12 years ago

oh in the first line, there is some html that didn't show html body body /html I removed the tags so it would show, thank u

coder4567 commented 11 years ago

hy, look I'm not sure why the code is not showing, if u want give me ur mail and I can send it for u. I will try one more time. headString = headString + "<script type=\"text/javascript\" src='jquery.js'>"+ "<script type=\"text/javascript\" src='rangy-core.js'>" + "<script type= \"text/javascript\" src='rangy-serializer.js'>" + "<script type=\"text/javascript\" src='android.selection.js'>"; Elements bodys = htmlDoc.getElementsByTag("body"); Element body = bodys.get(0); String bodyString = body.toString(); bodyString = bodyString.substring("<body".length()); int aIndex = bodyString.indexOf(">"); bodyString = bodyString.substring(aIndex+1); int bIndex = bodyString.indexOf(""); bodyString = bodyString.substring(0, bIndex); bodyString = bodyString.replaceAll("\n", " "); //creating final html string to load into webview templateString = getAssetsFileContents(getString(R.string.template_file)); templateString = templateString.replaceFirst("headplaceholder", headString); String finalBody = ""; if (!readingWV.isBookLanguage()) { // arabic finalBody = "

"+ bodyString + "
"; } else { finalBody = "
" + bodyString+ "
"; } templateString = templateString.replaceFirst("bodyplaceholder", finalBody); ////////////////////////////////////////////////////////////////////////////////// readingWV.post(new Runnable() { @Override public void run() { System.out.println("templateString"+templateString); readingWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", null);

coder4567 commented 11 years ago

if I remove the content of templateString and put it in an html file in the assets and call loadUrl instead of loadDataWithBaseURL, the javascript content gets called, but I need to use loadDataWithBaseURL for I need to add the javascript file into the header. Thanks in advance.

coder4567 commented 11 years ago

the import worked finally, I had to use: script type="text/javascript" src="file:///android_asset/jquery.js" script script type="text/javascript" src="file:///android_asset/rangy-core.js" script script type="text/javascript" src="file:///android_asset/rangy-serializer.js" script script type="text/javascript" src="file:///android_asset/android.selection.js" script instead of : script src='jquery.js' script script src='rangy-core.js' script script src='rangy-serializer.js' script script src='android.selection.js' script

I removed the tags so it can show.

btate commented 11 years ago

That makes sense. The files were using relative paths for the javascript imports.

On Nov 12, 2012, at 4:48 AM, coder4567 notifications@github.com wrote:

the import worked finally, I had to use: script type="text/javascript" src="file:///android_asset/jquery.js" script script type="text/javascript" src="file:///android_asset/rangy-core.js" script script type="text/javascript" src="file:///android_asset/rangy-serializer.js" script script type="text/javascript" src="file:///android_asset/android.selection.js" script instead of : script src='jquery.js' script script src='rangy-core.js' script script src='rangy-serializer.js' script script src='android.selection.js' script

I removed the tags so it can show.

— Reply to this email directly or view it on GitHub.