-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
163 lines (138 loc) · 4.45 KB
/
content_script.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
const waitAndExecute = (condition, callback) => {
let timer = setInterval(() => {
if (condition()) {
clearInterval(timer);
callback();
}
}, 100);
}
const uploadImages = (element, files) => {
const extensionMap = {
"image/png": "png",
"image/tiff": "tif",
"image/vnd.wap.wbmp": "wbmp",
"image/x-icon": "ico",
"image/x-jng": "jng",
"image/x-ms-bmp": "bmp",
"image/svg+xml": "svg",
"image/webp": "webp",
"image/gif": "gif",
"image/jpeg": "jpeg",
};
Promise.all(files.map((file) => (
fetch(file).then((response) => response.blob())
))).then((blobs) => {
const dt = new DataTransfer();
for (blob of blobs) {
dt.items.add(new File([blob], `image.${extensionMap[blob.type]}`, {type: blob.type}));
}
element.files = dt.files;
element.dispatchEvent(new Event('change', { bubbles: true }));
});
}
const updateInput = (element, value) => {
Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set.call(element, value);
element.dispatchEvent(new Event('change', { bubbles: true }));
};
const updateTextArea = (element, value) => {
Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set.call(element, value);
element.dispatchEvent(new Event('change', { bubbles: true }));
};
const selectDropdown = (element, value) => {
let checkCleanState = false;
waitAndExecute(
() => document.getElementsByClassName("__fb-light-mode").length === 1,
() => {
element.dispatchEvent(new Event('click', { bubbles: true }));
checkCleanState = true;
}
)
waitAndExecute(
() => (
document.getElementsByClassName("__fb-light-mode").length === 2 &&
checkCleanState
),
() => {
for (element of document.getElementsByClassName("__fb-light-mode")) {
if (element.tagName === 'DIV') {
for (span of element.getElementsByTagName("span")) {
if (span.textContent === value) {
span.dispatchEvent(new Event('click', { bubbles: true }));
}
}
}
}
}
)
}
const selectLocation = (input, value) => {
let checkCleanState = false;
waitAndExecute(
() => document.getElementsByClassName("__fb-light-mode").length === 1,
() => {
updateInput(input, value);
checkCleanState = true;
}
)
waitAndExecute(
() => (
document.getElementsByClassName("__fb-light-mode").length === 2 &&
checkCleanState
),
() => {
for (element of document.getElementsByClassName("__fb-light-mode")) {
if (element.tagName === 'DIV') {
waitAndExecute(() => {
return element.getElementsByTagName("span").length > 0;
}, () => {
for (span of element.getElementsByTagName("span")) {
if (span.textContent === value) {
span.dispatchEvent(new Event('click', { bubbles: true }));
}
}
});
}
}
}
)
}
window.onload = function () {
let spans = document.getElementsByTagName("span");
let imageElement = document.getElementsByTagName('input')[3];
let titleElement, priceElement,
descriptionElement, skuElement, categoryElement,
conditionElement, locationElement;
for (span of spans) {
if (span.textContent === 'Title' && !titleElement) {
titleElement = span.nextSibling;
}
if (span.textContent === 'Price' && !priceElement) {
priceElement = span.nextSibling;
}
if (span.textContent === 'Description' && !descriptionElement) {
descriptionElement = span.nextSibling;
}
if (span.textContent === 'SKU' && !skuElement) {
skuElement = span.nextSibling;
}
if (span.textContent === 'Category' && !categoryElement) {
categoryElement = span;
}
if (span.textContent === 'Condition' && !conditionElement) {
conditionElement = span;
}
if (span.textContent === 'Location' && !locationElement) {
locationElement = span.nextSibling;
}
}
chrome.runtime.sendMessage({"message": "ready"}, (response) => {
updateInput(titleElement, response.title);
updateInput(priceElement, response.price);
updateTextArea(descriptionElement, response.description);
updateInput(skuElement, response.sku);
selectDropdown(categoryElement, response.category);
selectDropdown(conditionElement, response.condition);
selectLocation(locationElement, response.location);
uploadImages(imageElement, response.images);
});
}