# 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:** [June 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:** 4\
**Page:** 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:** [June 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);
  }
}());

```

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [June 13, 2017, 1:19am UTC](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393/2 "2017-06-13T01:19:33Z")

</div>

Cool, could you add a screenshot of it doing its stuff here?

---

<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:** [June 13, 2017, 2:19am UTC](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393/3 "2017-06-13T02:19:55Z")

</div>

okie-dokey!

some `test` content _follows_ **below** :

> 'Twas brillig, and the slithy toves  
> Did gyre and gimble in the wabe:  
> All mimsy were the borogoves,  
> And the mome raths outgrabe.

 ![](https://global.discourse-cdn.com/meta/original/3X/7/f/7f43da4bd1f01499396655814f0093e549fd7010.jpg)

```
"Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"

```

---

<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:** [June 13, 2017, 2:23am UTC](https://meta.discourse.org/t/bookmarklet-to-extract-the-contents-of-the-topmost-visible-post/64393/4 "2017-06-13T02:23:48Z")

</div>

…which results in:

 ![](https://global.discourse-cdn.com/meta/original/3X/d/d/ddecb6d2c72e50d72dcb9dbde2b7cafd0f290f20.png)

and I will have to use my work PC later to post a screenshot of Outlook, if that’s what you meant.
