Writing your own content-blocker for Safari 9 in El Capitan: 5, you’re on your own now

Having joined the Apple Developer Program, you will discover how to obtain a Safari Extensions signing certificate. As usual, Apple guides you through the process in perfect detail, and your certificate will be installed on your Mac.

You now need to develop your content-blocker, using the outline given in the inital article, and the details of the second. Create and save your Safari Extension folder as described in the fourth article, and construct your JSON source file, saving it to that folder.

A good start might be the following:
[
{
"action": {
"type": "block-cookies"
},
"trigger": {
"url-filter": ".*"
}
},
{
"action": {
"type": "block"
},
"trigger": {
"url-filter": "googleads.*"
}
},
{
"action": {
"type": "block"
},
"trigger": {
"url-filter": "googlesyndication.*"
}
}
]

safconblock51The first trigger-action pair blocks all cookies from all sites, and is a good way of discovering which sites do rely on cookies for their content. For example, amazon.co.uk and other online retailers will not recognise you, instead of greeting you in their normal way.

The second and third trigger-action pairs simply block all content from the two major Google advertising streams, which accounts for a sizeable proportion of online advertising. Plenty of ads from other sites will still make their way through this filter, but it will surprise you just how much comes from those two Google streams alone.

safconblock52The develop cycle is actually smoother than it first seemed: edit your JSON source, install the content-blocker using the button in Extensions Builder, test on sites, uninstall the blocker, edit the JSON, install, and so on. But it is so much better for having done it the ‘official’ way with a certificate, as once you are happy that your content-blocker is doing the job, you can leave it installed, even share or sell it.

You can find a good range of content-blockers already available in source, to provide inspiration and guidance: thanks to krishkumar’s BlockParty, dgraham’s Ka-Block, bettie’s BlockAnalytics, and rentzsch’s clicktoflash.

Here you may discover another advantage to writing your own content-blocker: not only can you tailor it to do what you want, and let through the more acceptable ads for sites which you want to support financially, but because your blocker is yours, sites which try to detect blocking may not recognise it.

You can now decide whether you want to focus on protecting your privacy, perhaps by only granting cookie access to certain sites where this is to your benefit, whether you want to control the onslaught and distraction of advertising, or perhaps prevent your browsing from being hijacked to potentially malicious sites, porn, or even political views which you find objectionable. You are back in control of what you are served, which can be no bad move.

Happy – and responsible – browsing.