Include JS Functions Library in JS File

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

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
cdwyland
Posts: 9
Joined: Sun May 06, 2012 3:19 pm

Include JS Functions Library in JS File

Post by cdwyland » Fri Aug 10, 2012 6:47 am

I have many macros written in javascript, and most of them use the same custom functions that I have written. As such, I want to have just 1 single js functions library file and call that from each of my macro scripts so as to not duplicate code. I know there is no native way to include files within .js and nothing I have tried as worked to append two .js files.

I'm sure this is something that someone else using iMacros has asked themself, so how do I do this?
soma
Posts: 33
Joined: Fri Aug 10, 2012 12:02 am

Re: Include JS Functions Library in JS File

Post by soma » Thu Aug 16, 2012 4:06 pm

For my scripts I load them using the the eval function.

For example to load jQuery:

Code: Select all

loadScriptAtURL('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
if (!jQuery) {
    throw new Error('jQuery failed to load');
}
$.ajaxSetup({
    async: false
});

/**
  * Function to load a generic script at the specified URL
  */ 
function loadScriptAtURL(url) {

    var request = new XMLHttpRequest();
    var async = false;
    request.open('GET', url, async);

    request.send();
    // because of "false" above, will block until the request is done and status
    // is available. Not recommended, however it works for simple cases.
    if (request.status !== 200) {
        var message = 'an error occurred while loading script at url: ' + url +', status: ' + request.status;
        iimDisplay(message);
        return false;
    }
    eval(request.response);
    return true;
}
I have only used this technique to load scripts that are hosted somewhere and are web accessible. You might be able to do this using a URL to a local file but I have not tried that.

If you need somewhere to host your scripts you can always make a GitHub repository containing your scripts. Then on the GitHub website click the link to the "raw" version of you .js file and copy the URL
cdwyland
Posts: 9
Joined: Sun May 06, 2012 3:19 pm

Re: Include JS Functions Library in JS File

Post by cdwyland » Fri Aug 17, 2012 7:23 pm

I uploaded my functions library and changed the url in the code you provided. I added the code to a test script on my computer but I am unable to call any of my functions from my functions library that I uploaded to my server.
soma
Posts: 33
Joined: Fri Aug 10, 2012 12:02 am

Re: Include JS Functions Library in JS File

Post by soma » Fri Aug 17, 2012 9:44 pm

What debugging have you done? Does request.response have any text in it? Try doing an

Code: Select all

alert("request response: " + request.response);
You might have to change request.response to request.responseText

Can you load jQuery and calls it's functions?
cdwyland
Posts: 9
Joined: Sun May 06, 2012 3:19 pm

Re: Include JS Functions Library in JS File

Post by cdwyland » Fri Aug 17, 2012 11:02 pm

I get the following error when running the script (all it does is call the remote functions library).

ReferenceError: jQuery is not defined (Error code: 991)

If I remove the if (jQuery...) statement, I get the following error.

ReferenceError: request is not defined (Error code: 991)
rex2012
Posts: 2
Joined: Thu Sep 06, 2012 5:59 am

Re: Include JS Functions Library in JS File

Post by rex2012 » Thu Sep 06, 2012 6:14 am

just simply use the method component import, sample :
Define your javascript module and name it with my_module.jsm for example (.js extension does not work, dunno why)
function test_module() {
return "Test Module";
}

In your running my_test.js file, you need to reference to your module .jsm stored on the disk :
Components.utils.import("file:///C:/My_Path/my_module.jsm");
alert(test_module());
cdwyland
Posts: 9
Joined: Sun May 06, 2012 3:19 pm

Re: Include JS Functions Library in JS File

Post by cdwyland » Sun Sep 16, 2012 7:40 am

I get an error returned:

Code: Select all

Error: file:///C:/My_Path/my_module.jsm - EXPORTED_SYMBOLS is not an array. (Error code: 991)
crself
Posts: 3
Joined: Mon Oct 25, 2010 8:01 am

Re: Include JS Functions Library in JS File

Post by crself » Fri Jan 04, 2013 2:34 pm

I changed the url and got error:ReferenceError: geoip_country_code is not defined (Error code: 991)

Code: Select all

loadScriptAtURL('http://j.maxmind.com/app/geoip.js');
alert(geoip_country_code());
soma wrote:For my scripts I load them using the the eval function.

For example to load jQuery:

Code: Select all

loadScriptAtURL('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
if (!jQuery) {
    throw new Error('jQuery failed to load');
}
$.ajaxSetup({
    async: false
});

/**
  * Function to load a generic script at the specified URL
  */ 
function loadScriptAtURL(url) {

    var request = new XMLHttpRequest();
    var async = false;
    request.open('GET', url, async);

    request.send();
    // because of "false" above, will block until the request is done and status
    // is available. Not recommended, however it works for simple cases.
    if (request.status !== 200) {
        var message = 'an error occurred while loading script at url: ' + url +', status: ' + request.status;
        iimDisplay(message);
        return false;
    }
    eval(request.response);
    return true;
}
I have only used this technique to load scripts that are hosted somewhere and are web accessible. You might be able to do this using a URL to a local file but I have not tried that.

If you need somewhere to host your scripts you can always make a GitHub repository containing your scripts. Then on the GitHub website click the link to the "raw" version of you .js file and copy the URL
bred11
Posts: 1
Joined: Thu Jun 13, 2013 8:08 pm

Re: Include JS Functions Library in JS File

Post by bred11 » Thu Jun 13, 2013 9:24 pm

Anybody found solution?
stevenp
Posts: 16
Joined: Tue Jun 18, 2013 12:27 am

Re: Include JS Functions Library in JS File

Post by stevenp » Tue Jun 18, 2013 12:34 am

same problem here when using

Code: Select all

Components.utils.import("file:///C:/my_module.jsm");
I get this error:
Error: file:///C:/my_module.jsm - EXPORTED_SYMBOLS is not an array. (Error code: 991)
I can't believe that a 12-year automation software, 5-year firefox addon don't have the ability to #include javascript .js files.
Am I missing something?
eker
Posts: 7
Joined: Fri Feb 08, 2013 4:57 pm

Re: Include JS Functions Library in JS File

Post by eker » Thu Jun 20, 2013 11:33 pm

same problem for me :(
stevenp
Posts: 16
Joined: Tue Jun 18, 2013 12:27 am

Re: Include JS Functions Library in JS File

Post by stevenp » Tue Jun 25, 2013 3:17 pm

Is this a limitation in Firefox XUL environment?
stevenp
Posts: 16
Joined: Tue Jun 18, 2013 12:27 am

Re: Include JS Functions Library in JS File

Post by stevenp » Tue Jun 25, 2013 3:18 pm

I would really like to be able to include custom user functions from some library folder, and the ability to use jQuery selectors and traversing without calling .iim every time.
Thank you.
DWICBIOOM
Posts: 20
Joined: Wed Dec 29, 2010 1:24 am

Re: Include JS Functions Library in JS File

Post by DWICBIOOM » Wed Jun 26, 2013 12:41 am

Hello,

I think I may have found something helpful for this scenario. Although to be honest, I'm rather intermediate in javascript and this seems to be more of an advanced conversation so it may be of limited use to you guys. Anyway, take a look at the link below if you have a moment...

http://stackoverflow.com/questions/9500 ... 00775|a436

Regards,
Cark
stevenp
Posts: 16
Joined: Tue Jun 18, 2013 12:27 am

Re: Include JS Functions Library in JS File

Post by stevenp » Wed Jun 26, 2013 4:05 am

The topic at stackoverflow is about including javascript to the page - .createElement('script') method is about adding <script></script> tag to the page in the DOM.
The js functions, variables are not available to the imacros firefox javascript :(
Post Reply