-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
30 lines (23 loc) · 900 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var url = require('url');
module.exports = function (root, cb) {
root.addEventListener('click', function (ev) {
if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.defaultPrevented) {
return true;
}
var anchor = null;
for (var n = ev.target; n.parentNode; n = n.parentNode) {
if (n.nodeName === 'A') {
anchor = n;
break;
}
}
if (!anchor) return true;
var href = anchor.getAttribute('href');
var u = url.parse(anchor.getAttribute('href'));
if (u.host && u.host !== location.host) return true;
ev.preventDefault();
var base = location.protocol + '//' + location.host;
cb(url.resolve(location.pathname, u.path || '') + (u.hash || ''));
return false;
});
};