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

ACtl.getFile


Gets the content of any local file or URL. The maximum file size is only limited by the available RAM. ACtl.getFile(fileLocation[,returnType]) fileLocation Type: string Either a URL, data URI, local path or UNC path of the file. It can be relative to the script's location (if there is one). It can also contain placeholders such as <desktop>, <documents> and others. returnType Type: string Default: 'text' How to return the file's content.
'text' A text string
'json' Same type returned by JSON.parse
'html' A DocumentFragment object
'htmlDoc' A Document object
'xmlDoc' An XMLDocument object
'css' An HTMLStyleElement object
'module' A Javascript module object (same as ACtl.import)
'image' An HTMLImageElement object
'canvas' An HTMLCanvasElement object
'binary' A binary string
'blob' A Blob object
'file' A File object
'objectUrl' An object URL string
'dataUri' A data URI string
'base64' A Base64 string
Returns Type: Promise Returns immediately. The Promise will resolve with the file content as specified by returnType. Throws Type: string Error description if fileLocation cannot be accessed or if the file cannot be interpreted as returnType.

Examples

Get the first Google result for a search term.

	let search = 'Olympics' ;
	//Download the search results as an HTML fragment
	let html = await ACtl.getFile('https://google.com/search?q='+search, 'html') ;
	//Get the first search result
	let result = html.querySelector('#main a[href^=http][ping]') ;
	//Extract the title and URL of the result
	let title = result.innerText ;
	let url = result.href ;
	alert(`TITLE:\n${title}\n\nURL:\n${url}`) ;
	

Add a text label to a local image file.

	let filePath = 'C:/Projects/image.png' ;
	//Load the image file as a canvas object
	let canvas = await ACtl.getFile(filePath, 'canvas') ;
	let ctx = canvas.getContext('2d') ;
	//Set text style
	ctx.font = '14px arial' ;
	ctx.fillStyle = 'blue' ;
	ctx.textBaseline = 'top' ;
	//Draw the text in the canvas at coordinates [5,5]
	ctx.fillText('Hello, world!', 5, 5) ;
	//Save the canvas object back to the file
	await ACtl.saveFile(filePath, canvas) ;
	

Obtain Tesla's current stock price from a JSON service.

	let url = `http://bloomberg.com/markets2/api/intraday/TSLA:US?days=1&interval=5`;
	let data = await ACtl.getFile(url, 'json') ;
	alert("TESLA stock price is: $" + data[0].price.slice(-1)[0].value ) ;
	
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