-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
66 lines (59 loc) · 2.26 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
chrome.storage.local.get(
{
url: "",
key: "",
secret: "",
},
function (config) {
let KEY = config.key;
let SECRET = config.secret;
let WORDPRESS_URL = config.url;
// wait until page is loaded (element exists) and then call replaceImages method with all data needed
let checkExist = setInterval(() => {
let order_id_element = document.body.querySelector(".panel-body > .form-group > div > label");
if (order_id_element && 0 != +order_id_element.innerText) {
clearInterval(checkExist);
replaceImages(+order_id_element.innerText, WORDPRESS_URL, KEY, SECRET);
}
}, 100); // check every 100ms
}
);
function replaceImages(order_id, WORDPRESS_URL, KEY, SECRET) {
let product_url = `${WORDPRESS_URL}/wp-json/wc/v3/orders/${order_id}?consumer_key=${KEY}&consumer_secret=${SECRET}`;
let items = document.body
.querySelector(".table-striped tbody")
.querySelectorAll("tr");
fetch(product_url)
.then((res) => res.json())
.then((out) => {
let products = out.line_items;
for (let product in products) {
let meta_data = products[product].meta_data;
let zakeke_data = meta_data.find((obj) => {
return obj.key === "zakeke_data";
});
if (zakeke_data) {
let previews = zakeke_data.value.previews;
let imageString = "";
for (let pre in previews) {
if (+pre !== 0)
imageString +=
'<img src="' +
previews[pre].url +
'" height="200px" width="200px"/>';
}
if (imageString !== "") {
items[product].querySelectorAll(
"td"
)[1].innerHTML = imageString;
}
}
}
})
.catch((err) => {
alert(
"Failed to fetch data. Did you configure the plugin correctly?"
);
console.error(err);
});
}