carene_waterman: An image of the Carina Nebula (Default)
[personal profile] carene_waterman
My second AO3 bookmarklet (StripEmpties) is a really simple tool to take out the extra blank paragraphs the AO3 parser puts in sometimes.

It was easy to code, fast to test, and I made it a little less error-prone by making sure the paragraphs hidden are only inside the chapters div.


Until I tried to post it to my GitHub page. The bookmarklet there is a link element with the javascript as the href, because a bookmarklet is really a link in your browser with the code as its URL. So you have to escape a bunch of characters that confuse the browser when it tries to write that html.

Which was all very simple until I had to get the code to compare the innerHTML of a paragraph to  . So I had to get my href and then the URL to not convert that to an actual unbreaking space.

You'd expect to be able to simply escape the ampersand with the HTML code and all would work out. No, somewhere either in GitHubs page creation system or in the browser that got converted to an actual unbreaking space.

I finally, after much trial and frustrating error, figured out that I needed to end up with the URL escape code for ampersand in my bookmarklet. so #26nbsp;. But just putting that in in the HTML for the href of the link ended up as an actual unbreaking space.

I had to fool the web page (or the browser, still not sure which) by putting in the HTML escape for the #, %, then the number 26 and so on, so: %26nbsp;, which the href and then the URL saw as #26nbsp; and could successfully perform the comparison with  .

Whew. That's a lot of escaping.

When I was first writing the code, I came across the advice that often what you really want is the textContent not the innerHTML, which seems like good advice, because the inner HTML includes any child elements that exist.

But I could not figure out how to test for an textContent that was an unbreaking space. I finally stumbled on it after I had posted the bookmarklet with the test for innerHTML.

You test textContent using the escaped character code in the hex, octal or unicode form.

So, do I change this? Maybe next time.

Date: 2016-01-22 04:04 am (UTC)
aadikah: Picture of a lit rosebud candle. (Default)
From: [personal profile] aadikah
Thank you, thank you, thank you for this bookmarklet! It was something that was desperately needed, but I never dreamed to look for it, or to try to code something similar myself. Your hard work and metaphorical (I hope) hair-pulling are so very much appreciated! <3

(Apologies for being weird and tracking down this post, but I really wanted to express my gratitude. You rock!)


carene_waterman: An image of the Carina Nebula (Default)

July 2014

2021 22 2324 2526

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 25th, 2017 06:54 pm
Powered by Dreamwidth Studios