在 SWT 瀏覽器小部件中獲取選定文本 (Getting a Selected Text In SWT Browser Widgets)


問題描述

在 SWT 瀏覽器小部件中獲取選定文本 (Getting a Selected Text In SWT Browser Widgets)

I am implementing SWT Browser in my Project for displaying a WebPage. After displaying a web page into this browser, if a user selects particular text, how can I get the selected text? please help me with suitable example 


參考解法

方法 1:

Maybe this link will help you out.  

This link posits the idea of using a StatusTextListener to intercept text selections.  Below is a snippet of the StatusTextListener implementation.

// Status Text Listener Interface. 
public void changed (StatusTextEvent evt) { 
    String text = evt.text; 
    if ((text != null) && (text.length() > 0)) { 
        if ((! text.equalsIgnoreCase ("Done")) && (! text.equals (lastStatusText))) { 
            System.err.println (evt.text); 
            lastStatusText = text; 
        } 
    } 
} // End of changed(). 

方法 2:

This seems to work well for me.  Give it a try.  

static String SCRIPT01 = "var html = \"\";"+
"if (typeof window.getSelection != \"undefined\") {"+
    "var sel = window.getSelection();"+
    "if (sel.rangeCount) {"+
        "var container = document.createElement(\"div\");"+
        "for (var i = 0, len = sel.rangeCount; i < len; ++i) {"+
            "container.appendChild(sel.getRangeAt(i).cloneContents());"+
        "}"+
        "html = container.innerHTML;"+
    "}"+
"} else if (typeof document.selection != \"undefined\") {"+
    "if (document.selection.type == \"Text\") {"+
        "html = document.selection.createRange().htmlText;"+
    "}" +
"}" +
"return html";

browser.addMouseListener(new MouseListener() {
    @Override
    public void mouseUp(MouseEvent arg0) {
        String selection = (String) browser.evaluate(SCRIPT01);
    }

    @Override
    public void mouseDown(MouseEvent arg0) {
    }

    @Override
    public void mouseDoubleClick(MouseEvent arg0) {
        String selection = (String) browser.evaluate(SCRIPT01);
    }
});

(by AbhitAdam GreenAdam Green)

參考文件

  1. Getting a Selected Text In SWT Browser Widgets (CC BY‑SA 3.0/4.0)

#java #Browser #swt #eclipse-rcp






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論