forked from 18F/private-eye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private-eye.js
61 lines (42 loc) · 1.44 KB
/
private-eye.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// https://github.com/18F/private-eye
(function() {
'use strict';
var PrivateEye = function(opts) {
var styles = document.createElement('style');
styles.innerHTML = 'a.private-link::after { content: "\\1F512"; font-size: 0.75em; vertical-align: top; }';
document.body.appendChild(styles);
var defaultMessage;
if ( opts.defaultMessage && 'string' === typeof opts.defaultMessage ) {
defaultMessage = opts.defaultMessage;
} else {
defaultMessage = "This is a link to a private site, which may or may not be accessible to you.";
}
opts.ignoreUrls.forEach(function(url) {
var hrefValue;
var titleValue;
// If the `url` is an Object, then parse the properties `message` & `url`
//
if ( url === Object( url ) ) {
titleValue = url.message;
hrefValue = url.url;
} else {
hrefValue = url;
titleValue = defaultMessage;
}
var anchors = document.querySelectorAll('a[href*="' + hrefValue + '"]');
Array.prototype.forEach.call(anchors, function(anchor) {
anchor.className += ' private-link';
// Only replace the anchor's title if it is empty
//
if ( ! anchor.title ) {
anchor.title = titleValue;
}
});
});
};
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = PrivateEye;
} else {
window.PrivateEye = PrivateEye;
}
})();