Hi everyone,
how can you load the content from another confluence page?
I am trying this, which is not working:
var resFunction = function(data) { alert(data); }; var text =AJS.$.get("http://localhost:1990/confluence/plugins/viewsource/viewpagesrc.action?pageId=983079", resFunction);
Produces error:
Error occurred during template rendering: Encountered ")" at /decorators/main.vmd[line 26, column 19] Was expecting one of: "(" ... ... ... ... "##" ... "\\\\" ... "\\" ... ... "*#" ... "*#" ... ... ... ... ... ... ... ... ... ... "{" ... "}" ... . Contact your administrator for assistance.
Any advices?
Thanks!
Community moderators have prevented the ability to post new answers.
Start by setting the return value to a string, and then use some regex to extract the contents between the <body> tags. This page shows a simple example:
http://stackoverflow.com/questions/356340/regular-expression-to-extract-html-body-content
thank you :)
Even though the code is not nice it might help anyone some time
<script type="text/javascript"> AJS.$(document).ready(function(){ var regexPageId = new RegExp("#template:([0-9]*)#"); var oldContent = tinymce.activeEditor.getContent(); if(regexPageId .test(oldContent)) { var newPageId = regexPageId.exec(oldContent); var newPageId1 = newPageId[1]; var myContextPath = AJS.contextPath(); var templateUrl = location.protocol+"//"+location.host+""+myContextPath+"/plugins/viewsource/viewpagesrc.action?pageId="+newPageId1; jQuery.get(templateUrl, function(data){ var mydata = jQuery.trim(data); mydata = mydata.replace(/[\n\r\t]/g,''); var regex = new RegExp("<body([^>]*)>(.*)</body>"); var newBody = regex.exec(mydata); var newBody2 = newBody[2]; jQuery.trim(newBody2); newBody2.replace(/^(\\s*)/,''); tinymce.activeEditor.setContent(newBody2); }); } }); </script>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Rather than scraping the contents of the view page source action, why don't you just use the REST or JSON-RPC API to get the contents of the desired page in a format that is easy to consume?
AJS.toInit(function() { var pageId = 0; // TODO: Populate this value. // Build the URL to the REST API. var url = AJS.params.contextPath + "/rest/prototype/1/content/" + pageId + ".json"; function useContent(page) { AJS.log("Here is the page XHTML - " + page.body.value); }; AJS.$.get(url, useContent); });
EDIT: Here's the full REST API documentation and the SOAP/XML-RPC/JSON-RPC API.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
it is working, and way easier. Thanks! However, there are two issues:
1) Line 12: AJS.$.get() is not working for me, I have to use jQuery.get()
2) page.body.value, does not include css classes e.g. for a table. Do you know how to fix this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How are you loading your javascript into the page? If you're doing it via a plugin, make sure your web resource has a dependency on confluence.web.resources:ajs to make sure that AJS gets loaded first.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh, I'm an idiot. I've realised why you were scraping the view source page instead of using the API :)
Scraping the view source page gives you the rendered edit HTML for the page. Calling the REST API gives you the un-rendered storage XHTML for the page. Can you un-accept this answer since the original approach you tried is probably better!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, I try to get the original approach to work with Confluence 4.1.3, but I get a "tinymce is undefined"
while page with user macro is loaded. Anyone had this experience?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Johannes,
I think you can use jQuery Load to get content from another page and show then in you current page.
It will work like this:
$("#myDiv").load("anotherpage.php", function (response, status, xhr) { if (status == "error") $("#myDiv").html("Error: " + xhr.status + ": " + xhr.statusText); }); });
In the above code the jQuery Load will show all HTML content from anotherpage.php in myDiv.
If you see the jQuery Load method uses, then you will find it can be used to grab just a section of the HTML of another page too.
See this code now:
$("#myDiv").load("anotherpage.php #someDiv");
In the above line of code, i am just grabbing all the HTML from someDiv only and showing it inside myDiv.
I think here you can easily use jQuery Load method.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm guessing that you can't pass just any old raw characters into the alert function. You probably have to escape special characters, like single and double quotes and maybe others. One idea is to wrap the string in the escape() method before calling alert():
alert(escape(data));
hth,
matt
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks matt. The problem occurs from the javascript code in Line 5. I figured out
jQuery.get("url", resFunction)
returns the content, even though I have no idea how to get body html from that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why not just use the Include macro? It will save you a lot of headaches :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I need the content from another page in the editor and not only an include macro.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.