Vivek Pradhan
Vivek Pradhan

Reputation: 4847

How to dynamically create javascript file in Browser

I recently started exploring Browserify for bundling Node modules and using them in the browser. It's neat and works great, however I want an improvement in the work flow. In my use case, I have a script.js file that requires node modules like Cylon etc.

For brevity, script.js looks something like:

"use strict";

var Cylon = require('cylon'); 
Cylon.robot({
    name: "BrowserBot",

    connections: {
      arduino: { adaptor: 'firmata', port: '/dev/tty.usbmodem1411' }
    },

    devices: {
      led: { driver: 'led', pin: 8 }
    },

    work: function(my) {
      Cylon.Logger.info("Hi, my name is " + my.name)

      every((2).seconds(), function() {
        Cylon.Logger.info("Toggling the LED");
        my.led.toggle();
      });
    }
  });

Cylon.start();

I was looking at the bundle.js file that browserify generates and i could find the exact code block mentioned above, and I think a node process is started with this code and some bindings. I want the script.js file to be dynamic to allow the user to use a different pin on an LED or any other small change for that matter. Since I am not changing any dependencies for this file, I should be just able to replace that block in bundle.js with the new contents of the script.js file as other modules are already loaded and bundled in the bunndle.js right?

I want to know if this is possible in a browser setting. Chrome Apps allow file Storage, so it is possible for me to generate bundle.js dynamically after initial creation where I just plug-in the contents of script.js and load bundle.js in an HTML file? How do I go about this?

While the question is not specific to Cylon, I am still adding it as a tag for my specific usecase.

Upvotes: 1

Views: 1163

Answers (1)

adjuremods
adjuremods

Reputation: 2998

All the .js files should be specified in the Apps manifest.json. I don't think you can edit items from the app's folder (even when accessing it thru file storage)

Upvotes: 0

Related Questions