forked from yuneko1127/gender-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
213 lines (180 loc) · 6.45 KB
/
content_script.js
File metadata and controls
213 lines (180 loc) · 6.45 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* キーワードと規定入力を定義 */
if (typeof window.KEY_WORDS === 'undefined') {
// 探してほしい入力欄のキーワードを追加
window.KEY_WORDS = ["gender", "sex", "seibetsu", "seibetu", "性別"];
}
if (typeof window.VALUE_TO_TEXT === 'undefined') {
// テキストインプットのときに入力する値を定義
window.VALUE_TO_TEXT = "無回答";
}
/* イベントリスナーの追加 */
document.addEventListener("DOMContentLoaded", () => {
randomInputs();
});
document.addEventListener("submit", (event) => {
if (event.target && event.target.tagName === "FORM") {
saveFormData(event.target);
}
}, true);
/* 関数の定義 */
// ランダム入力を行う関数
function randomInputs() {
randomRadio();
randomDropdown();
fillTextInput();
}
// ラジオボタンをランダム入力する関数
function randomRadio(){
// ラジオボタンを探す
const radioSelector = KEY_WORDS.map(keyword =>
`input[type="radio"][name*="${keyword}"], input[type="radio"][aria-label*="${keyword}"]`
).join(', ');
const radios = document.querySelectorAll(radioSelector);
// ラジオボタンが存在するか確認
if (radios.length === 0) {
return;
}
const requiredRadios = [];
radios.forEach(radio => {
if (radio.required || radio.dataset.require === "true") {
requiredRadios.push(radio);
}
});
// 選択されているボタンを解除
requiredRadios.forEach(radio => {
if (radio.checked) {
radio.checked = false;
}
});
// ランダムに1つ選択
if(requiredRadios.length > 0) {
const randomIndex = Math.floor(Math.random() * requiredRadios.length);
requiredRadios[randomIndex].checked = true;
}
return;
}
// ドロップダウンをランダム入力する関数
function randomDropdown(){
// dropdownを探す
const dropdownSelector = KEY_WORDS.map(keyword =>
`select[name*="${keyword}"], select[aria-label*="${keyword}"]`
).join(', ');
const dropdowns = document.querySelectorAll(dropdownSelector);
// ドロップダウンメニューが存在するか確認
if (dropdowns.length === 0) {
return;
}
const requiredDropdowns = [];
dropdowns.forEach(dropdown => {
if (dropdown.required) {
requiredDropdowns.push(dropdown);
}
});
// ランダムに1つ選択
requiredDropdowns.forEach(dropdown => {
const options = dropdown.options;
const randomIndex = Math.floor(Math.random() * options.length);
dropdown.selectedIndex = randomIndex;
});
return;
}
// テキストインプットに規定入力を入力する関数
function fillTextInput(){
// テキストインプットを探す
const textInputSelector = KEY_WORDS.map(keyword =>
`input[type="text"][name*="${keyword}"], input[type="text"][aria-label*="${keyword}"]`
).join(', ');
const textInputs = document.querySelectorAll(textInputSelector);
// テキストインプットが存在するか確認
if (textInputs.length === 0) {
return;
}
const requiredTextInputs = [];
textInputs.forEach(textInput => {
if (textInput.required) {
requiredTextInputs.push(textInput);
}
});
// テキストインプットに指定された値を設定
requiredTextInputs.forEach(textInput => {
textInput.value = VALUE_TO_TEXT;
});
return;
}
// 保存するデータを探し、保存関数を呼び出す関数
function saveFormData(form) {
let dataToSave = {
url: window.location.href,
radios: null,
dropdowns: null,
textInputs: null,
selectedValue: null
};
// ラジオボタンを探す
const radioSelector = KEY_WORDS.map(keyword =>
`input[type="radio"][name*="${keyword}"], input[type="radio"][aria-label*="${keyword}"]`
).join(', ');
const radios = document.querySelectorAll(radioSelector);
// ラジオボタンの情報を得る
if (radios.length > 0) {
radios.forEach(radio => {
if (radio.checked) {
dataToSave.selectedValue = radio.value;
}
});
dataToSave.radios = Array.from(radios).map(radio => ({
id: radio.id || null,
value: radio.value,
checked: radio.checked
}));
}
// dropdownを探す
const dropdownSelector = KEY_WORDS.map(keyword =>
`select[name*="${keyword}"], select[aria-label*="${keyword}"]`
).join(', ');
const dropdowns = document.querySelectorAll(dropdownSelector);
// ドロップダウンの情報を得る
if (dropdowns.length > 0) {
dropdowns.forEach(dropdown => {
const options = dropdown.options;
const selectedOption = options[dropdown.selectedIndex];
dataToSave.selectedValue = selectedOption ? selectedOption.text : null;
});
dataToSave.dropdowns = Array.from(dropdowns).map(dropdown => ({
options: Array.from(dropdown.options).map(option => ({
value: option.value,
text: option.innerText,
selected: option.selected
}))
}));
}
// テキストインプットを探す
const textInputSelector = KEY_WORDS.map(keyword =>
`input[type="text"][name*="${keyword}"], input[type="text"][aria-label*="${keyword}"]`
).join(', ');
const textInputs = document.querySelectorAll(textInputSelector);
// テキストインプットの情報を得る
if (textInputs.length > 0) {
textInputs.forEach(textInput => {
dataToSave.selectedValue = textInput.value;
});
dataToSave.textInputs = Array.from(textInputs).map(textInput => ({
value: textInput.value
}));
}
if(radios.length > 0 || dropdowns.length > 0 || textInputs.length > 0) {
saveToStorage(dataToSave);
}
}
// 実際にデータをストレージに保存する関数
function saveToStorage(dataToSave) {
chrome.storage.local.get(['genderData'], function(result) {
// 以前のデータを取得
let genderData = result.genderData || [];
// 新しいデータを追加
genderData.push(dataToSave);
// ストレージに保存
chrome.storage.local.set({ genderData: genderData }, function() {
});
});
}