Global Safari Exension functions and scope

Guides | Tutorial By 3 years ago

Safari applications seem to have a different scope between your target html page and main.js end script. This means that if you wish to inject a control onto your screen which calls a function in your main.js script it will not work because the two can’t see each other.

Instead what you need to do is put your javascript function in the scope of your html page, which is actually fairily easy.

You can inject the javascript function into the html page with the following function in your main.js.

function addFunction(func)
	//	Inject function into page html
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.innerHTML = func.toString();

Use the function as so in main.js:

function doStuff()


This will import the doStuff function into the main html page, which is now in the same scope as any controls calling doStuff() on the same page.