Execute a macro from javascript in a user macro

Mike Pearson January 26, 2017

I'm trying to write a User Macro in which an HTML button triggers a javascript function, which in turn executes a Confluence macro. 

I can get a macro (e.g. "I Like Cheese!") to execute just fine if placed directly in the HTML.  For example, the following works fine:

<!DOCTYPE html>
<html><head></head>
<body>
<p><div><ac:structured-macro ac:name="cheese"/></div></p>
</body></html>

 

I can also successfully inject HTML with a button press.  For example, the following works fine:

<!DOCTYPE html>
<html><head></head>
<body>

<button onclick="runScript()">Insert HTML From Script</button>
<p><div id="result"></div></p>
 
<script type="text/javascript">
function runScript() {
      document.getElementById('result').innerHTML = '<b>Some bold text.</b>'
 };
</script>
 
</body></html>

 

BUT, when I try to inject HTML for the macro, it DOESN'T work.  The following does not work:

<!DOCTYPE html>
<html><head></head>
<body>

<button onclick="runScript()">Execute Macro From Script</button>
<p><div id="result"></div></p>
 
<script type="text/javascript">
function runScript() {
      document.getElementById('result').innerHTML = '<ac:structured-macro ac:name="cheese"/>'
 };
</script>
 
</body></html>

 

Can anyone spot why the first two cases work, but the 3rd does not?

Any feedback/advice much appreciated!

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Mike Pearson January 27, 2017

Thanks Volodymyr.  

I think I managed to get it working by injecting a call to the macro using $action.getHelper().renderConfluenceMacro(), rather than the HTML. The following appears to work:

<!DOCTYPE html>
<html><head></head>
<body>

<button onclick="runScript()">Execute Macro From Script</button>
<p><div id="result"></div></p>

<script type="text/javascript">
function runScript() {
      document.getElementById('result').innerHTML = '$action.getHelper().renderConfluenceMacro("{cheese}")'
 };
</script>
</body></html>


QUESTION:

Is there a problem with using $action.getHelper().renderConfluenceMacro() to call macros from within other macros, instead of using the <ac:structured-macro> markup?

0 votes
Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 26, 2017

Mac-roses are rendered on server so adding a macro tag via JavaScript has no effect.

TAGS
AUG Leaders

Atlassian Community Events