1 Reply Latest reply on Feb 17, 2015 8:25 AM by mattdickens

    Problem creating content with anchor links via the api

    mattdickens

      So I've got a need to allow users to create a backup of the contents of a formatted text widget. The code creates the backup just fine but I find the anchor link hrefs become corrupted in that the protocol is stripped off the front and the full jive environment's domain is pre-pended to each href.

       

      I've just put this script together to illustrate the problem except this JS will create a copy of whatever jive document you have open when you execute it. It creates a hidden copy of the document, visible only to you.

       

      var title = $j('.jive-content h1').text() +" ("+(new Date()).toString().split(' ').splice(1,4).join(' ')+")";
      var content = $j(".jive-rendered-content").html().replace(/"/g, '"').replace(/'/g, ''');
      $j.ajax({
        type: 'POST',
        url: "/api/core/v3/contents",
        dataType: "json",
        data: '{"content":{"type": "text/html","text": "'+content+'"},"subject": "'+title+'","type": "document","visibility": "hidden"}',
        beforeSend: function (xhr) {xhr.setRequestHeader('X-J-Token', parent._jive_auth_token);},
        headers: {'Accept': 'application/json','Content-Type': 'application/json'},
        success: function (data) {alert("A copy of this document has been created in your personal document space. It is only visible to you.")},
        error: function (xhr, status, err) {console.log(status, err);}
      });
      
      
      

       

      I suspect the problem is due to the way I'm replacing the double and single quotes with their escaped versions but I don't see how else to pass the complete html content in the ajax call. I've got this annoying feeling I might be missing something stupid.

       

      To try it, simply open a simple jive document containing at least one anchor link, open your developer toolbar console and paste the above code in. You should then have a duplicate document of your document created in your personal space.

       

      Below shows the html of my original document followed by the html in the copy. We can see that the html5 custom data attributes are being stripped from the anchors but the altered hrefs is strange.

       

      original.PNG

       

      the Copy:

       

      copy.PNG

       

       

      Can anyone please tell me how I can avoid this.

        • Re: Problem creating content with anchor links via the api
          mattdickens

          For anyone who's curious, it was something stupid! Stringifying the content and excluding the extra quotes around it in the JSON data string solved the problem.

           

          var title = $j('.jive-content h1').text() +"("+(new Date()).toString().split(' ').splice(1,4).join(' ')+")";
          var content = JSON.stringify($j(".jive-rendered-content").html());
          $j.ajax({
            type: 'POST',
            url: "/api/core/v3/contents",
            dataType: "json",
            data: '{"content":{"type": "text/html","text": '+content+'},"subject": "'+title+'","type": "document","visibility": "hidden"}',
            beforeSend: function (xhr) {xhr.setRequestHeader('X-J-Token', parent._jive_auth_token);},
            headers: {'Accept': 'application/json','Content-Type': 'application/json'},
            success: function (data) {alert("A copy of this document has been created in your personal document space. It is only visible to you.")},
            error: function (xhr, status, err) {console.log(status, err);}
          });
          
          

           

          I may well expand on this to create a little utility that can allow users to specify the document or collection of documents in a container they wish to duplicate and the target container they wish them to be added to. Useful if you have a set of template documents that you want users to be able to copy and personalise.