Ignore:
Timestamp:
02/03/14 16:03:18 (10 years ago)
Author:
jts21
Message:

Add ability to extract images from a web browser by right clicking them

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r813 r815  
    3636import javafx.scene.web.WebEngine;
    3737import javafx.scene.web.WebView;
     38import netscape.javascript.JSObject;
    3839
    3940import org.expeditee.gui.DisplayIO;
     
    4344import org.expeditee.io.WebParser;
    4445import org.expeditee.items.Item;
     46import org.expeditee.items.Picture;
    4547import org.expeditee.items.Text;
    4648import org.expeditee.settings.network.NetworkSettings;
     
    382384                                                String selection = (String) JfxBrowser.this._webEngine.executeScript("window.getSelection().toString()");
    383385
    384                                                 // Do nothing if no text is selected
    385                                                 if (!selection.equals("")) {
     386                                                // If no text is selected, see if an image is under the cursor
     387                                                if (selection.length() == 0) {
     388                                                        JSObject window = (JSObject) JfxBrowser.this._webEngine.executeScript("window");
     389                                                        Object image = JfxBrowser.this._webEngine.executeScript(""
     390                                                                        + "function isImage(o) {"
     391                                                                        + "  if(o.tagName == \"IMG\" && o.src != null) {"
     392                                                                        + "    return true;"
     393                                                                        + "  }"
     394                                                                        + "  if(window.getComputedStyle(o).getPropertyValue(\"background-image\").indexOf(\"url\") == 0) {"
     395                                                                        + "    return true;"
     396                                                                        + "  }"
     397                                                                        + "  return false;"
     398                                                                        + "};"
     399                                                                        + "var clicked = document.elementFromPoint(" + e.getX() + "," + e.getY() + ");"
     400                                                                        + "if(isImage(clicked)) {"
     401                                                                        + "  clicked;"
     402                                                                        + "} else {"
     403                                                                        // TODO: look in child elements and other elements behind the current one
     404                                                                        // For now just return nothing
     405                                                                        + "  null;"
     406                                                                        + "}"
     407                                                                        + "");
     408                                                        if(image instanceof org.w3c.dom.Node) {
     409                                                                System.out.println(image);
     410                                                                try {
     411                                                                        JSObject style = (JSObject) window.call("getComputedStyle", image);
     412
     413                                                                        JSObject bounds = (JSObject) ((JSObject) image).call("getBoundingClientRect", new Object[] {});
     414                                                                        float width = Float.valueOf(bounds.getMember("width").toString());
     415                                                                        float height = Float.valueOf(bounds.getMember("height").toString());
     416                                                                       
     417                                                                        Picture pic;
     418                                                                       
     419                                                                        org.w3c.dom.Node node = ((org.w3c.dom.Node) image);
     420                                                                       
     421                                                                        if (((String) style.call("getPropertyValue", new Object[] { "background-image" })).startsWith("url(")) {
     422                                                                                pic = WebParser.getBackgroundImageFromNode(node, style, DisplayIO.getCurrentFrame(), null,
     423                                                                                        (float) FrameMouseActions.getX(), (float) FrameMouseActions.getY(), width, height);
     424                                                                        } else {
     425                                                                                String imgSrc;
     426                                                                                if(node.getNodeName().toLowerCase().equals("img") &&
     427                                                                                                (imgSrc = ((JSObject) node).getMember("src").toString()) != null) {
     428                                                                                        pic = WebParser.getImageFromUrl(imgSrc, null, DisplayIO.getCurrentFrame(),
     429                                                                                                        (float) FrameMouseActions.getX(), (float) FrameMouseActions.getY(), (int) width, null, null, null, null, null, 0, 0);
     430                                                                                } else {
     431                                                                                        return;
     432                                                                                }
     433                                                                        }
     434                                                                        pic.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
     435                                        FrameMouseActions.pickup(pic);
     436                                        } catch (Exception e1) {
     437                                        // TODO Auto-generated catch block
     438                                        e1.printStackTrace();
     439                                }
     440                                                        }
     441                                                } else {
    386442                                                        // Copy text and attach to cursor
    387443                                                        Text t = new Text(selection);
Note: See TracChangeset for help on using the changeset viewer.