DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Last post Author Topic: DONE: Copy selection on webpage as text with text hyperlink URL's and ASCIIart  (Read 40,342 times)

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
After a little fooling around with GreaseMonkey and some outright butchery of a few userscripts, I've come up with something guaranteed to turn any sane website into a masterpiece of confusion.

Using the AutoHK site you gave above as an example, you'll get:

2011-03-30_15-16-32.jpg

Code: (javascript)
// ==UserScript==
// @name          TextLink
// @namespace     
// @description   Append text link after clickable link
// @include       http://*
// @include       https://*
// @exclude       http://192.168.0.*
// ==/UserScript==

var links, link;
links = document.evaluate(
   '//a[@href]',
   document,
   null,
   XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
   null);

var re_http  = new RegExp("^http.*:\/\/");
var re_mail = new RegExp("^mailto:");
var re_ftp = new RegExp("^ftp.*:\/\/");
for (var i = 0; i < links.snapshotLength; i++) {
   a = links.snapshotItem(i);

   if(a.href.match(re_http)) {
      newDiv = document.createElement('text');
      a.parentNode.insertBefore(newDiv, a.nextSibling);
      newDiv.innerHTML = "&nbsp;[" + a + "]";
   }
   if(a.href.match(re_mail)) {
      newDiv = document.createElement('text');
      a.parentNode.insertBefore(newDiv, a.nextSibling);
      newDiv.innerHTML = "&nbsp;[" + a + "]";
   }
   if(a.href.match(re_ftp)) {
      newDiv = document.createElement('text');
      a.parentNode.insertBefore(newDiv, a.nextSibling);
      newDiv.innerHTML = "&nbsp;[" + a + "]";
   }
}

Save as TextLink.user.js - unless you have another called that, in which case save it as something else and change the name on the @name line.  Then just drop it on the Firefox window, if GreaseMonkey is installed it should recognise and install it.

I've only tested it on a couple of sites and I have no idea if what I've done follows Good Programming Guidelines but hey, what do expect for 30 minutes of completely unintelligent, uneducated keyboard tapping.

I'd recommend you leave it disabled until you're on a site you want to copy text from, then enable it and refresh ;)
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: March 30, 2011, 05:19 AM by 4wd »

Nater

  • Participant
  • Joined in 2011
  • *
  • Posts: 13
This works!  Awesome :up: 

I hope others can find this useful.

I have tried it out and it works great.  The only improvement I can think of for it would be to also recognize email addresses that are linked, but I am happy the way it is! 

THANK YOU 4wd!!!!!

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Nice work, 4wd.  Well done.  :D

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
This works!  Awesome :up: 

Thanks!

Quote
I have tried it out and it works great.  The only improvement I can think of for it would be to also recognize email addresses that are linked, but I am happy the way it is!

Stupid me, mailto addresses don't have // on the end, just edit it to change:

var re_mail = new RegExp("^mailto:\/\/");

to:

var re_mail = new RegExp("^mailto:");

and that should take care of it.

Nice work, 4wd.  Well done.  :D

Well, I wouldn't call it exactly 'nice' since I don't know if Element type of 'text' actually exists, that was just a guess on my part.  When it worked I stopped looking for the 'legal' method  ;D

Thanks skwire.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Well, I wouldn't call it exactly 'nice' since I don't know if Element type of 'text' actually exists, that was just a guess on my part.  When it worked I stopped looking for the 'legal' method

Whenever the OP says, "This works!  Awesome  :up:," well, that's nice work.  :D

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Does that mean I get the coveted csnacks badge ?

;)
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Nater

  • Participant
  • Joined in 2011
  • *
  • Posts: 13
var re_mail = new RegExp("^mailto:\/\/");

to:

var re_mail = new RegExp("^mailto:");

Updated and works with email now too.  Thanks!

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Does that mean I get the coveted csnacks badge ?

Absolutely.  =]

Nater

  • Participant
  • Joined in 2011
  • *
  • Posts: 13
skwire - thanks for your AHK script.  Though your script was not what I originally had in mind, I can use it with 4wd's greasemonkey user script and get even more functionality than I had originally bargained for.

I ended up getting 2 snacks for the price of one.

 

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
skwire - thanks for your AHK script.  Though your script was not what I originally had in mind, I can use it with 4wd's greasemonkey user script and get even more functionality than I had originally bargained for.

You're welcome.  I figured you would use both in conjunction.

I ended up getting 2 snacks for the price of one.

Gotta love a bargain.  :P

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Does that mean I get the coveted csnacks badge ?

Absolutely.  =]

Cool!  Next stop the SuperBoy badge  :P
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Original address: https://www.donationcoder.com/forum/index.php?topic=26164.0