-
Notifications
You must be signed in to change notification settings - Fork 17
/
obfuscate.js
160 lines (143 loc) · 3.45 KB
/
obfuscate.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { useState } from 'react';
import T from 'prop-types';
const Obfuscate = (props) => {
const {
element,
children,
tel,
sms,
facetime,
email,
href,
headers,
obfuscate,
obfuscateChildren,
linkText,
style,
onClick,
...others
} = props;
const [humanInteraction, setHumanInteraction] = useState(false);
const linkProps = children || tel || sms || facetime || email || href;
const Component = element;
const generateLink = () => {
let link;
// Combine email header parameters for use with email
const combineHeaders = (params) => {
return Object.keys(params)
.map((key) => `${key}=${encodeURIComponent(params[key])}`)
.join('&');
};
if (email) {
link = `mailto:${email}`;
if (headers) {
link += `?${combineHeaders(headers)}`;
}
} else if (tel) {
link = `tel:${tel}`;
} else if (sms) {
link = `sms:${sms}`;
} else if (facetime) {
link = `facetime:${facetime}`;
} else if (href) {
link = href;
} else if (typeof children !== 'object') {
link = children;
} else {
return '';
}
return link;
};
const handleClick = () => {
// If focused or hovered, this js will be skipped with preference for html
if (humanInteraction === false) {
// Allow instantiator to provide an onClick method to be called
// before we change location (e.g. for analytics tracking)
if (onClick && typeof onClick === 'function') {
onClick();
}
window.location.href = generateLink({
email,
headers,
tel,
sms,
facetime,
href,
children,
});
}
};
const reverse = (content) =>
typeof content !== 'undefined' &&
content.split('').reverse().join('').replace('(', ')').replace(')', '(');
const obfuscatedStyle = {
...style,
unicodeBidi: 'bidi-override',
direction:
humanInteraction === true ||
obfuscate === false ||
obfuscateChildren === false
? 'ltr'
: 'rtl',
};
const renderedLink =
humanInteraction === true ||
obfuscate === false ||
typeof children === 'object' ||
obfuscateChildren === false // Allow child elements
? linkProps
: reverse(linkProps);
const clickProps =
Component === 'a'
? {
href:
humanInteraction === true || obfuscate === false
? generateLink()
: linkText || 'obfuscated',
onClick: handleClick,
}
: {};
return (
<Component
onFocus={() => setHumanInteraction(true)}
onMouseOver={() => setHumanInteraction(true)}
onContextMenu={() => setHumanInteraction(true)}
{...others}
{...clickProps}
style={obfuscatedStyle}
>
{renderedLink}
</Component>
);
};
Obfuscate.propTypes = {
element: T.string,
children: T.node,
tel: T.string,
sms: T.string,
facetime: T.string,
email: T.string,
href: T.string,
headers: T.shape({}),
obfuscate: T.bool,
obfuscateChildren: T.bool,
linkText: T.string,
style: T.shape({}),
onClick: T.func,
};
Obfuscate.defaultProps = {
element: 'a',
children: undefined,
tel: undefined,
sms: undefined,
facetime: undefined,
email: undefined,
href: undefined,
headers: undefined,
obfuscate: undefined,
obfuscateChildren: undefined,
linkText: undefined,
style: {},
onClick: undefined,
};
export default Obfuscate;