Tumblr – Tumblr Appends JavaScript at End of Post

tumblrtumblr-themes

Can you identify what the following javascript does? And better, where does it come from?

Frequently, as I make a text post on Tumblr, this gets appended at the end. It doesn't appear in the WYSIWYG editor at all, but shows up as series of small gray boxes at the end of post when published, and shows up if I click "HTML" in the editor – the following code appears below the actual text of the post in the editable raw source of the post.

Is this some Firefox extension messing the editor up? Some virus? A Tumblr bug? Some kind of feature I don't know about? Normally I always edit the code and strip this manually, but I'd prefer to know how it got there in the first place…

    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function decodeContentTags(str) {
        var div = document.createElement("div");
        div.innerHTML = str;
        return div.innerHTML;
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function newTagLinks(postID, tags) {
        tag = tags.split(",");
        postTags = "post_tags_" + postID;
        $(postTags).innerHTML = "";
        for (i = 0; i < tag.length; i++) {
        var addLabel = document.createElement("a");
        addLabel.setAttribute("href", "/tagged/" + tag[i]);
        addLabel.innerHTML = "#" + tag[i];
        $(postTags).appendChild(addLabel);
        }
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function displayTags(postID) {
        existingTags = "";
        hasTags = true;
        if (String($("post_tags_" + postID)) != "null") {
        for (i = 0; i < $("post_tags_" + postID).childElements().length; i++) {
            existingTags += $("post_tags_" + postID).down(i).innerHTML.substr(1) + ",";
        }
        }
        existingTags = existingTags.slice(0, -1);
        tags = prompt("Tags", existingTags);
        if (tags != null) {
        $("tagEdit-" + postID).innerHTML = "working...";
        $("tagEdit-" + postID).removeAttribute("onClick");
        editTags(postID, tags);
        }
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function editTags(postID, tags) {
        var editPost = "/edit/" + postID;
        new Ajax.Request(editPost, {onSuccess: function (response) {r = response.responseText;dataStart = r.indexOf("<form");dataEnd = r.indexOf("");html = r.substring(dataStart, dataEnd);var inputs = html.match(/<input[^>]*>/g);var textareas = html.match(/<textarea[^>]*>[^<]*<\/textarea>/g);var params = {};var name;for (i = 0; i < inputs.length; i++) {name = inputs[i].match(/name="([^"]*)"/);if (name) {if (String(inputs[i].match(/value="([^"]*)"/)) == "null") {params[name[1]] = null;} else {params[name[1]] = decodeContentTags(inputs[i].match(/value="([^"]*)"/)[1]).unescapeHTML();}}}for (i = 0; i < textareas.length; i++) {name = textareas[i].match(/name="([^"]*)"/);if (name && !/id="custom_tweet"/.test(textareas[i])) {params[name[1]] = decodeContentTags(textareas[i].replace(/<(?!\s*\/?\s*p\b)[^>]*>/gi, "")).unescapeHTML();}}params['post[tags]'] = tags;delete params['post[promotion_type]'];delete params.preview_post;new Ajax.Request(editPost, {method: "post", parameters: params, onSuccess: function (transport) {newTagLinks(postID, tags);$("tagEdit-" + postID).innerHTML = "done!";$("tagEdit-" + postID).setAttribute("onClick", "displayTags(" + postID + ")");setTimeout(function () {$("tagEdit-" + postID).innerHTML = "edit tags";}, 1500);}, onFailure: function () {$("tagEdit-" + postID).innerHTML = "x_x";$("tagEdit-" + postID).setAttribute("title", "Please reload!");}});}});
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function addTagEdit(postControl, postID) {
        var addLabel = document.createElement("a");
        addLabel.setAttribute("id", "tagEdit-" + postID);
        addLabel.setAttribute("style", "cursor: pointer;");
        addLabel.setAttribute("onClick", "displayTags(" + postID + ")");
        addLabel.innerHTML = "edit tags";
        postControl.appendChild(addLabel);
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    function getPostsTags(startNum) {
        var allPosts = $$("li.is_mine.:not(li.new_post)");
        for (var i = startNum; i < allPosts.length; i++) {
        postID = allPosts[i].getAttribute("id").substr(5);
        addTagEdit(allPosts[i].down().next(), postID);
        }
        return allPosts.length + startNum;
    }
    // ]]]]><![CDATA[>
    // ]]></script>
    <script type="text/javascript">// <![CDATA[
    // <![CDATA[
    (function mainTags() {
        var startNum = 0;
        var pageLength = $$("li.is_mine.:not(li.new_post)").length;
        startNum = getPostsTags(startNum);
        Ajax.Responders.register({onLoaded: function () {checkPage = setInterval(function () {var newLength = $$("li.is_mine.:not(li.new_post)").length;if (pageLength < newLength) {getPostsTags(pageLength);pageLength = newLength;}window.clearInterval(checkPage);return;}, 5000);}});
    })();
    // ]]]]><![CDATA[>
    // ]]></script>

Best Answer

The piece of code contains different functions, as you can see. According to their names and what they do, it looks like they are simply for generating the grey boxes which would be tags. Other functions are i.e. editing the tag(s).