Skip to content

Instantly share code, notes, and snippets.

@adnan-i
Last active August 29, 2015 14:15
Show Gist options
  • Save adnan-i/9782cbdc8fd27a517e72 to your computer and use it in GitHub Desktop.
Save adnan-i/9782cbdc8fd27a517e72 to your computer and use it in GitHub Desktop.
Angular directive that prepends all outbound links with a "http referrer obfuscation" service
"use strict";
var angular = require("angular");
/**
* Prepends all outbound links with a "http referrer obfuscation" service
*/
angular.module("ax.components.dom.hideReferrer", [])
.directive("a", function HideReferrerDirective($window) {
// webpack module bilder allows for CommonJS module loading
var config = require("../../../../config");
// Utilizing DOM and JS to parse URI
var referrer = $window.document.createElement('a');
referrer.href = config.referrerService;
return {
restrict: "E",
link: function(scope, elem, attrs) {
var a = elem[0];
// Let's not observe local ui-router links
if (attrs.hasOwnProperty('uiSref')) {
return;
}
attrs.$observe('href', function(href) {
// We wait for the href attr to compile, then we ensure it's not a local link, and then we check it's not already pre-pended
if (a.hostname && a.hostname !== $window.location.hostname && a.hostname !== referrer.hostname) {
a.href = config.referrerService + a.href;
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment