# Bookmarklet to extract the contents of the topmost visible post

**URL:** <https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393>\
**Category:** Sysadmins\
**Tags:** reference\
**Created:** [12 ביוני,‏ 2017,‏ 11:26pm UTC](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393 "2017-06-12T23:26:17Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![jmsachs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jmsachs/32/107682_2.png) [@jmsachs](https://meta.discourse.org/u/jmsachs)\
**Post date:** [12 ביוני,‏ 2017,‏ 11:26pm UTC](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393/1 "2017-06-12T23:26:17Z")

</div>

Just wanted to share my solution to major frustration in copying a Discourse post into an Outlook 2007 message (cue numerous [sad trombone](https://www.sadtrombone.com/) noises). Perhaps it has other applications. This essentially does the following:

- figures out the post id
- finds the relevant `<article>` node and clones it
- removes the enclosing `<a>` link from each image
- opens a new document (`about:blank`) with Calibri as the font-family
- takes the cloned node and appends it to the new document

If I use it in Chrome I can then copy the resulting document, then Paste Special → as HTML Format into Outlook 2007 and it has a reasonable similarity to the original Discourse post.

Firefox creates the new `about:blank` page correctly but doesn’t seem to get data into the Windows Clipboard in a way that Outlook understands formatting.

enjoy…

```plaintext
javascript:(function() {
  var url = window.location.href;
  var reply_id = window.location.pathname.match(/\/t\/[^/]*\/([0-9]+)(?:\/([0-9]+))?$/);
  if (reply_id)
  {
    reply_id = reply_id[2] || "1";
	var original_contents = document.querySelector('article#post_'+reply_id+' .topic-body .contents .cooked');
	
	var contents = original_contents.cloneNode(true);
	function unwrap(element) {
	  var fragment = document.createDocumentFragment();
      while (element.firstChild) {
        fragment.appendChild(element.firstChild);
      }
      element.parentNode.replaceChild(fragment, element);
	}
	contents.querySelectorAll('img').forEach(function(img){
	  unwrap(img.parentElement); /* remove containing <a> link */
	});

	var newdoc = window.open().document;
	newdoc.write("<!DOCTYPE html><html><head><style type='text/css'>.cooked { font-family: Calibri, sans-serif; }</style></head><body></body></html>");
	newdoc.close();
    newdoc.adoptNode(contents);	
	newdoc.body.appendChild(contents);
  }
}());

```

---

_[View the full topic](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393)._
