Changeset 811


Ignore:
Timestamp:
02/03/14 12:29:36 (10 years ago)
Author:
ngw8
Message:

Switched new webparser method to use a loop when adding elements to the page, since there was no longer any reason to use a recursive function

File:
1 edited

Legend:

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

    r808 r811  
    336336                                                        int visibleHeight = (Integer) webEngine.executeScript("window.innerHeight");
    337337
    338                                                         WebParser.addTextToFrame(doc, visibleWidth, visibleHeight, window, webEngine, frameToAddTo);
     338                                                        WebParser.addTextToFrame(visibleWidth, visibleHeight, window, webEngine, frameToAddTo);
    339339                                                        System.out.println("added text");
    340340                                                        FrameIO.SaveFrame(frameToAddTo);
     
    431431                                public void run() {
    432432                                        try {
     433                                                JSObject window = (JSObject) webEngine.executeScript("window");
     434
    433435                                                webEngine.executeScript(""
    434436                                                                // Initializing the counter used when scrolling the page
     
    443445                                                                + "cssHide.appendChild(document.createTextNode(style));"
    444446                                                                + "document.getElementsByTagName('head')[0].appendChild(cssHide);");
    445                                                
    446                                                 HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
    447 
    448                                                 JSObject window = (JSObject) webEngine.executeScript("window");
    449 
    450                                                 frame.setBackgroundColor(rgbStringToColor((String) ((JSObject) (window.call("getComputedStyle", new Object[] { doc }))).call("getPropertyValue",
    451                                                                 new Object[] { "background-color" })));
    452                                                
     447
    453448                                                // Functions to be used later in JavaScript
    454449                                                webEngine.executeScript(""
     
    11441139         * @throws IllegalAccessException
    11451140         */
    1146         private static void addTextToFrame(Node rootElement, int visibleWidth, int visibleHeight, JSObject window, WebEngine webEngine, Frame frame) throws InvocationTargetException,
     1141        private static void addTextToFrame(int visibleWidth, int visibleHeight, JSObject window, WebEngine webEngine, Frame frame) throws InvocationTargetException,
    11471142                        IllegalAccessException, IllegalArgumentException {
    11481143
    1149                 Node currentNode = rootElement;
    1150 
    1151                 if (currentNode.getNodeType() == Node.TEXT_NODE) {
    1152 
     1144                webEngine.executeScript("var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);");
     1145               
     1146                Node currentNode;
     1147               
     1148                while ((currentNode = (Node) webEngine.executeScript("walker.nextNode()")) != null) {   
    11531149                        JSObject style;
    11541150                        JSObject bounds;
     
    11601156                        bounds = (JSObject) ((JSObject) currentNode.getParentNode()).call("getBoundingClientRect", new Object[] {});
    11611157
    1162 
    1163                         // Bounding rectangle position is relative to the current view, so scroll position must be added to x/y
    1164                         // TODO: This doesn't check if an element or any of its parent elements have position:fixed set - the only
    1165                         // way to check seems to be to walking through the element's parents until the document root is reached
     1158                        // TODO: This doesn't check if an element or any of its parent elements have position:fixed set - the only way to check seems to be to walking through the element's parents until the document root is reached (or a recursive function)
    11661159                        float x = Float.valueOf(bounds.getMember("left").toString());
    11671160                        float y = Float.valueOf(bounds.getMember("top").toString());
     
    13241317                                }
    13251318                        }
    1326 
    1327                 } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
    1328                         Node childNode = currentNode.getFirstChild();
    1329 
    1330                         while (childNode != null) {
    1331                                 addTextToFrame(childNode, visibleWidth, visibleHeight, window, webEngine, frame);
    1332                                 childNode = childNode.getNextSibling();
    1333                         }
    13341319                }
    13351320        }
Note: See TracChangeset for help on using the changeset viewer.