Include JS Functions Library in JS File

Discussions and Tech Support specific to the iMacros Firefox add-on.
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
stevenp
Posts: 16
Joined: Tue Jun 18, 2013 12:27 am

Re: Include JS Functions Library in JS File

Post by stevenp » Fri Aug 30, 2013 8:44 am

instead of

Code: Select all

var request = new XMLHttpRequest();
write

Code: Select all

const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
request = XMLHttpRequest();
GERAsimov
Posts: 1
Joined: Sun Jul 06, 2014 2:31 pm

Re: My synchronous version without jQuery

Post by GERAsimov » Sun Jul 06, 2014 2:41 pm

After 3 days of searching, I found the solution thanks for stevenp and heinob.

source on stackoverflow:

Code: Select all

function myRequire( url ) {
	const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
	var ajax = XMLHttpRequest();
    ajax.open( 'GET', url, false ); // <-- the 'false' makes it synchronous
    ajax.onreadystatechange = function () {
    	var script = ajax.response || ajax.responseText;
    	if (ajax.readyState === 4) {
    		switch( ajax.status) {
    			case 200:
    			eval.apply( window, [script] );
    			console.log("script loaded: ", url);
    			break;
    			default:
    			console.log("ERROR: script not loaded: ", url);
    		}
    	}
    };
    ajax.send(null);
}
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Include JS Functions Library in JS File

Post by viking » Sun Jun 21, 2020 9:50 pm

I would like to use the moment.js library (https://www.webfx.com/blog/web-design/j ... moment-js/) in an iMacros javascript. Is that possible?

iMacros v8.9.7
Firefox v52.8.1
WIN7 x64
access2vivek
Posts: 63
Joined: Wed Dec 20, 2017 10:00 am
Location: New Delhi, India
Contact:

Re: Include JS Functions Library in JS File

Post by access2vivek » Tue Jul 28, 2020 8:23 am

I was stuck at this very point for a very long time. It took me months to finally find a working solution. Here it is:-

Code: Select all

//	Define a XMLHttpRequest object

const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
var getLibrary= XMLHttpRequest();

//	Create a Get Request for the script/Library
getLibrary.open('GET',"URL OF THE SCRIPT",false);

//	Send the request
getLibrary.send();

//	Add the response code to the current window
eval.apply(window,[getLibrary.response]);
Now, you should be able to use your functions easily from an online source. If however, you want to use a file in your system, simple add the "file:///" tag before it. For example: -

Code: Select all

getLibrary.open('GET',"file:///home/access2vivek/Documents/Programming/iMacros/customLibrary.js",false);
Edit: - It seems, there is already a solution for the same.
Robust iMacros development with quick turnaround time. Drop an email at "access2content@gmail.com" with your project details.
Post Reply