2011-04-08 34 views

回答

1

這是不使用閃光燈。 getInputSelection函數來自這裏:Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?

<?xml version="1.0"?> 

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> 

<window id="mywin" title="my app" 
    width="800" height="600" persist="screenX screenY width height sizemode" 
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 

    <popupset> 
     <menupopup id="clipmenu"> 
     <menuitem label="Copy" oncommand="copy()"/> 
     <menuitem label="Paste" oncommand="paste();"/> 
     </menupopup> 
    </popupset> 

    <browser 
      type="content-primary" 
     src="http://127.0.0.1/" 
     flex="1" 
     disablehistory="true" 
     id="browserId" 
     context="clipmenu" 
     /> 
<script> 
<![CDATA[ 

    function copy() 
{ 
    var tabBrowser = document.getElementById("browserId"); 
    var selectedTagName = tabBrowser.contentWindow.document.activeElement.tagName; 
    var windowObj; 
    if(selectedTagName == "IFRAME") 
{ 
    windowObj = tabBrowser.contentWindow.frames[tabBrowser.contentWindow.document.activeElement.name]; 
} 
else 
{ 
    windowObj = document.getElementById("browserId").contentWindow; 
} 

var selectedText = windowObj.getSelection(); 

if(!selectedText || selectedText == "") 
{ 
    var focused = windowObj.document.activeElement; 
    if(focused && focused.value) 
    { 
     selectedText = getSelectionFromInput(focused); 
    } 
} 

//alert(selectedText + "---"); 

const clipHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); 
    clipHelper.copyString(selectedText); 
} 

function getSelectionFromInput(focused) 
{ 
var focusedValue = focused.value; 
var sel = getInputSelection(focused); 
var selectedText = ""; 
if(focusedValue.length == (sel.end)) 
{ 
    selectedText = focusedValue.substring(sel.start); 
} 
else 
{ 
    selectedText = focusedValue.substring(sel.start, (sel.end)); 
} 

return selectedText; 
} 

    function paste() 
    { 
     var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard); 
    var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); 
     trans.addDataFlavor("text/unicode"); 
     clip.getData(trans, clip.kGlobalClipboard); 
     var str = new Object(); 
     var len = new Object(); 
     trans.getTransferData("text/unicode",str,len); 
     str = str.value.QueryInterface(Components.interfaces.nsISupportsString); 
     str = str.data.substring(0, len.value/2); 

     var focused = document.commandDispatcher.focusedElement; 
var focusedValue = focused.value; 
var sel = getInputSelection(focused); 
focused.value = focusedValue.substring(0,sel.start) + str + focusedValue.substring(sel.end); 
} 

function getInputSelection(el) { 
var start = 0, end = 0, normalizedValue, range, 
    textInputRange, len, endRange; 

if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") { 
    start = el.selectionStart; 
    end = el.selectionEnd; 
} else { 
    range = document.selection.createRange(); 

    if (range && range.parentElement() == el) { 
     len = el.value.length; 
     normalizedValue = el.value.replace(/\r\n/g, "\n"); 

     // Create a working TextRange that lives only in the input 
     textInputRange = el.createTextRange(); 
     textInputRange.moveToBookmark(range.getBookmark()); 

     // Check if the start and end of the selection are at the very end 
     // of the input, since moveStart/moveEnd doesn't return what we want 
     // in those cases 
     endRange = el.createTextRange(); 
     endRange.collapse(false); 

     if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) { 
      start = end = len; 
     } else { 
      start = -textInputRange.moveStart("character", -len); 
      start += normalizedValue.slice(0, start).split("\n").length - 1; 

      if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) { 
       end = len; 
      } else { 
       end = -textInputRange.moveEnd("character", -len); 
       end += normalizedValue.slice(0, end).split("\n").length - 1; 
      } 
     } 
    } 
} 

return { 
    start: start, 
    end: end 
}; 
} 
]]> 
</script> 
</window> 

更新爲支持iframe。在XULRunner的下SWT瀏覽器 「複製」 上下文菜單

+0

由於項目不再活躍,我還沒有測試過這個解決方案 – 2013-10-07 12:53:32

1

使用下面的代碼創建菜單。

<popupset> 
<menupopup id="clipmenu"> 

     <menuitem label="Copy" oncommand="copy();"/> 
    <menuseparator/> 
    <menuitem label="paste" oncommand="paste();"/> 
</menupopup> 
</popupset> 

<browser type="content" src="chrome://myapp/content/theme1/index.html" flex="1" context="clipmenu"/> 

將它連接到任何你想要的,我在這裏使其通過給菜單的ID在瀏覽器中元素的上下文屬性瀏覽器元素的上下文菜單。

然後對於每個菜單,您可以給該命令在oncommand事件中執行。我們已經給了函數複製和粘貼。

然後編寫代碼從任何你想要的元素複製文本,或者如果你只想複製選定的數據。

有關複製命令,請參閱下面的鏈接。

http://www.deluxeblogtips.com/2010/06/javascript-copy-to-clipboard.html

+0

謝謝@esafwan。我想知道是否有直接的方式從XULRunner中訪問剪貼板而不使用閃存? – 2012-02-22 06:03:29

1

再舉一個例子(代碼是爲Eclipse V3.5.1,XULRunner的v1.8.1.3實現):

Browser browser = new Browser(parent, style | SWT.MOZILLA); 

Menu menu = new Menu(browser); 
MenuItem item = new MenuItem(menu, SWT.NONE); 
item.setText("Copy"); 
item.addSelectionListener(new SelectionListener() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 
     nsIWebBrowser webBrowser = (nsIWebBrowser) browser.getWebBrowser(); 
     nsIInterfaceRequestor req = (nsIInterfaceRequestor) webBrowser.queryInterface(nsIInterfaceRequestor.NS_IINTERFACEREQUESTOR_IID); 
     nsIDocShell docShell = (nsIDocShell) req.getInterface(nsIDocShell.NS_IDOCSHELL_IID); 
     nsIClipboardCommands cmds = (nsIClipboardCommands) docShell.queryInterface(nsIClipboardCommands.NS_ICLIPBOARDCOMMANDS_IID); 
     cmds.copySelection(); 
    } 

    @Override 
    public void widgetDefaultSelected(SelectionEvent e) { 
    } 
}); 
browser.setMenu(menu); 
相關問題