-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
372 lines (295 loc) · 12.5 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const info = "[condler] info: ";
const error = "[condler] error: ";
let element = {};
let newLayoutFlag = false;
// URLパースして値をsessionStorageCompareに渡す
function urlParse(url, radioButton) {
const params = new URLSearchParams(url);
if (params.has("s")) {
const s = params.get("s");
radioButton.each(function(index) {
const elementValue = $(this).val().replace("&sort=", "");
if (s == elementValue) {
element["button" + index] = true;
} else {
element["button" + index] = false;
}
});
} else {
for (let i = 0; i < radioButton.length; i++) {
element["button" + i] = false;
}
}
if (url.match(/p_6%3AAN1VRQENFRJN5/)) {
element.checkbox = true;
} else {
element.checkbox = false;
}
let price = url.match(/p_36%3A\d+00-\d+00/g);
if (price != null) {
price = price[0].replace("p_36%3A", "").split("-");
element.price0 = price[0].substring(0, price[0].length-2);
element.price1 = price[1].substring(0, price[1].length-2);
} else {
price = url.match(/p_36%3A\d+00-/g);
if (price != null) {
price = price[0].replace("p_36%3A", "").replace("00-", "");
element.price0 = price;
element.price1 = "";
} else {
price = url.match(/p_36%3A-\d+00/g);
if (price != null) {
price = price[0].replace("p_36%3A-", "");
element.price0 = "";
element.price1 = price.substring(0, price.length-2);
} else {
element.price0 = "";
element.price1 = "";
}
}
}
let percent = url.match(/p_8%3A\d+-\d+/g);
if (percent != null) {
percent = percent[0].replace("p_8%3A", "").split("-");
element.percent0 = percent[0];
element.percent1 = percent[1];
} else {
percent = url.match(/p_8%3A\d+-/g);
if (percent != null) {
percent = percent[0].replace("p_8%3A", "").replace("-", "");
element.percent0 = percent;
element.percent1 = "";
} else {
percent = url.match(/p_8%3A-\d+/g);
if (percent != null) {
percent = percent[0].replace("p_8%3A", "").replace("-", "");
element.percent0 = "";
element.percent1 = percent;
} else {
element.percent0 = "";
element.percent1 = "";
}
}
}
}
// 入力値分析、ページ遷移
// urlの部分 &high-price= と &pct-off= 分ける
function numberParse(type, low, high, replacedUrl) {
const pattern = /\D/g;
let param;
let highParam;
let lowParam;
if (type == 0) {
param = `&low-price=${low}&high-price=${high}`;
highParam = `&high-price=${high}`;
lowParam = `&low-price=${low}`;
} else if (type == 1) {
// 0入力しても削除されない
if (low == 0) {
low = "";
}
if (high == 0) {
high = "";
}
param = `&pct-off=${low}-${high}`;
highParam = `&pct-off=-${high}`;
lowParam = `&pct-off=${low}-`;
}
if (pattern.test(low)) {
//console.log(error + "low is NaN");
} else if (pattern.test(high)) {
//console.log(error + "high is NaN");
} else if (parseInt(low) > parseInt(high)) {
//console.log(error + "low > high");
} else if (low == "" && high == "") {
window.location.href = replacedUrl;
} else if (low == "") {
window.location.href = replacedUrl + highParam;
} else if (high == "") {
window.location.href = replacedUrl + lowParam;
} else {
window.location.href = location.href + param;
}
}
// 300ms後にページ遷移
function pageTransition(linkUrl) {
setTimeout(function() {
location.href = linkUrl;
}, 300);
}
function main(radioButton) {
// アドオン非表示設定の適用
browser.storage.local.get("isHideElem").then((result) => {
let isHideElem = result["isHideElem"];
for (const key of Object.keys(isHideElem)) {
if (isHideElem[key] == false) {
$("div#condler #" + key + "*").hide();
}
}
});
// url解析
if (!radioButton.length) {
//console.log(error + "get radioButton: failure");
}
urlParse(location.href, radioButton);
// 並べ替えの値復元、クリック時のイベントハンドラ
let allButtonFalseFlag = true;
radioButton.each(function(index) {
if (element["button" + index] == true) {
$(this).prop("checked", true);
allButtonFalseFlag = false;
}
$(this).on("click", function() {
window.location.href = location.href + $(this).val();
});
});
if (allButtonFalseFlag == true) {
$("div#condler li#sortRelevance input[type='radio'][name='radioButton']").prop("checked", true);
}
// Amazon公式出品の値復元
const checkbox = $("div#condler input[type='checkbox'][name='checkbox']");
if (element.checkbox == true) {
checkbox.prop("checked", true);
}
// Amazon公式出品クリック時のイベントハンドラ
const checkboxPrevState = checkbox.prop("checked");
checkbox.on("click", function() {
if (checkboxPrevState) {
//console.log(info + "checkbox: true->false");
// 出品者欄にAmazon.co.jpが存在するかどうか
if (document.getElementById("p_6/AN1VRQENFRJN5")) {
//console.log(info + "amazonCheckbox is exist");
// Amazon.co.jpチェック解除のリンクへ
if (newLayoutFlag) {
window.location.href = $("a#p_6\\/AN1VRQENFRJN5").attr("href");
} else {
window.location.href = $("div#s-refinements li#p_6\\/AN1VRQENFRJN5 a").attr("href");
}
} else {
// URLから関連要素削除したリンクへ
window.location.href = location.href.replace($(this).val(), "").replace("p_6%3AAN1VRQENFRJN5", "");
}
} else {
//console.log(info + "checkbox: false->true");
window.location.href = location.href + $(this).val();
}
});
// 値段絞り込み値を復元
const lowPrice = $("div#condler li#priceInput>input#low-price");
const highPrice = $("div#condler li#priceInput>input#high-price");
if (element.price0) {
lowPrice.val(element.price0);
}
if (element.price1) {
highPrice.val(element.price1);
}
// 値引き率絞り込み値を復元
const lowPercent = $("div#condler li#percentOffInput>input#low-percent");
const highPercent = $("div#condler li#percentOffInput>input#high-percent");
if (element.percent0) {
lowPercent.val(element.percent0);
}
if (element.percent1) {
highPercent.val(element.percent1);
}
// 値段絞り込みのGoボタンクリック時のイベントハンドラ
$("div#condler li#priceInput input.a-button-input").on("click", function() {
const lowPriceVal = lowPrice.val();
//console.log(info + "lowPrice: " + lowPriceVal);
const highPriceVal = highPrice.val();
//console.log(info + "highPrice: " + highPriceVal);
// abcが混ざってる -> 何もしない
// 空 -> 削除
// 0 -> そのまま普通に処理
// p_36%3A[PRICE]00-[PRICE]00,
const replacedUrl = location.href.replace(/p_36%3A\d+00-\d+00/g, "").replace(/p_36%3A-?\d+00-?/g, "");
numberParse(0, lowPriceVal, highPriceVal, replacedUrl);
});
// 値引き率絞り込みのGoボタンクリック時のイベントハンドラ
$("div#condler li#percentOffInput input.a-button-input").on("click", function() {
const lowPercentVal = lowPercent.val();
//console.log(info + "lowPercent: " + lowPercentVal);
const highPercentVal = highPercent.val();
//console.log(info + "highPercent: " + highPercentVal);
// p_8%3A[PERCENT]-[PERCENT]
const replacedUrl = location.href.replace(/p_8%3A\d+-\d+/g, "").replace(/p_8%3A-?\d+-?/g, "");
numberParse(1, lowPercentVal, highPercentVal, replacedUrl);
});
// div#dropdown-content-s-all-filters配下のaタグに対して遅延.load()
$("div#dropdown-content-s-all-filters span.a-declarative>a[href^='\\/s?']").on("click", function() {
setTimeout(() => {
const timer = setInterval(() => {
if (!document.getElementById("#condler")) {
clearInterval(timer);
let div = document.createElement("div");
div.id = "condler";
div.className = "a-section a-spacing-none";
$("div#dropdown-content-s-all-filters>div:first").before(div);
$("div#condler").load(browser.runtime.getURL("search-options-dom.html"), loadCallback());
}
}, 100);
}, 1000);
});
// カテゴリー選択時「すべてのカテゴリー」を押したときのイベントハンドラ
$("div#departments li#n>span.a-list-item").on("click", function(e) {
e.preventDefault();
//console.log(info + "all category clicked");
// チェックボックス
let linkUrl = $(this).closest("a").attr("href");
if (linkUrl === undefined) {
// その他普通のテキストなど
linkUrl = $(this).children("a").attr("href");
}
pageTransition(linkUrl);
});
// 検索画面の左側の条件欄リンククリック時のイベントハンドラ
// 「すべてのカテゴリー」とカテゴリー展開テキストは除外
$("div#s-refinements span.a-list-item").not("div#departments li#n>span.a-list-item, div#s-refinements li.a-spacing-micro>span.a-list-item:has('a.a-expander-header.a-declarative.a-expander-extend-header.s-expander-text')").on("click", function(e) {
// デフォルトのの動作をキャンセル
e.preventDefault();
// 他のプログラムのイベントリスナーを停止する
// たぶんチェックボックスだけイベントリスナーが設置してある
e.stopImmediatePropagation();
//console.log(info + "s-refinements clicked");
let linkUrl = $(this).closest("a").attr("href");
if (linkUrl === undefined) {
linkUrl = $(this).children("a").attr("href");
}
pageTransition(linkUrl);
});
}
// 100ms間隔で5回、radioButtonの取得を試みる
// 読み込みタイミングに違いがあるため
function loadCallback() {
const timer = setInterval(() => {
const radioButton = $("div#condler input[type='radio'][name='radioButton']");
if (radioButton.length) {
//console.log(info + "get radioButton succeed");
clearInterval(timer);
main(radioButton);
}
}, 100, 5);
}
$(window).on("load", function() {
// ドロップダウン並べ替え非表示
$("span.rush-component div.sg-col-6-of-20.sg-col.sg-col-6-of-16.sg-col-6-of-12 span.a-dropdown-container").hide();
// アドオン用DOM要素作成
let div = document.createElement("div");
div.id = "condler";
div.className = "a-section a-spacing-none";
// 100ms間隔で5回、targetNodeの取得を試みる
const timer = setInterval(() => {
const primaryTarget = $("div#s-refinements>div>div:first");
const secondaryTarget = $("div#dropdown-content-s-all-filters>div:first");
if (primaryTarget.length) {
clearInterval(timer);
primaryTarget.before(div);
$("div#condler").load(browser.runtime.getURL("search-options-dom.html"), loadCallback());
} else if (secondaryTarget.length) {
clearInterval(timer);
newLayoutFlag = true;
secondaryTarget.before(div);
$("div#condler").load(browser.runtime.getURL("search-options-dom.html"), loadCallback());
}
}, 100, 5);
});