@blotoutio/providers-auto-form-fill-sdk 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/index.js +192 -0
- package/package.json +21 -0
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
var ProvidersAutoFormFillSdk = (function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const formFillCountKey = 'formFillCount';
|
|
5
|
+
|
|
6
|
+
const extractHighPrioritySelector = (element) => {
|
|
7
|
+
if (!element) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
const attributes = element ? element.attributes : [];
|
|
11
|
+
if (element.id) {
|
|
12
|
+
return `#${element.id}`;
|
|
13
|
+
}
|
|
14
|
+
if (element.className) {
|
|
15
|
+
return `.${element.className.split(' ').join('.')}`;
|
|
16
|
+
}
|
|
17
|
+
if (element.name) {
|
|
18
|
+
return `${element.tagName.toLowerCase()}[name="${element.name}"]`;
|
|
19
|
+
}
|
|
20
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
21
|
+
if (/^data-(.+)$/.test(attributes[i].name)) {
|
|
22
|
+
return `${element.tagName.toLowerCase()}[${attributes[i].name}="${attributes[i].value}"]`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (element.tagName.toLowerCase() === 'input' && element.type) {
|
|
26
|
+
return `input[type="${element.type}"]`;
|
|
27
|
+
}
|
|
28
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
29
|
+
const attrName = attributes[i].name;
|
|
30
|
+
if (attrName !== 'type') {
|
|
31
|
+
const attrValue = attributes[i].value;
|
|
32
|
+
return `${element.tagName.toLowerCase()}[${attrName}="${attrValue}"]`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return element.tagName.toLowerCase();
|
|
36
|
+
};
|
|
37
|
+
const extractFields = (formSelector) => {
|
|
38
|
+
const data = [];
|
|
39
|
+
const formElement = document.querySelector(formSelector);
|
|
40
|
+
const selectElements = formElement === null || formElement === void 0 ? void 0 : formElement.querySelectorAll('select');
|
|
41
|
+
selectElements === null || selectElements === void 0 ? void 0 : selectElements.forEach((selectElement) => {
|
|
42
|
+
const selectValue = selectElement.value;
|
|
43
|
+
const selectSelector = extractHighPrioritySelector(selectElement);
|
|
44
|
+
if (selectValue && selectSelector) {
|
|
45
|
+
data.push({
|
|
46
|
+
selector: selectSelector,
|
|
47
|
+
value: selectValue,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const inputElements = formElement === null || formElement === void 0 ? void 0 : formElement.querySelectorAll('input');
|
|
52
|
+
inputElements === null || inputElements === void 0 ? void 0 : inputElements.forEach((inputElement) => {
|
|
53
|
+
if (inputElement.type === 'password' ||
|
|
54
|
+
inputElement.type === 'file' ||
|
|
55
|
+
inputElement.type === 'button' ||
|
|
56
|
+
inputElement.type === 'hidden' ||
|
|
57
|
+
inputElement.type === 'image' ||
|
|
58
|
+
inputElement.type === 'submit') {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const inputValue = inputElement.type === 'radio' || inputElement.type === 'checkbox'
|
|
62
|
+
? inputElement.checked
|
|
63
|
+
: inputElement.value;
|
|
64
|
+
const inputSelector = extractHighPrioritySelector(inputElement);
|
|
65
|
+
if (inputValue && inputSelector) {
|
|
66
|
+
data.push({
|
|
67
|
+
selector: inputSelector,
|
|
68
|
+
value: inputValue,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
const textAreaElements = formElement === null || formElement === void 0 ? void 0 : formElement.querySelectorAll('textarea');
|
|
73
|
+
textAreaElements === null || textAreaElements === void 0 ? void 0 : textAreaElements.forEach((textAreaElement) => {
|
|
74
|
+
const textAreaValue = textAreaElement.value;
|
|
75
|
+
const textAreaSelector = extractHighPrioritySelector(textAreaElement);
|
|
76
|
+
if (textAreaValue && textAreaSelector) {
|
|
77
|
+
data.push({
|
|
78
|
+
selector: textAreaSelector,
|
|
79
|
+
value: textAreaValue,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return JSON.stringify(data);
|
|
84
|
+
};
|
|
85
|
+
const updateFormFields = (formSelector, keys, cachedKvData) => {
|
|
86
|
+
try {
|
|
87
|
+
formSelector = formSelector.trim();
|
|
88
|
+
const formElement = document.querySelector(formSelector);
|
|
89
|
+
const formattedSelector = `AUTO_FILL_${formSelector}`;
|
|
90
|
+
if (!formSelector ||
|
|
91
|
+
!formElement ||
|
|
92
|
+
!(formattedSelector in Object.assign(Object.assign({}, keys), cachedKvData))) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const isDataCached = Object.keys(cachedKvData).includes(formattedSelector);
|
|
96
|
+
const fields = isDataCached
|
|
97
|
+
? JSON.parse(cachedKvData[formattedSelector].toString())
|
|
98
|
+
: JSON.parse(keys[formattedSelector].toString());
|
|
99
|
+
if (!fields || !fields.length) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
fields.forEach((field) => {
|
|
103
|
+
const element = formElement === null || formElement === void 0 ? void 0 : formElement.querySelector(field.selector);
|
|
104
|
+
if (element) {
|
|
105
|
+
if ((element.tagName.toLowerCase() === 'input' &&
|
|
106
|
+
element.type === 'checkbox') ||
|
|
107
|
+
element.type === 'radio') {
|
|
108
|
+
;
|
|
109
|
+
element.checked = field.value;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
element.value = field.value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return isDataCached ? false : true;
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
console.log(`Unable to auto fill the form with selector ${formSelector.trim()}`);
|
|
120
|
+
console.error(e);
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const getFormattedSelectorName = (selector) => {
|
|
125
|
+
return `AUTO_FILL_${selector}`;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const init = (params) => {
|
|
129
|
+
const { manifest, getEdgeData, sendEdgeData, keyName } = params;
|
|
130
|
+
try {
|
|
131
|
+
if (!manifest ||
|
|
132
|
+
!manifest.variables ||
|
|
133
|
+
!manifest.variables['formSelectors']) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const formSelectors = JSON.parse(manifest.variables['formSelectors']);
|
|
137
|
+
if (!formSelectors || !formSelectors.length) {
|
|
138
|
+
console.error('Enter a valid form selectors to auto-fill');
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const formattedSelectors = formSelectors.map((item) => getFormattedSelectorName(item));
|
|
142
|
+
const sessionData = JSON.parse(sessionStorage.getItem(keyName) || '{}');
|
|
143
|
+
const kvData = sessionData && 'kv' in sessionData ? sessionData['kv'] : {};
|
|
144
|
+
const formSelectorsWOCache = formattedSelectors.filter((formSelector) => !Object.keys(kvData).includes(formSelector));
|
|
145
|
+
let successCount = 0;
|
|
146
|
+
getEdgeData([...formSelectorsWOCache, formFillCountKey], (keys) => {
|
|
147
|
+
formSelectors.forEach((formSelector) => {
|
|
148
|
+
const result = updateFormFields(formSelector, keys, kvData);
|
|
149
|
+
successCount = successCount + (result === true ? 1 : 0);
|
|
150
|
+
});
|
|
151
|
+
if (successCount) {
|
|
152
|
+
if (!isNaN(parseInt(keys[formFillCountKey]))) {
|
|
153
|
+
successCount = successCount + parseInt(keys[formFillCountKey]);
|
|
154
|
+
}
|
|
155
|
+
sendEdgeData({ [formFillCountKey]: successCount.toString() }, {}, { method: 'beacon' });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
formSelectors.forEach((formSelector) => {
|
|
159
|
+
const element = document.querySelector(formSelector);
|
|
160
|
+
if (element) {
|
|
161
|
+
element.addEventListener('submit', () => {
|
|
162
|
+
const formData = extractFields(formSelector);
|
|
163
|
+
const formattedSelector = getFormattedSelectorName(formSelector);
|
|
164
|
+
sendEdgeData({ [formattedSelector]: formData }, {}, { method: 'beacon' });
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
console.error(e);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const data = {
|
|
175
|
+
name: 'autoFormFill',
|
|
176
|
+
init,
|
|
177
|
+
};
|
|
178
|
+
try {
|
|
179
|
+
if (window) {
|
|
180
|
+
if (!window.edgetagProviders) {
|
|
181
|
+
window.edgetagProviders = [];
|
|
182
|
+
}
|
|
183
|
+
window.edgetagProviders.push(data);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch (_a) {
|
|
187
|
+
// No window
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return data;
|
|
191
|
+
|
|
192
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blotoutio/providers-auto-form-fill-sdk",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Auto Form Fill Browser SDK for EdgeTag",
|
|
5
|
+
"author": "Blotout",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/blotoutio/edgetag-sdk",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"main": "./index.js",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/blotoutio/edgetag-sdk.git"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"package.json",
|
|
19
|
+
"README.md"
|
|
20
|
+
]
|
|
21
|
+
}
|