-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.js
More file actions
169 lines (148 loc) · 3.76 KB
/
Copy pathintegration.js
File metadata and controls
169 lines (148 loc) · 3.76 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
function createEmbeddedElement(top = 0) {
const contElements = document.getElementsByClassName("_mpsEx___mainConteiner_");
if (contElements && contElements.length > 0) {
for (let i = 0; i < contElements.length; i++) {
contElements[i].remove();
}
}
const conteiner = document.createElement("div");
conteiner.id = "_mpsEx___mainConteiner";
conteiner.className = "_mpsEx___mainConteiner_";
Object.assign(conteiner.style, {
display: "flex",
alignItems: "center",
gap: "8px",
position: "absolute",
top: `${top}px`,
left: "100%",
marginLeft: "12px",
background: "rgba(0, 0, 0, 0)",
});
const limitInput = document.createElement("input");
Object.assign(limitInput, {
type: "number",
name: "-",
id: "_mpsEx___limitInput",
value: "1",
placeholder: "max",
size: "5",
max: "99999",
min: "0",
});
Object.assign(limitInput.style, {
padding: "2px 4px",
border: "solid 1px #929292",
borderRadius: "4px",
height: "26px",
maxWidth: "56px",
fontSize: "12px",
});
conteiner.appendChild(limitInput);
const image = document.createElement("img");
Object.assign(image, {
id: "_mpsEx___submitBtn",
src: chrome.runtime.getURL("icons/icon128.png"),
width: "26",
height: "26",
});
image.style.cursor = "pointer";
image.style.transition = "transform 0.2s ease";
image.style.opacity = "0.85";
conteiner.appendChild(image);
function startJumpAnimation() {
image.style.animation = "jump 0.4s infinite alternate";
}
function stopJumpAnimation() {
image.style.animation = "";
}
const styleTag = document.createElement("style");
styleTag.textContent = `
@keyframes jump {
0% { transform: translateY(0); }
100% { transform: translateY(-5px); }
}
#_mpsEx___submitBtn:hover {
transform: scale(1.1);
opacity: 1 !important;
}
#_mpsEx___submitBtn[data-busy="true"] {
animation: jump 0.4s infinite alternate !important;
opacity: 1 !important;
}
`;
document.head.appendChild(styleTag);
image.addEventListener("click", async () => {
if (image.dataset.busy === "true") return;
image.dataset.busy = "true";
image.style.cursor = "not-allowed";
startJumpAnimation();
await new Promise((resolve) => {
chrome.runtime.sendMessage({
action: "parseCatalog",
data: {
query: document.URL,
limit: parseInt(limitInput.value || "0", 10),
open: true,
return: false,
active: true,
}
}, (_) => resolve());
});
stopJumpAnimation();
image.dataset.busy = "false";
image.style.cursor = "pointer";
limitInput.value = "1";
});
return conteiner;
}
async function setupOzon() {
try {
const container = document.querySelector('div[data-widget="searchResultsSort"]');
if (!container) {
return;
}
const child = container.firstElementChild;
if (!child) return;
child.style.position ||= 'relative';
child.appendChild(createEmbeddedElement(9));
} catch (_) { }
}
async function setupWildberries(i = 0) {
const selectors = [
'div.catalog-title-wrap',
'div.searching-results__inner'
];
if (i > 3) {
return;
}
await new Promise(r => setTimeout(r, 500));
try {
for (const selector of selectors) {
const container = document.querySelector(selector);
if (!container) {
continue;
}
if (container.childElementCount <= 1) {
await setupWildberries(i++);
}
const child = container.lastElementChild;
if (!child) {
await setupWildberries(i++);
};
child.style.position ||= 'relative';
child.appendChild(createEmbeddedElement(-4));
return;
}
await setupWildberries(i++);
} catch (_) { }
}
async function main() {
const url = document.URL;
await new Promise(r => setTimeout(r, 100));
if (url.startsWith("https://www.ozon.ru/")) {
await setupOzon();
} else if (url.startsWith("https://www.wildberries.ru/")) {
await setupWildberries();
}
}
main().catch(console.error);