Results 1 to 3 of 3

Thread: Get selected text and use url in same function?

  1. #1
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get selected text and use url in same function?

    Hi guys,

    I've got a script that is working with linked text but I want it to work with both already linked text <a href> and user selected text.... in Firefox by the way (It's gonna be a FF addon)

    Here's how I'm calling the function -

    Code:
      <menuitem id="myid" label="MyLabel" accesskey="C"
      insertafter="context-stop" oncommand="myFunction();"/>
    and here's the (working) function that uses a hyperlink -

    Code:
    function myFunction() {
    
    try {url = gContextMenu.linkURL} // new FF, other?
    catch(e) {
       try {url = gContextMenu.linkURL()} // old FF, SM, other?
       catch(e) {url = String(gContextMenu.link)} // either FF, other?
    }
    window.open('http://myurl.com/file.php?url=' + url, 'window name')
    }
    Here's the code for the user selected portion that I want to integrate.

    Code:
    function foo() {
       var selObj = window.getSelection(); 
       alert(selObj);
       var selRange = selObj.getRangeAt(0);
       // do stuff with the range
    }
    Is it possible to do it in one function or do I need two - in which case is it possible to call 2 functions simultaneously?

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Just integrate the two together.

    Code:
    function myFunction() {
    
       var selObj = window.getSelection(); 
       alert(selObj);
       var selRange = selObj.getRangeAt(0);
       // do stuff with the range
    
    try {url = gContextMenu.linkURL} // new FF, other?
    catch(e) {
       try {url = gContextMenu.linkURL()} // old FF, SM, other?
       catch(e) {url = String(gContextMenu.link)} // either FF, other?
    }
    window.open('http://myurl.com/file.php?url=' + url, 'window name')
    }
    - Josh

  3. #3
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    Just integrate the two together.

    Code:
    function myFunction() {
    
       var selObj = window.getSelection(); 
       alert(selObj);
       var selRange = selObj.getRangeAt(0);
       // do stuff with the range
    
    try {url = gContextMenu.linkURL} // new FF, other?
    catch(e) {
       try {url = gContextMenu.linkURL()} // old FF, SM, other?
       catch(e) {url = String(gContextMenu.link)} // either FF, other?
    }
    window.open('http://myurl.com/file.php?url=' + url, 'window name')
    }
    Hi,

    I should have mentioned that the user selected part of the code will be used to define URL as well ... not both parts of the code can define URL at the same time, therefore it's giving back a blank result because if it is selected text then by the time it gets to the gContextMenu portion of the code (the right click part) then the url is reset to blank ...

    What I really need is a check to see whether the user used selected text or a hyperlink to run the script - then run only the appropriate section...

    Just not sure how to do that in JS

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •