-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
121 lines (101 loc) · 3.98 KB
/
parser.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
document.addEventListener('mousedown', function(event) {
var tokens = "";
/* Analyse Element */
if (event.target !== 'undefined') {
tokens += parseNodeAttributes(event.target);
tokens += parseNodeText(event.target);
}
/* Analyse Children */
if (matchSocial(tokens) == "" || !matchAction(tokens)) {
if (event.target !== 'undefined' && event.target.childNodes !== 'undefined' && event.target.childNodes.length > 0) {
var children = event.target.childNodes;
for (var i = 0; i < children.length; i++) {
tokens += parseNodeAttributes(children[i]);
tokens += parseNodeText(children[i]);
}
}
}
/* Analyse Parent */
if (matchSocial(tokens) == "" || !matchAction(tokens)) {
if (event.target !== 'undefined' && event.target.parentNode !== 'undefined') {
tokens += parseNodeAttributes(event.target.parentNode);
tokens += parseNodeText(event.target.parentNode);
}
}
/* Analyse Grandparent */
if (matchSocial(tokens) == "" || !matchAction(tokens)) {
if (event.target !== 'undefined' && event.target.parentNode !== 'undefined' && event.target.parentNode.parentNode !== 'undefined') {
tokens += parseNodeAttributes(event.target.parentNode.parentNode);
tokens += parseNodeText(event.target.parentNode.parentNode);
}
}
/* Analyse Siblings */
if (matchSocial(tokens) == "" || !matchAction(tokens)) {
if (event.target !== 'undefined' && event.target.parentNode !== 'undefined' && event.target.parentNode.childNodes !== 'undefined'
&& event.target.parentNode.childNodes.length > 0) {
var children = event.target.parentNode.childNodes;
for (var i = 0; i < children.length; i++) {
tokens += parseNodeAttributes(children[i]);
tokens += parseNodeText(children[i]);
}
}
}
/* Analyse Siblings of Parent */
if (matchSocial(tokens) == "" || !matchAction(tokens)) {
if (event.target !== 'undefined' && event.target.parentNode !== 'undefined' && event.target.parentNode.parentNode !== 'undefined'
&& event.target.parentNode.parentNode.childNodes !== 'undefined' && event.target.parentNode.parentNode.childNodes.length > 0) {
var children = event.target.parentNode.parentNode.childNodes;
for (var i = 0; i < children.length; i++) {
tokens += parseNodeAttributes(children[i]);
tokens += parseNodeText(children[i]);
}
}
}
/* Save the found social network chosen to Sign In - done on background (so its possible to know current tab) */
if ( matchSocial(tokens) !== "" && matchAction(tokens) ) {
chrome.runtime.sendMessage({social_found: matchSocial(tokens)});
}
});
/* Retrieves all attribute values from Node */
function parseNodeAttributes(el) {
if ( typeof el.attributes === 'undefined' ) { return ""; }
var nodes=[], values=[];
for (var att, i = 0, atts = el.attributes, n = atts.length; i < n; i++) {
att = atts[i];
nodes.push(att.nodeName);
values.push(att.nodeValue);
}
return values.toString();
}
/* Retrieves the text from Node */
function parseNodeText(el) {
if ( el.firstChild === null || typeof el.firstChild.nodeValue === 'undefined' ) {
return "";
} else {
return el.firstChild.nodeValue;
}
}
/* Returns true if it finds any of the specified action words */
function matchAction(tokens) {
var action_tokens = ['login', 'logon', 'log', 'signin', 'signon', 'sign', 'connect', 'register', 'enter', 'continue'];
var action_found = new Boolean(false);
for ( var i = 0; i < action_tokens.length; i++ ) {
var match = new RegExp(action_tokens[i], 'i');
if ( tokens.search(match) != -1 ) {
action_found = new Boolean(true);
}
}
return action_found;
}
/* Returns the social network found on the button (if any) */
function matchSocial(tokens) {
var social_tokens = ['facebook', 'twitter', 'google', 'angellist', 'instagram', 'github', 'linkedin', 'buffer', 'spotify', 'truecaller', 'whatsapp', 'microsoft'];
var social_found = "";
for ( var i = 0; i < social_tokens.length; i++ ) {
var match = new RegExp(social_tokens[i], 'i');
if ( tokens.search(match) != -1 ) {
social_found = social_tokens[i];
}
}
return social_found;
}