0

I managed to add text to what's being copied using the code in this answer(the second option). However, when I change the string concatenation and add a call to replace, I get an error "replace is not a function".

copytext = window.getSelection().replace(/some pattern/, 'replace value'); // Fails

The "selection" object seems to be very complex and I can't even find the text inside it. I could call toString on it but that's not an option because I'm copying HTML from a contenteditable div and I need to preserve the formatting.

I'm trying to do this because I have relative links in the div's content and they're being converted to absolute links in the copied text for some reason. This only happens when accessing my demo from rawgit. Locally, it works normally.

Any ideas on how I could accomplish this?

UPDATE

Here's a jsfiddle with my current setup: https://jsfiddle.net/8kx8v8pb/

Community
  • 1
  • 1
Ariel
  • 3,383
  • 4
  • 43
  • 58
  • 1
    The `+` operator just coerces the object to a string. Call `.toString()` explicitly (or fake it with `+ ""`) – blgt Jul 28 '16 at 08:41
  • `the second option` - so you're only interested in IE? – Jaromanda X Jul 28 '16 at 08:41
  • http://stackoverflow.com/questions/4176923/html-of-selected-text/4177234#4177234 show how to get the html out of the selection object. – jHilscher Jul 28 '16 at 09:03
  • @JaromandaX , not really, it just seemed shorter and according to @mems ' comment it works on other browsers as well if you replace `window.clipboardData` with `event.clipboardData`. – Ariel Jul 28 '16 at 11:22

2 Answers2

0

You need to cast it to a string (getSelection() returns a Selection object). So either append a "" or cast it to a string with .toString() before executing the .replace()

so in your case, the code should be like this:

copytext = (window.getSelection() + "").replace(/some pattern/, 'replace value');

or

copytext = (window.getSelection().toString()).replace(/some pattern/, 'replace value');

Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

Jorrex
  • 1,503
  • 3
  • 13
  • 32
  • The problem with this is that I loose all the formatting in the content being copied. To be more explicit, I have a contenteditable div with rich text inside of it, including links etc. If I use `toString` I loose all of that. – Ariel Jul 28 '16 at 10:57
0

selection is a object so if you want to run the replace function on its text then use following.

window.getSelection().anchorNode.data.replace(/some pattern/, 'replace value');

I hope this will help

localhost
  • 483
  • 4
  • 10
  • This does work but only for the first element in my selection. My selection has multiple nodes, I suppose I could loop through them and apply `replace` to each? Like I said I'm not familiar with the structure of javascript selections so I'm having a hard time trying to do even something as simple as looping though the nodes, so assistance is appreciated. – Ariel Jul 28 '16 at 10:49
  • can you post your complete code here?. it would be great if you can create jsfiddle for this. – localhost Jul 28 '16 at 11:12