-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrun.js
143 lines (131 loc) · 4.3 KB
/
run.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
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
function isFunction(functionToCheck) {
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}
function randomInt(min,max)
{
var result = Math.floor((Math.random() * max) + min);
return result;
}
var htmlInputTypes = {
"color" : function(){
return '#'+pad(Math.floor(Math.random()*16777215).toString(16), 6);
},
"date" : function(){
var rndDate = randomDate(new Date(1977, 8, 1), new Date(2999, 8, 1));
var result = [
rndDate.getFullYear(),
"-",
pad(rndDate.getMonth() + 1, 2),
"-",
pad(rndDate.getDay() + 1, 2),
].join('');
return result;
},
"datetime-local" : "",
"email" : "internet.email",
"month" : function(){
var rndDate = randomDate(new Date(1977, 8, 1), new Date(2999, 8, 1));
var result = [
rndDate.getFullYear(),
"-",
pad(rndDate.getMonth() + 1, 2)
].join('');
return result;
},
"number" : "random.number",
"range" : function(el){
var minValue= parseInt($(el).prop('min'), 10);
var maxValue = parseInt($(el).prop('max'), 10);
return randomInt(minValue, maxValue);
},
"search" : "lorem.words",
"tel" : "phone.phoneNumber",
"url" : "internet.url",
"week" : function(){
var rndDate = randomDate(new Date(1977, 8, 1), new Date(2999, 8, 1));
var onejan = new Date(rndDate.getFullYear(),0,1);
var weeknumber = Math.ceil((((rndDate - onejan) / 86400000) + onejan.getDay()+1)/7);
var result = rndDate.getFullYear() + "-W" + pad(weeknumber, 2);
return result;
},
"radio" : selectRandomRadio,
"checkbox" : function(el){
var randomBoolean = Math.random() >= 0.5;
$(el).attr("checked", randomBoolean);
}
};
var fakerCultures = ["az", "cz", "de", "de_AT", "de_CH", "en", "en_AU", "en_BORK", "en_CA", "en_GB", "en_IE", "en_IND", "en_US", "en_au_ocker", "es", "es_MX", "fa", "fr", "fr_CA", "ge", "id_ID", "it", "ja", "ko", "nb_NO", "nep", "nl", "pl", "pt_BR", "ru", "sk", "sv", "tr", "uk", "vi", "zh_CN", "zh_TW"];
function selectRandomRadio(el){
var groupName = $(el).attr("name");
var radios = $("input:radio:enabled[name='" + groupName + "']");
var idx = randomInt(0, radios.length - 1);
$(radios[idx]).prop("checked", true);
}
var randomizeInputValue = function(el){
if ($(el).length != 0){
switch( $(el)[0].nodeName.toLowerCase() ) {
case "input":
var type = $(el).attr('type');
var value = faker.lorem.word();
var fakerMethod = htmlInputTypes[type];
if (fakerMethod){
if (isFunction(fakerMethod)){
value = fakerMethod(el);
} else {
value = faker.fake("{{" + fakerMethod + "}}")
}
}
$(el).focus().val(value);
break;
case "select":
var opts = $(el)[0].options;
var idx = randomInt(0, opts.length - 1);
var val = opts[idx].value;
$(el).focus().val(val);
break;
case "textarea":
$(el).focus().val(faker.lorem.sentences());
break;
}
}
};
chrome.extension.sendRequest({
"action": "getOptions",
"args": []
}, function(response){
if (deepAutofillChromeExtensionSettings){
if (deepAutofillChromeExtensionSettings.randomLocale){
console.info("setting locale", deepAutofillChromeExtensionSettings.randomLocale);
faker.locale = deepAutofillChromeExtensionSettings.randomLocale
}
for(var i = 0; i < deepAutofillChromeExtensionSettings.fields.length; i++){
var field = deepAutofillChromeExtensionSettings.fields[i];
var value = faker.lorem.word();
if (field.static){
value = field.static;
}
if (field.random){
value = faker.fake(field.random);
}
if (!field.static && !field.random){
randomizeInputValue($(field.selector));
} else {
$(field.selector).focus().val(value);
}
console.debug(field.selector, value);
}
} else {
$("input:enabled, select:enabled, textarea:enabled").not(':button,:hidden,input[type=submit],input[readonly]').each(function(){
randomizeInputValue(this);
});
}
});