Add support for text direction in PastePad

It worked, thanks!
Didn’t know I can use html/head/body tags in PastePad, plus I always tried dir="rtl", not lang="he".
The downside with this interim solution is you can’t preview (i.e. click “Source” again) before clicking “Push to Kindle”.

But download aside, good news - and I’ve even opened my own username for this - I’ve developed a code that adds Hebrew declaration to your editor when needed!

The involved steps:

  1. Attach a click event to your submit buttons (it took me hours to realize I can’t attach a submit event because it clashes with the existing submit events).
    So change
    <input type="submit" name="action" class="btn large ppxtralarge" value="...">
    to
    <input onclick="add_heb_support()" type="submit" name="action" class="btn large ppxtralarge" value="...">
  2. Upon clicking the submit buttons, detect if Hebrew was used. If not, do nothing special. If yes, add the prefix. In order to do that, add the following script to PastePad:
<script>
     function add_heb_support() {
        var prefix='<html lang="he" />', source_value = CKEDITOR.instances.body.getData(), source = '#cke_contents_body>textarea', source_btn = '#cke_34';
        if (document.querySelectorAll(source).length==0) // Open the Source editor
           document.querySelector(source_btn).click();
        if(source_value.indexOf(prefix) == -1 && contains_heb(source_value)) // Add Hebrew support if needed
           CKEDITOR.instances.body.setData(prefix + source_value);
     }

    function contains_heb(str) {
        return (/[\u0590-\u05FF]/).test(str);
    }
</script>

To demonstrate this, I’ve even prepared a Bookmarklet (you love those, right?) that anyone can use right now: