How to search/replace text being copied before sending to clipboard using Javascript/jQuery? -
i managed add text what's being copied using code in this answer(the second option). however, when change string concatenation , add call replace
, error "replace not function".
copytext = window.getselection().replace(/some pattern/, 'replace value'); // fails
the "selection" object seems complex , can't find text inside it. call tostring
on that's not option because i'm copying html contenteditable div , need preserve formatting.
i'm trying because have relative links in div's content , they're being converted absolute links in copied text reason. happens when accessing my demo rawgit. locally, works normally.
any ideas on how accomplish this?
update
here's jsfiddle current setup: https://jsfiddle.net/8kx8v8pb/
you need cast string (getselection()
returns selection object). either append "" or cast string .tostring()
before executing .replace()
so in case, code should 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
Comments
Post a Comment