Giter Club home page Giter Club logo

Comments (8)

btate avatar btate commented on July 17, 2024

Can I see some code?

Thanks,
Brandon

On Nov 2, 2012, at 3:55 AM, coder4567 [email protected] 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.

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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();

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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();

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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>"+ "<script type="text/javascript" src='rangy-core.js'></script>" + "<script type=
"text/javascript" src='rangy-serializer.js'></script>" + "<script type="text/javascript" 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("");
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);

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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.

from btandroidwebviewselection.

coder4567 avatar coder4567 commented on July 17, 2024

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.

from btandroidwebviewselection.

btate avatar btate commented on July 17, 2024

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

On Nov 12, 2012, at 4:48 AM, coder4567 [email protected] 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.

from btandroidwebviewselection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.