Unable to inject storage format

javascript
confluence-developers

#1

I am using this script here:

var newBodyText = '<div class=\"carousel\"><ac:link ac:anchor=\"Anchor Link\"><ac:link-body><ac:image><ri:url ri:value="http://confluence.atlassian.com/
images/logo/confluence_48_trans.png" /></ac:image></ac:link-body></ac:link></div>';
jQuery.ajax({
    url: contextPath + "/rest/api/content/" + AJS.params.pageId + "?expand=body.storage,version,ancestors",
    success: function(response) {
        response.body.storage.value = newBodyText;
        response.version.number += 1;
        jQuery.ajax({
            contentType: 'application/json',
            type: 'PUT',
            url: contextPath + "/rest/api/content/" + AJS.params.pageId,
            data: JSON.stringify(response),
            success: function(response) {
                console.log(response);
                console.log("Success!");
            },
            error: function(response) {
                console.log(response);
                console.log("Error!");
            }
        });
    }
});

But it’s throwing an error every time, and I can’t find out where. The image link is exactly as described in the Confluence Storage Document here. I also tried not escaping the escaping the quotes:

var newBodyText = '<div class="carousel"><ac:link ac:anchor="Anchor Link"><ac:link-body><ac:image><ri:url ri:value="http://confluence.atlassian.com/
images/logo/confluence_48_trans.png" /></ac:image></ac:link-body></ac:link></div>';
jQuery.ajax({
    url: contextPath + "/rest/api/content/" + AJS.params.pageId + "?expand=body.storage,version,ancestors",
    success: function(response) {
        response.body.storage.value = newBodyText;
        response.version.number += 1;
        jQuery.ajax({
            contentType: 'application/json',
            type: 'PUT',
            url: contextPath + "/rest/api/content/" + AJS.params.pageId,
            data: JSON.stringify(response),
            success: function(response) {
                console.log(response);
                console.log("Success!");
            },
            error: function(response) {
                console.log(response);
                console.log("Error!");
            }
        });
    }
});

That one errors out, too. This script always works, so I’m not sure what has happened with this iteration specifically. Any help is appreciated! Thanks!


#2

Did You have an error response? You said that the script worked, what is different from working and current? Try to simplify newBodyText.


#3

In the unescaped version, I receive the following error: SyntaxError: '' string literal contains an unescaped line break’ with the text debugger eval code:1:148. In the escaped version, I receive the same error SyntaxError: '' string literal contains an unescaped line break followed by debugger eval code:1:153. I have gone through this code a thousand times–and this is a code block directly from Atlassian in their own storage format. It should work.


#4

In first codeblock I not see escape for this

value="http and 48_trans.png" /&gt;&lt;

Try escaping also them.


#6

You’re right @andrewdvizhok ! I’ll try that now!

UPDATE: It worked! Thanks a ton!