---
title: 'Bookmarklet: Clean highlighted code for copying'
date: 2015-12-01 12:14
slug: bookmarklet-clean-highlighted-code-for-copying
categories:
- Code
tags:
- code
- javascript
- bookmarklet
---

I made a little bookmarklet this morning for cleaning up highlighted code blocks in web pages for copying into my notes. You just click it on any web page and all `pre>code` blocks and `table.highlight` elements will be turned into plain text.

![](https://brettterpstra.com/uploads/2015/12/codecleaner_sm.gif)

Once code blocks are cleaned, double-clicking anywhere in the block will select all the contents of the field.

If this would be of use to you, you can drag the bookmarklet below to your menu bar and try it out. It doesn't currently work on GitHub in this form, but you can create a local bookmarklet from the source if needed. GitHub, of course, offers raw source easily, so it's not really a concern.

[CodeCleaner bookmarklet](https://brettterpstra.com/bookmarklets/codecleaner.js)

It's not heavily tested, so let me know if you see bugs. It also only handles the most common code highlighters, so there are more than likely code blocks it will fail on, but it serves my needs as is.

**Side notes:** By default it will add code fences (triple backticks), and try to detect the language. If a language is found in the `pre` or `table` element's classes or `data-language` attribute (falling back to those of the parent `div` or `code` tag), it will be added to the opening fence. The [source script](/bookmarklets/codecleaner.js) allows these to be overridden with an options object passed to the main function (e.g. `CC.cleaner({fence: false, detectLang: false})`, but I don't currently have that implemented in the bookmarklet version.

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
