-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (59 loc) · 2.37 KB
/
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
function addM2EditButton() {
const rows = document.querySelectorAll('div[data-index="associated"] .admin__dynamic-rows.data-grid tbody tr');
const currentUrl = window.location.href;
const url = new URL(currentUrl);
rows.forEach(function(row) {
m2FoundAssociatedProducts = true;
const idCell = row.querySelector('span[data-index="id"]');
if (idCell) {
const id = idCell.innerHTML;
let newUrl = url.toString().replace(/\/id\/\d+/, `/id/${id}`);
const updatedUrl = newUrl.toString();
const productNameCell = row.querySelector('[data-index="name"]');
if (productNameCell.querySelector('span[data-index="name"]')) {
const productName = productNameCell.querySelector('span[data-index="name"]').innerHTML;
const link = document.createElement('a');
link.textContent = productName;
link.href = updatedUrl;
link.target = '_blank';
productNameCell.innerHTML = '';
productNameCell.appendChild(link);
return true;
}
}
});
return false;
}
function tryToAddURL() {
// Try to add the edit url
const result = addM2EditButton();
// If was not possible to add the button, check if spinner is invisible, then try again
if (!result) {
const spinnerVisibility = window.getComputedStyle(document.querySelector('.admin__form-loading-mask')).display !== 'none';
if (!spinnerVisibility) {
// Only applies if page has associated grouped products
if (document.querySelector('div[data-index="associated"]')) {
const rows = document.querySelectorAll('div[data-index="associated"] .admin__dynamic-rows.data-grid tbody tr');
if (rows && rows.length > 0) {
addM2EditButton();
}
return true;
}
}
}
return false;
}
function init() {
let tries = 0;
const limit = 5;
const checkIfPageIsLoaded = setInterval(function() {
tries++;
result = tryToAddURL();
// try 5 times, or if screenloader is gone and there is no secction of associated grouped products
if (result || limit === tries) {
clearInterval(checkIfPageIsLoaded);
}
}, 2000);
}
// Init
init();