I am trying to copy some text from a page without copying the entire page url for the inline (#links).
Here's my example fiddle: https://jsfiddle.net/adxhoz5v/10/
When you press the copy button, it copies the entire link and the clipboard, when pasted into another rich text editor, looks like this:
<p>But it is a fascinating CSS trick and the web is a big place with an unknowable magnitude of situations where sometimes weird solutions are needed.</p>
<h3><a id="technique-1-directional-trickery" class="aal_anchor" href="https://fiddle.jshell.net/_display/?editor_console=true#technique-1-directional-trickery" aria-hidden="true"></a>Technique #1: Directional Trickery</h3>
<p>The trick here is to have the scrolling parent element use <code>direction: rtl</code> (or the opposite of whatever your primary direction is), and have the inside of the scrolling element switch back to whatever your normal is.</p>
Notice how it copies the inline link with url of the jsfiddle page (page url):
href="https://fiddle.jshell.net/_display/?editor_console=true#technique-1-directional-trickery"
My question is, is there a way to prevent this default behaviour so that I am only left with the inline links in my clipboard. Like so:
href="#technique-1-directional-trickery"
To reproduce, please use your desktop or any online rich text editor like: https://html-online.com/editor
The problem is that it will always add the baseURI of the document for rich text output. Since this property is automatically populated and read-only there's no way to avoid this.
Luckily there's a workaround though. You can utilize the ClipboardEvent interface to obtain the data of your DIVs
.outerHTML
property instead of relying on the ClipboardJS library. Rich text is preserved by setting the format of the copied text totext/html
.Here's an example based on your fiddle: