b2cloud

14th February 2014

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();
	head.appendChild(script);
}

Use the function as so in main.js:

function doStuff()
{
	alert('hello');
}

addFunction(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.

Recommended Posts

View/modify Safari extension source code

Post by 3 years ago

If you want to see how a Safari extension is working, or even change its functionality, it’s quite easy. First, locate the extension. It should be living in your library folder. Note this is a

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!