AutoControl Control your browser your way Forum               Install now from theChrome Web Store

Script Isolation


An AutoControl script is isolated from the page it's running in and from other AutoControl scripts running in the same page. This is desireable to avoid conflicts between variables and function names created by each script and by the page's own code.
The way this isolation works and the methods to go around it are different in each case. Let's see them separately.
Isolation from the page
An AutoControl script and the page's own code run in different global contexts. These two contexts have their own copy of the window global object and the entire DOM.
A variable or property created in one context, like this window.myGlobalVar = 'Hi' or like this document.body.myProperty = 'Bye', will not be visible from the other context.
However, doing this document.body.innerHTML = 'Hello, world!' will be visible from both contexts (including other scripts), because this affects the rendering of the page, which can be manipulated from both contexts even though they have a separate copy of the DOM.
So, an AutoControl script has full access to the page and all its Web APIs as usual, but it cannot directly access the functions and variables created by the page's code.

Let's see this isolation in action and then we'll see how to circumvent it.
We'll use the page your are reading right now as an example. If you see its source code, you'll find this code in it:

	var myGlobalVar = "I'm in the page context." ;
	function myFunction(...args){
		return myGlobalVar + ' Passed arguments are: ' + args ;
	}
	

Now, if we use AutoControl to run the following script in this page:

	alert( myFunction('Hello!') ) ;
	

We'll get the error ReferenceError: myFunction is not defined, which shows that the script is indeed unable to find a function with that name, let alone use it.
In order to get around this isolation, AutoControl's scripting API provides the ACtl.runInPageCtx function which, in its simplest form, can be used as follows:

	ACtl.runInPageCtx( ()=>{
		alert( myFunction('Hello!') ) ;
	}) ;
	

Now the script runs as expected, with no errors. Go ahead and try it yourself on this page, it's a real example.
Also, in addition to just running code in the page context, ACtl.runInPageCtx can be used to pass variables from one context to another. Learn how at the function's reference page.
Isolation from other scripts
AutoControl scripts run all in the same global context, but each on its own local scope. This way a script can define local variables that can only be seen by itself. But they can also create global varibles that are visible to all AutoControl scripts running on the same page.
For example:

	let localVariable = 'No other script can see me' ;
	globalVariable = 'All scripts will see this' ;
	

The use of the let keyword (as well as var) creates the variable in the script's local scope, whereas omitting let will create the variable in the global scope (unless the variable already exists in the local scope).
Likewise, we can do the same with functions:

	function localFunction(...args){ /* ... */ }
	let alsoLocal = function(...args){ /* ... */ } ;
	globalFunction = function(...args){ /* ... */ } ;
	

Variables and functions have the same lifetime of the page the script is running in. Once a tab is closed, reloaded or navigated to a different page, all variables and scripts running on that page disappear.
In order to keep data persistently, scripts can use ACtl.var for private variables accessible to the script only, and ACtl.pubVar for public variables accessible to all scripts.
Scripting API Script Isolation Asynchronicity Backgrnd Scripts GUI vs API ACtl.include ACtl.import ACtl.getFile ACtl.saveFile ACtl.saveURL ACtl.openURL ACtl.closeTab ACtl.runInTab ACtl.runInFrames ACtl.runInPageCtx ACtl.getTabInfo ACtl.getTabIds ACtl.TAB_ID ACtl.setTabState ACtl.captureTab ACtl.execAction ACtl.runCommand ACtl.getClipboard ACtl.setClipboard ACtl.expand ACtl.switchState ACtl.var ACtl.pubVar ACtl.on ACtl.off ACtl.sleep ACtl.STOP_CHAIN ACtl.STOP_FULL_SEQ