PDA

View Full Version : Get selected text and use url in same function?



DoA
08-08-2009, 04:58 PM
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 -


<menuitem id="myid" label="MyLabel" accesskey="C"
insertafter="context-stop" oncommand="myFunction();"/>

and here's the (working) function that uses a hyperlink -


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.



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?

JShor
08-08-2009, 05:35 PM
Just integrate the two together.



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')
}

DoA
08-08-2009, 06:02 PM
Just integrate the two together.



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