Firefox Extension: Get selected text

后端 未结 3 2049
南笙
南笙 2021-02-08 14:27

I am working on a simple Firefox Extension and I want to get the selected text. I tried this:

var WordCount = {
    /* ... */
    changeSelected: function() {
           


        
相关标签:
3条回答
  • 2021-02-08 14:39

    This works in firefox javascripting, so should be OK

    window.getSelection().toString();
    

    My guess is that document.commandDispatcher.focusedWindow fails

    0 讨论(0)
  • 2021-02-08 14:45

    Is this a normal Firefox extension or is it a JetPack Firefox extension.

    In JetPack it would be

    var doc = jetpack.tabs.focused.contentWindow;
    if (doc.wrappedJSObject){ //This just checks if Firefox has put a XPCNativeWrapper around it for security
      win = doc.wrappedJSObject;
    }
    

    or you can just access the window directly with window.getSelection() like dcaunt suggested

    0 讨论(0)
  • 2021-02-08 14:57

    Your problem is that document.commandDispatcher.focusedWindow is going to be pointing to a chrome window, where I suspect you actually want a content window. Try replacing that with content.getSelection()

    0 讨论(0)
提交回复
热议问题