Changeset 691
- Timestamp:
- 01/14/14 12:31:46 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r690 r691 139 139 140 140 JavaFX.WebEngineExecuteScript.invoke(webEngine, "" 141 + "var css = 'h1 { background: red; }';" // TODO141 + "var css = 'a * { outline: 0.1px outset rgba(9,9,9,0.001); }';" // TODO 142 142 + "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 151 148 + "head.appendChild(style);" 152 149 ); … … 179 176 ); 180 177 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 183 179 Object contentElements = JavaFX.WebEngineExecuteScript.invoke(webEngine, "document.querySelectorAll('body *');"); 184 180 int contentElementsLength = (Integer) JavaFX.JSObjectGetMember.invoke(contentElements, "length"); … … 191 187 JavaFX.WebEngineExecuteScript.invoke(webEngine, "para.style.wordBreak = 'normal';"); 192 188 193 // Creating a TreeWalker that is used to loop over all the TextNodes within the current paragraph189 // Creating a TreeWalker that is used to loop over all the TextNodes within the current element 194 190 JavaFX.WebEngineExecuteScript.invoke(webEngine, "var walker = document.createTreeWalker(para, NodeFilter.SHOW_TEXT, null, false);"); 195 191 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)"); 203 208 204 209 int nodesLength = (Integer) JavaFX.JSObjectGetMember.invoke(textNodes, "length"); … … 243 248 } 244 249 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 245 266 // Creating a TreeWalker that is used to loop over all the nodes within the document 246 267 JavaFX.WebEngineExecuteScript.invoke(webEngine, "var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL);"); … … 248 269 Node currentNode; 249 270 250 // Looping through all the text nodes in the current paragraph271 // Looping through all the nodes in the document 251 272 while ((currentNode = (Node) JavaFX.WebEngineExecuteScript.invoke(webEngine, "walker.nextNode()")) != null) { 252 273 … … 313 334 314 335 String textTransform = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "text-transform" }); 336 337 String linkUrl = (String) JavaFX.JSObjectGetMember.invoke(currentNode.getParentNode(), "href"); 315 338 316 339 Boolean fontFound = false; … … 421 444 t.setWidth(Math.round(width + (t.getSize()))); 422 445 446 if (!linkUrl.equals("undefined")) { 447 t.setAction("gotourl " + linkUrl); 448 t.setActionMark(false); 449 } 450 423 451 } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 424 452 … … 427 455 // 'ELEMENT_NODE' (and not 'TEXT_NODE' otherwise there would be double-ups 428 456 String bgImage = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-image" }); 457 458 String linkUrl = (String) JavaFX.JSObjectGetMember.invoke(currentNode, "href"); 429 459 430 460 if (bgImage.startsWith("url(")) { … … 488 518 489 519 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); 491 521 } catch (MalformedURLException mue) { 492 522 // probably a 'data:' url, not supported yet … … 499 529 if (currentNode.getNodeName().toLowerCase().equals("img") && (imgSrc = JavaFX.JSObjectGetMember.invoke(currentNode, "src").toString()) != null) { 500 530 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); 502 532 } catch (MalformedURLException mue) { 503 533 // probably a 'data:' url, not supported yet … … 599 629 * @throws IOException 600 630 */ 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, 602 632 float originXPercent, float originYPercent) 603 633 throws MalformedURLException, … … 685 715 686 716 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 } 687 722 688 723 frame.addItem(pic);
Note:
See TracChangeset
for help on using the changeset viewer.