Created
October 12, 2016 14:04
-
-
Save EranSch/3ff58e1e68d75567a520263fdce0b5e9 to your computer and use it in GitHub Desktop.
Rewrite links leaving site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const $ = require('jquery'); | |
$('a').each(function() { | |
var $this = $(this); | |
var url = $this.attr('href'); | |
var redir = `//${location.host}/`; | |
var href = /http(s|):\/\//i; | |
var hrefPat = new RegExp(href); | |
if (hrefPat.test(url) && !!!$this.attr('data-allowext')) { | |
var www = /(www|)\.example\.com/i; | |
var wwwPat = new RegExp(www); | |
if (!wwwPat.test(url)) { | |
var newurl = redir + 'leaving-example/' + | |
'?dest=' + encodeURIComponent(url.toString()) + | |
'&ref=' + encodeURIComponent(location.pathname) | |
$this.attr('href', newurl); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment