views:

411

answers:

3

I need a javascript bookmark to take the url I have in the clipboard parse out the 2 numbers and create a new url, and add a link to the top of the page, that when clicked adds the url to my bookmark menu.

Say I have url's like these

http://www.website.com/frontpageeditor.jhtml?sectionID=2844&poolID=6276
javascript:getPoolPageUrl(9800,22713)

Then I need to add the numbers to this url

javascript:frames['content'].getPoolPageUrl(9800,22713)

and then add the url to the top of the frame "content".

I have tried forever on this, but I can't figure out it out.



Update
I've put something together, to show you what I need. This one doesn't work though.

Any ideas why?

var url = window.clipboardData.getData('Text');
var reg = /(\d+)/g;
var matches = url.match(reg); //returns ["2844","6276"]
var newUrl = "javascript:frames['content'].getPoolPageUrl("+matches[0]+","+matches[1]+")";
var link = document.createElement('a');
link.src = newUrl;
frames['content'].document.body.appendChild(link);



Update2
This works. Any changes I can do to make it even better?

var url = window.clipboardData.getData('text');
var matches = url.match(/(\d+)/g);
var link = frames['content'].document.createElement('a');
link.href = "javascript:frames['content'].getPoolPageUrl("+matches[0]+","+matches[1]+")";
link.innerHTML = document.title;
frames['content'].document.body.appendChild(link);
+5  A: 

Ok, first of all I think you cannot retrieve the text from clipboard from java script, my guess that it would be a major security issue if you can.

Let's assume you have the clipboard in a string you can call this function:

var url = "http://www.website.com/frontpageeditor.jhtml?sectionID=2844&poolID=6276"; //clip
var reg = /(\d+)/g;
var matches = url.match(reg); //returns ["2844","6276"]
var newUrl = "javascript:frames['content'].getPoolPageUrl("+matches[0]+","+matches[1]+")";
frames['content'].document.getElementById("linkPlaceHolderWhereYouWantToAdd").href=newUrl;
MahdeTo
Yes, I think you can copy text **to** the clipboard, but you need to use the OS's paste facility to retrieve it.
Cerebrus
+2  A: 

You're creating the element in one document, and then appending it to a child located in another document. This doesn't work. You need to create the element in the document that you're going to be adding it to.

Also, the a object doesn't have a src member, it uses href.

Eg:

var link = frames['content'].document.createElement('a');
link.href = newUrl;
link.innerHTML = newUrl;
frames['content'].document.body.appendChild(link);

Do note however, that window.clipboardData is IE-specific code.

Jon Benedicto
How can I place the link on the top of the page instead of at the bottom?
mofle
if (document.body.firstChild) { document.body.insertBefore(link, document.body.firstChild)}else { document.body.appendChild(link);}
Jon Benedicto
Thank you for this ;)
mofle
A: 

JavaScript is not permitted to access the clipboard's contents for one reason alone: Security.

If you accidentally copied your credit card number or some other personally-identifying information into your clipboard, and you visited a malicious website, it could easily snatch up your clipboard and send it off to the server before you even knew there was a risk. So, browser developers explicitly forbid it.

greyfade
Like Jon benedicto said. It is possible in IE.
mofle
But only in the local zone, correct? If not, this is still a glaring security hole.
greyfade