Include javascript files (solution)

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

Include javascript files (solution)

Post by ravi_npatty » Sat Dec 26, 2015 11:36 am

Dear iMacros users,

I was missing the include support for java script files. And some of you might also felt the same way. I worked out a solution for this which may not be very professional, but atleast working.

A brief Intro about how this works:

1. Keep all your common functions and global variable definitions in separate .js file (Please add //<<START>> at the beginning of each .js file you want to include. Please note that //<<START>> should be the first character of the line and below that you can add your function definitions.
2. Copy paste the first two lines from the include_example.js in to your new script where you want to import your other files.
3. Call include(fname) to include the file you want to import. Call as many times as you need. fname is the name of the script to import
4. Call include(cur_name,true) to include the contents of your new script. cur_name is the script file name of current script. true is to indicate that this is the last include call and instruct to run the script.
5. Add your script code and declare a function called main() but do not call this function from this code. This function will be called by the script.

//Example code in main file
//###########################################################################################
if(typeof(script_dir)=="undefined") //--- This is just example. Please copy paste from attachement.
if(typeof(include)!=typeof(Function) //--- This is just example. Please copy paste from attachement.

include("common_scripts.js");
include("include_example.js",true);

//<<START>>
function main()
{
alert("inside main function");
var ss=test_function(458);
alert(ss);
}

//##########################################################################################

What happens in background:
* Create a temporary file "tmpjs.js" in macros folder and all contents of the file called via include is appended to this temporary file.
* using tag url goto this temporary .js file is run.

Sorry if I could not explain properly. I hope this will be useful for many, so I share it here.
Still if native support for include is provided by iMacros, it would be better. Till then this can be used. This also provided a chance for somebody can improve up on this.


Thanks.
Attachments
include_exaple.zip
iMacros javascript include example
(1.7 KiB) Downloaded 92 times
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

Re: Include javascript files (solution)

Post by ravi_npatty » Sat Dec 26, 2015 11:39 am

Dear iMacros users,

In the above solution, if I can get the currect script file name and path on the fly, it can be more straight forward. I am passing this manually at present. If somebody has solution for this it will be useful.

Thanks.
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

Re: Include javascript files (solution)

Post by ravi_npatty » Mon Dec 28, 2015 10:19 am

I wonder how I was not aware of eval function. It seems more straght forward to include code to the current script than copying them into a temporary file and running it.

Thanks to blueX324 http://forum.imacros.net/viewtopic.php? ... xts#p45258

Here is a simple solution (slightly modified) taken from many useful functions provided in exts.rar from the above thread.

Example code:
//###############################################################################################
if(typeof(include)!=typeof(Function)){function include(filename){var script_dir=imns.Pref.getFilePref("defsavepath").path;if(script_dir.indexOf("\\")>=0) script_dir+="\\";else script_dir+="/";this.eval(imns.FIO.readTextFile(imns.FIO.openNode(script_dir+filename)));}}


include("common_scripts.js");

function main()
{
alert("inside main function");
var result=test_function(353654);
alert(result);
}
main();
//###############################################################################################
Attachments
include example.zip
(720 Bytes) Downloaded 80 times
User avatar
alex686
Posts: 32
Joined: Wed Oct 21, 2015 6:48 am
Contact:

Re: Include javascript files (solution)

Post by alex686 » Mon Jan 18, 2016 5:57 am

Have you tried Browserify?
iMacros for Firefox: 8.9.7
Firefox: 49.0.1
Pale Moon: 27.0.2
OS: Arch Linux, Windows 10

Manage iMacros from your mobile device: https://manage.getbot.ru
Post Reply