Changeset 691


Ignore:
Timestamp:
01/14/14 12:31:46 (10 years ago)
Author:
ngw8
Message:

Links partially working in converted web pages

File:
1 edited

Legend:

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

    r690 r691  
    139139                                               
    140140                                                JavaFX.WebEngineExecuteScript.invoke(webEngine, ""
    141                                                                 + "var css = 'h1 { background: red; }';" // TODO
     141 + "var css = 'a * { outline: 0.1px outset rgba(9,9,9,0.001); }';" // TODO
    142142                                                                + "var head = document.head;"
    143                                                                 + "style = document.createElement('style');"
    144                                                                 + "style.type = 'text/css';"
    145                                                                 + "if (style.styleSheet){"
    146                                                                 + "             style.styleSheet.cssText = css;"
    147                                                                 + "} "
    148                                                                 + "else {"
    149                                                                 + "             style.appendChild(document.createTextNode(css));"
    150                                                                 + "}"
     143                                                                + "var style = document.createElement('style');"
     144                                                                + "style.id = 'expediteeparser';"
     145
     146                                                                + "style.appendChild(document.createTextNode(css));"
     147
    151148                                                                + "head.appendChild(style);"
    152149                                                                );
     
    179176                                                );
    180177                                       
    181                                                 // Getting an array of HTML elements from the page that will be checked for 'content' (i.e. will be modified to be
    182                                                 // properly wrapped in Expeditee)
     178                                                // Getting an array of all HTML elements in the page
    183179                                                Object contentElements = JavaFX.WebEngineExecuteScript.invoke(webEngine, "document.querySelectorAll('body *');");
    184180                                                int contentElementsLength = (Integer) JavaFX.JSObjectGetMember.invoke(contentElements, "length");
     
    191187                                                        JavaFX.WebEngineExecuteScript.invoke(webEngine, "para.style.wordBreak = 'normal';");
    192188
    193                                                         // Creating a TreeWalker that is used to loop over all the TextNodes within the current paragraph
     189                                                        // Creating a TreeWalker that is used to loop over all the TextNodes within the current element
    194190                                                        JavaFX.WebEngineExecuteScript.invoke(webEngine, "var walker = document.createTreeWalker(para, NodeFilter.SHOW_TEXT, null, false);");
    195191
    196                                                         // Using Javascript to get an array of all the text nodes in the current node. Have to loop through twice (once
    197                                                         // to
    198                                                         // build the array and once actually going through the array, otherwise when the textnode is removed from
    199                                                         // the document items end up being skipped)
    200                                                         Object textNodes = JavaFX.WebEngineExecuteScript.invoke(webEngine, "function getTextNodes(rootNode){" + "var node;" + "var textNodes=[];"
    201                                                                         + "var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);" + "while(node=walk.nextNode()) {" + "if((node.textContent.trim().length > 0)) { "
    202                                                                         + "textNodes.push(node);" + "}" + "}" + "return textNodes;" + "}; " + "getTextNodes(para)");
     192                                                        // Using Javascript to get an array of all the text nodes in the current element. Have to loop through twice (once
     193                                                        // to build the array and once actually going through the array, otherwise when the textnode is removed from the
     194                                                        // document items end up being skipped)
     195                                                        Object textNodes = JavaFX.WebEngineExecuteScript.invoke(webEngine, ""
     196                                                                        + "function getTextNodes(rootNode){"
     197                                                                                + "var node;"
     198                                                                                + "var textNodes=[];"
     199                                                                                + "var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);"
     200                                                                                + "while(node=walk.nextNode()) {"
     201                                                                                        + "if((node.textContent.trim().length > 0)) { "
     202                                                                                                + "textNodes.push(node);"
     203                                                                                        + "}"
     204                                                                                + "}"
     205                                                                                + "return textNodes;"
     206                                                                        + "}; "
     207                                                                        + "getTextNodes(para)");
    203208
    204209                                                        int nodesLength = (Integer) JavaFX.JSObjectGetMember.invoke(textNodes, "length");
     
    243248                                                }
    244249                                               
     250                                                // Finding all links within the page, then setting the href attribute of all their descendants to be the same
     251                                                // link/URL.
     252                                                // This is needed because there is no apparent and efficient way to check if an element is a child of a link when
     253                                                // running through the document when added each element to Expeditee
     254                                                JavaFX.WebEngineExecuteScript.invoke(webEngine, ""
     255                                                                + "var anchors = document.getElementsByTagName('a');"
     256                                                                + ""
     257                                                                + "for (var i = 0; i < anchors.length; i++) {"
     258                                                                        + "var currentAnchor = anchors.item(i);"
     259                                                                        + "var anchorDescendants = currentAnchor.querySelectorAll('*');"
     260                                                                        + "for (var j = 0; j < anchorDescendants.length; j++) {"
     261                                                                                + "anchorDescendants.item(j).href = currentAnchor.href;"
     262                                                                        + "}"
     263                                                                + "}"
     264                                                                );
     265                                               
    245266                                                // Creating a TreeWalker that is used to loop over all the nodes within the document
    246267                                                JavaFX.WebEngineExecuteScript.invoke(webEngine, "var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL);");
     
    248269                                                Node currentNode;
    249270
    250                                                 // Looping through all the text nodes in the current paragraph
     271                                                // Looping through all the nodes in the document
    251272                                                while ((currentNode = (Node) JavaFX.WebEngineExecuteScript.invoke(webEngine, "walker.nextNode()")) != null) {
    252273
     
    313334
    314335                                                                                String textTransform = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "text-transform" });
     336
     337                                                                                String linkUrl = (String) JavaFX.JSObjectGetMember.invoke(currentNode.getParentNode(), "href");
    315338
    316339                                                                                Boolean fontFound = false;
     
    421444                                                                                t.setWidth(Math.round(width + (t.getSize())));
    422445
     446                                                                                if (!linkUrl.equals("undefined")) {
     447                                                                                        t.setAction("gotourl " + linkUrl);
     448                                                                                        t.setActionMark(false);
     449                                                                                }
     450
    423451                                                                        } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
    424452
     
    427455                                                                                // 'ELEMENT_NODE' (and not 'TEXT_NODE' otherwise there would be double-ups
    428456                                                                                String bgImage = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-image" });
     457                                                                               
     458                                                                                String linkUrl = (String) JavaFX.JSObjectGetMember.invoke(currentNode, "href");
    429459
    430460                                                                                if (bgImage.startsWith("url(")) {
     
    488518
    489519                                                                                        try {
    490                                                                                                 WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat, originXPercent, originYPercent);
     520                                                                                                WebParser.addImageFromUrl(bgImage, linkUrl, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat, originXPercent, originYPercent);
    491521                                                                                        } catch (MalformedURLException mue) {
    492522                                                                                                // probably a 'data:' url, not supported yet
     
    499529                                                                                if (currentNode.getNodeName().toLowerCase().equals("img") && (imgSrc = JavaFX.JSObjectGetMember.invoke(currentNode, "src").toString()) != null) {
    500530                                                                                        try {
    501                                                                                                 WebParser.addImageFromUrl(imgSrc, frame, x, y, (int) width, null, null, null, null, null, 0, 0);
     531                                                                                                WebParser.addImageFromUrl(imgSrc, linkUrl, frame, x, y, (int) width, null, null, null, null, null, 0, 0);
    502532                                                                                        } catch (MalformedURLException mue) {
    503533                                                                                                // probably a 'data:' url, not supported yet
     
    599629         * @throws IOException
    600630         */
    601         private static void addImageFromUrl(String imgSrc, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat,
     631        private static void addImageFromUrl(String imgSrc, String linkUrl, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat,
    602632                        float originXPercent, float originYPercent)
    603633                        throws MalformedURLException,
     
    685715
    686716                pic.setCrop((int)(cropStartX * invScale), (int)(cropStartY * invScale), (int)(cropEndX * invScale), (int)(cropEndY * invScale));
     717               
     718                if (linkUrl != null && !linkUrl.equals("undefined")) {
     719                        pic.setAction("goto " + linkUrl);
     720                        pic.setActionMark(false);
     721                }
    687722
    688723                frame.addItem(pic);
Note: See TracChangeset for help on using the changeset viewer.