Changeset 897


Ignore:
Timestamp:
02/16/14 00:38:14 (10 years ago)
Author:
ngw8
Message:

Changed webparser to use a for loop (rather than while) for scrolling around the page, allowing the progress bar to be updated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/io/WebParser.java

    r889 r897  
    284284        public static void parsePageSimple(final JfxBrowser browserWidget, final WebEngine webEngine, final WebView webView, final Frame frame) {
    285285                try {
     286                       
     287                        final int verticalScrollPerPage = (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.85);
     288                        final int horizontalScrollPerPage = (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.85);
     289                                               
    286290                        Platform.runLater(new Runnable() {
    287291
     
    289293                                public void run() {
    290294                                        browserWidget.setOverlayVisible(true);
    291                                         browserWidget.setWebViewSize(FrameGraphics.getMaxFrameSize().getWidth() * 0.9, FrameGraphics.getMaxFrameSize().getHeight() * 0.9);
     295                                       
     296                                        // Webview area is set to slightly larger than the size of a converted page, to give some overlap between each page
     297                                        browserWidget.setWebViewSize(horizontalScrollPerPage * 1.1, verticalScrollPerPage * 1.1);
    292298                                        browserWidget.setScrollbarsVisible(false);
    293299                                }
     
    295301
    296302                        final Object notifier = new Object();
    297                        
    298                         final MutableBool bottomReached = new MutableBool(false);
    299                         final MutableBool rightReached = new MutableBool(false);
    300                        
     303                                               
    301304                        final MutableInt verticalCount = new MutableInt(0);
    302305                        final MutableInt horizontalCount = new MutableInt(0);
     306                       
     307                        final MutableInt pagesVertical = new MutableInt(1);
     308                        final MutableInt pagesHorizontal = new MutableInt(1);
    303309
    304310                        final String pageTitle;
     
    318324
    319325                        WebParser.addButton("Return to original frame", frame.getName(), null, 200, frameset, null, 0f, 10f, null);
    320 
    321326
    322327                        Text link = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(), pageTitle, null);
     
    435440                                                                // Storing the current scroll position
    436441                                                                + "var originalScrollX = window.pageXOffset;"
    437                                                                 + "var originalScrollY = window.pageYOffset");
    438                                                
    439                                                
    440 
     442                                                                + "var originalScrollY = window.pageYOffset;");
     443                                                                                               
     444                                                window.setMember("horizontalScrollPerPage", horizontalScrollPerPage);
     445                                                window.setMember("verticalScrollPerPage", verticalScrollPerPage);
     446                                               
     447                                               
     448                                               
     449                                                // The scrollPerPage will always be less than the page's height, due to the overlap being added/allowed for between pages,
     450                                                // but if the webpage fits in a single converted page, there's no need for any overlap, so just use 1 as the number of pages
     451                                                if((Boolean) webEngine.executeScript("document.documentElement.scrollHeight > window.innerHeight")) {
     452                                                        pagesVertical.setValue((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollHeight") / (float) verticalScrollPerPage));
     453                                                }
     454                                               
     455                                                if((Boolean) webEngine.executeScript("document.documentElement.scrollWidth > window.innerWidth")) {
     456                                                        pagesHorizontal.setValue((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollWidth") / (float) horizontalScrollPerPage));
     457                                                }
     458                                               
     459                                                System.out.println(webEngine.executeScript("document.documentElement.scrollWidth") + "/" + horizontalScrollPerPage);
     460                                                System.out.println(pagesVertical.getValue() + "x" + pagesHorizontal.getValue());
     461                                               
    441462                                                // Setting up the element that contains the CSS to hide all text. Also hiding readability mode buttons.
    442463                                                // This is wiped before the text is converted, then re-added before taking the screenshot
     
    543564                                                        try {
    544565                                                                progressBar.set((50 * (j + 1)) / nodesLength);
    545                                                         } catch (ArrayIndexOutOfBoundsException e) {
     566                                                        } catch (Exception e) {
     567                                                                // Seems to be a bug somewhere along the line when updating the progressbar, so am catching any exception
     568                                                                // thrown here to avoid it stuffing up the rest of the parsing
    546569                                                                e.printStackTrace();
    547570                                                        }
     
    585608
    586609                        // Loop that scrolls the page horizontally
    587                         while (!rightReached.getValue()) {
    588                                 bottomReached.setValue(false);
    589                                
     610                        for(int i = 0; i < pagesHorizontal.getValue(); i++) {
     611                                                               
    590612                                Platform.runLater(new Runnable() {
    591613                                        @Override
     
    595617                                                        webEngine.executeScript(""
    596618                                                                        + "scrollCounter = 0;"
    597                                                                         + "window.scrollTo(scrollCounterHorizontal * 0.9 * window.innerWidth, 0);"
     619                                                                        + "window.scrollTo(scrollCounterHorizontal * horizontalScrollPerPage, 0);"
    598620                                                                        + "scrollCounterHorizontal = scrollCounterHorizontal+1;");
    599 
    600                                                         rightReached.setValue((Boolean) webEngine.executeScript("(window.pageXOffset + window.innerWidth >= document.documentElement.scrollWidth)"));
    601621
    602622                                                } catch (Exception e) {
     
    607627                               
    608628                                // Loop that scrolls the page vertically (for each horizontal scroll position)
    609                                 while (!bottomReached.getValue()) {
     629                                for(int j = 0; j < pagesVertical.getValue(); j++) {
     630                                       
     631                                        try {
     632                                                progressBar.set((int) (50 + ((float)(j+1)/(pagesVertical.getValue() * pagesHorizontal.getValue())  + ((float)(i) / pagesHorizontal.getValue())) * 50));
     633                                        } catch (Exception e) {
     634                                                e.printStackTrace();
     635                                        }
     636                                       
    610637                                        Platform.runLater(new Runnable() {
    611638                                                @Override
     
    614641                                                                // Scrolling down the page
    615642                                                                webEngine.executeScript(""
    616                                                                                 + "window.scrollTo(window.pageXOffset, scrollCounter * 0.9 * window.innerHeight);"
     643                                                                                + "window.scrollTo(window.pageXOffset, scrollCounter * verticalScrollPerPage);"
    617644                                                                                + "scrollCounter = scrollCounter+1;");
    618 
    619                                                                 bottomReached.setValue((Boolean) webEngine.executeScript("(window.pageYOffset + window.innerHeight >= document.documentElement.scrollHeight)"));
    620645       
    621646                                                                synchronized (notifier) {
Note: See TracChangeset for help on using the changeset viewer.