-
Notifications
You must be signed in to change notification settings - Fork 9
/
content.js
111 lines (101 loc) · 2.87 KB
/
content.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
const AUTOFILL_DATA_FILE = chrome.runtime.getURL('data.json');
const SITES_SELECTOR_MAPPING_FILE = chrome.runtime.getURL('sites.json');
let autofillData = {};
let sitesData = {};
/**
* Import the data.json file
*/
const dataStream = fetch(AUTOFILL_DATA_FILE)
.then(response => response.json())
.catch(e => console.log(`failed to read data.json: ${e}`));
/**
* Import the sites.json file
*/
const sitesStream = fetch(SITES_SELECTOR_MAPPING_FILE)
.then(response => response.json())
.catch(e => console.log(`failed to read sites.json: ${e}`));
/**
* Wait for all promises to be resolved before we initialize the extension
*/
Promise.all([
dataStream,
sitesStream
]).then(responses => {
autofillData = responses[0];
sitesData = responses[1];
init();
})
/**
* Checks if the hostname is supported.
* Finds a standardized equivalent if there's no direct match.
* @param {String} hostname - hostname of the current window
* @returns {String|null} - returns the standardized sitename or null
*/
function _isSupported(hostname) {
if (sitesData[hostname]) {
return hostname;
}
for (let site in supportedSites) {
if (hostname.includes(site)) {
return site;
}
}
return null;
}
/**
* Checks if the application type is simple or complex
* @param {String} siteName - standardized sitename
* @returns {Boolean}
*/
function _isComplex(siteName) {
return sitesData[siteName].isComplex;
}
/**
* Initialize the autofill extension
*/
function init() {
const siteName = _isSupported(window.location.hostname);
if (!siteName) {
return;
}
if (_isComplex(siteName)) {
autofillComplexPages(siteName);
} else {
autofillPage(siteName);
}
}
/**
* Find the autofill-able input fields on the page according the hostname
* @param {String} siteName - standardized sitename of the current window
*/
function autofillPage(siteName) {
for (let selector in sitesData[siteName].selectorMapping) {
const inputElement = $(selector);
if (inputElement.length) {
const inputData = sitesData[siteName].selectorMapping[selector];
autofillInput(selector, inputData, inputElement);
}
}
}
/**
* Handles autofill for complex application pages
* @param {String} siteName - standardized sitename of the current window
*/
function autofillComplexPages(siteName) {
// TODO: Handle complex cases
}
/**
* Autofill the input field(s) with the input data
* @param {jQuery} inputElement - jQuery Object containing the input element(s)
* @param {String} inputData - data to be autofilled into the input element
*/
function autofillInput(selector, inputData, inputElement) {
const inputValue = autofillData[inputData];
inputElement.each(element => {
if (inputElement[element].tagName === 'SELECT') {
$(`${selector} option:contains("${inputValue}")`).attr("selected", true);
} else {
$(selector).val(inputValue).change();
}
});
}