2

What I need is to save the content (html) of firepad periodically in a database.

I tried to save that information with a cron service which call a page that with an ajax request send the content to a second php page that do the job.

If I call manually this url all work, but if the url is called by the cron service javascript doesn't execute.

How can I do this?

Andrew Lee
  • 10,127
  • 3
  • 46
  • 40
appersiano
  • 2,670
  • 22
  • 42

1 Answers1

4

Unfortunately there isn't a direct way to achieve this right now. You had a nice idea with the cron job, but since javascript has to execute, it won't work unless you use a headless browser like phantomjs to load the page. You might give that a try.

Alternatively, one of our firepad users, Clément Wehrung solved this with some tricky node code that runs firepad.js inside of node from a backend server. I haven't played with the code, but it may be a good starting point.

node-firepad.js:

/* jshint evil: true */

var jsdom = require('jsdom');
var fs = require('fs');

var Firepad = {};

/**
* Load Dependencies Sources
*/

// HACK: Make Firepad expose FirebaseAdapter
Firepad.FirepadSource = fs.readFileSync(__dirname+'/firepad.js').toString().replace('return firepad.Firepad;', 'firepad.Firepad.FirebaseAdapter = firepad.FirebaseAdapter; return firepad.Firepad;').replace('= global.CodeMirror', '= window.CodeMirror || global.CodeMirror');
Firepad.CodeMirrorSource = fs.readFileSync(__dirname+'/codemirror.js');

/**
* Node Firepad Proxy Loader
*/

Firepad.load = function(ref, callback) {

 jsdom.env('<head></head><body><div id="firepad"></div></body>', function (errors, window) {

   /**
    * Load CodeMirror
    */

   var document = document || window.document;
   var navigator = navigator || { userAgent:'', platform:'' };
   eval(Firepad.CodeMirrorSource+'');
   var CodeMirror = window.CodeMirror;

   /**
    * Load Firepad
    */

   eval(Firepad.FirepadSource+'');
   // HACK: disable cursor
   Firepad.FirebaseAdapter.prototype.sendCursor = function(){};
   Firepad.FirebaseAdapter.prototype.setColor = function(){};

   /**
    * Launch
    */

   var codeMirror_ = CodeMirror(window.document.getElementById('firepad'), { lineWrapping: true }, window);

   var firepad_ = Firepad.fromCodeMirror(ref, codeMirror_,
       { richTextShortcuts: true, richTextToolbar: true });

   if (callback) callback(firepad_, window, errors, document);
 });
};

module.exports = Firepad;

Which you can then use something like this:

var FirepadManager = require(__dirname+’/node-firepad’);

FirepadManager.load(firebaseRef, function(firepad, window, errors, document) {
  // The callback is called as soon as the Firepad instance is created.
  // Hence, you can perform stuff like adding your entities support at this point...
  addEntitiesToFirepad(firepad, document);
  firepad.on('ready', function() {
    if (callback) callback(firepad, window, errors, document);
  });
});

If you go this route and have troubles, let me know (michael@firebase.com). I may be able to help. Good luck!

Michael Lehenbauer
  • 16,229
  • 1
  • 57
  • 59
  • Thanks for the quick answer, I think that phantomjs is the best solution for my problem, but I have this service on a shared hosting with no phatomjs support. About the second solution proposed is too intricate. It would be nice to get the current content throught a simple json feed ... I'll wait to this feature. – appersiano May 20 '14 at 00:20