How to programmatically generate code to display in jsfiddle.net?

Viewed 434

My goal is to generate dynamic code based on the user input, and then have that code programmatically put to jsfiddle so the user can preview the generated code as well as the running result.

I've tried using the jsfiddler library which utilizes phantomjs for the job, but the only output I am getting is:

Failed to create JS Fiddle Failed with code: Error: spawn /Users/***/test/untitled folder/untitled folder/jsfiddler/node_modules/phantomjs/bin/phantomjs ENOENT

Here is my javascript code:

var jsfiddler = require("./jsfiddler");

let options = {
  html: "<body>This is an example jsfiddle</body>",
  js: 'console.log("Example JS!")',
  css: "body { font-family: Helvetica, Arial, sans-serif; }",
  link: "http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css",
  script: "http://code.jquery.com/jquery-1.11.1.min.js",
  title: "JSFiddler Sample",
  description: "Sample page created via JSFiddler api module",
  normalize_css: true,
  wrap: "b" /* body */,
  dtd: 4,
};
jsfiddler.create(options, function (err, fiddlerUrl) {
  if (err) return console.error("Failed to create JS Fiddle", err);
  console.log("Created JS Fiddle: ", fiddlerUrl);
});

It will also be super helpful if there's any other way to achieve the goal, thanks a ton in advance!

2 Answers

It's possible to display a fiddle that's not saved in JSFiddle, using a POST.

API Documentation

https://docs.jsfiddle.net/api/display-a-fiddle-from-post

Example using form

https://jsfiddle.net/3Lwgk4c5/

Well, the library is using the older version of phantomjs which is no longer configurable. Also, the installation of the package is no longer working. So, you need to figure out some other solution.

Related