-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (137 loc) · 5.77 KB
/
index.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
(async function() {
class TrendyolSellerAPI {
#apiEndpoints = {
follow: 'https://public.trendyol.com/discovery-sellerstore-mobilewebgw-service/api/follow/',
storeUrl: 'https://public-sdc.trendyol.com/discovery-sellerstore-mobilewebgw-service/api/seller-store/getStoreUrl',
availableCoupons: 'https://public.trendyol.com/discovery-sellerstore-mobilewebgw-service/api/coupon/getAvailableCoupons',
profile: 'https://public-mdc.m.trendyol.com/discovery-sellerstore-mobilewebgw-service/seller-profile/magaza/profil',
};
constructor({ sellerId }) {
this.sellerId = sellerId;
}
_fetchData(url) {
return fetch(url).then(response => response.json());
}
getFollowers() {
const url = `${this.#apiEndpoints.follow}?sellerId=${this.sellerId}`;
return this._fetchData(url);
}
getStoreUrl() {
const url = `${this.#apiEndpoints.storeUrl}?sellerId=${this.sellerId}`;
return this._fetchData(url);
}
getAvailableCoupons() {
const url = `${this.#apiEndpoints.availableCoupons}?sellerId=${this.sellerId}`;
return this._fetchData(url);
}
getProfile() {
const url = `${this.#apiEndpoints.profile}/0-m-${this.sellerId}?__renderMode=stream&storefrontId=1&channelId=1`;
return this._fetchData(url).then(data => {
const trueUrl = data['$headers'].Location;
return this._fetchData(`${this.#apiEndpoints.profile}/../../${trueUrl}&__renderMode=stream&storefrontId=1`)
});
}
}
function createSellerWidget({ name, score, image, followers, link }) {
const fragment = document.createDocumentFragment();
const mainEl = document.createElement('div');
mainEl.classList.add('seller-widget');
const imageEl = document.createElement('img');
imageEl.src = image;
imageEl.alt = name;
mainEl.appendChild(imageEl);
const nameEl = document.createElement('h3');
nameEl.textContent = name;
mainEl.appendChild(nameEl);
const scoreEl = document.createElement('span');
scoreEl.textContent = `Mağaza Puanı: ${score}`;
mainEl.appendChild(scoreEl);
const followersEl = document.createElement('span');
followersEl.textContent = `Takipçi: ${followers}`;
mainEl.appendChild(followersEl);
const linkEl = document.createElement('a');
linkEl.href = link;
linkEl.textContent = 'Mağaza Profili';
mainEl.appendChild(linkEl);
fragment.appendChild(mainEl);
return fragment;
}
function createSellerWidgetCSS() {
let sellerWidgetElCSS = document.createElement('style');
sellerWidgetElCSS.innerHTML = `
.seller-widget {
border: 1px solid #D1D1D1;
padding: 15px;
width: 250px;
height: 250px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: #FFFFFF;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
border-radius: 8px;
font-family: Arial, sans-serif;
overflow: hidden;
position: relative;
margin: 10px;
}
.seller-widget img {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 50%;
margin-bottom: 10px;
}
.seller-widget h3 {
margin: 5px 0;
font-size: 1.2em;
color: #333333;
text-align: center;
}
.seller-widget span {
color: #555555;
font-size: 0.9em;
margin: 2px 0;
text-align: center;
}
.seller-widget a {
background-color: #FF6000;
color: white;
padding: 6px 12px;
text-decoration: none;
border-radius: 4px;
font-size: 0.9em;
position: absolute;
bottom: 15px;
transition: background-color 0.3s;
}
.seller-widget a:hover {
background-color: #CC4D00;
}`;
return sellerWidgetElCSS;
}
document.head.appendChild(createSellerWidgetCSS());
const sellers = document.querySelectorAll('.trendyol-seller-profile');
for (const seller of sellers) {
const sellerId = seller.getAttribute('seller-id');
const trendyolSellerAPI = new TrendyolSellerAPI({ sellerId });
const followers = await trendyolSellerAPI.getFollowers();
const storeUrl = await trendyolSellerAPI.getStoreUrl();
const profile = await trendyolSellerAPI.getProfile();
let profileDocument = new DOMParser().parseFromString(profile.main, 'text/html');
let profileName = profileDocument.querySelector('.seller-info__wrapper__info-box__name').textContent;
let profileScore = profileDocument.querySelector('.score-actual').textContent;
let profileImage = profileDocument.querySelector('.seller-icon img').getAttribute('src');
let followerCount = followers.text;
let profileLink = storeUrl;
let sellerWidget = createSellerWidget({
name: profileName,
score: profileScore,
image: profileImage,
followers: followerCount,
link: profileLink
});
seller.appendChild(sellerWidget);
}
})();