-
Notifications
You must be signed in to change notification settings - Fork 3
/
ikea.js
94 lines (85 loc) · 3.29 KB
/
ikea.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
function bookmarklet(countryList) {
const priceElClass = "pip-temp-price-module__current-price";
function createPriceEl(href, cnt, el) {
fetch(href).then(response => response.text()).then(data => {
var doc = new DOMParser().parseFromString(data, "text/html");
var price = doc.evaluate("//*[contains(@class, '"+priceElClass+"')]", doc, null, 4, null).iterateNext();
var text = cnt.substr(0, 2) + ': ' + (price === null ? "-" : price.firstChild.textContent);
var priceDiv = document.createElement("div");
var priceA = document.createElement("a");
priceA.setAttribute("href", href);
priceA.appendChild(document.createTextNode(text));
priceDiv.appendChild(priceA);
el.appendChild(priceDiv);
});
}
if (/ikea\.com$/.test(window.location.hostname)) {
var url = window.location.pathname;
var cur = url.slice(1, 6);
var countries = countryList.split(",").filter(el => el != cur);
if (/\/shoppingcart/.test(url)) {
[...document.querySelectorAll("div[itemscope]")].map(function(el) {
var urlProd = el.querySelector("a").href;
var priceEl = el.querySelector("div[class^='price_total']");
countries.forEach(function(cnt) {
var href = urlProd.replace(cur, cnt);
createPriceEl(href, cnt, priceEl.parentElement);
});
});
} else if (/\/favourites/.test(url)) {
[...document.querySelectorAll(".list-ingka-price-module")].map(function(el) {
var urlProd = el.querySelector("a").href;
var priceEl = el.querySelector(".list-ingka-price__sr-text");
countries.forEach(function(cnt) {
var href = urlProd.replace(cur, cnt);
createPriceEl(href, cnt, priceEl.parentElement);
});
});
} else {
var urlProd = url.slice(6, );
countries.forEach(function(cnt) {
var href = "https://www.ikea.com/" + cnt + urlProd;
createPriceEl(href, cnt, document.querySelector("."+priceElClass).parentElement.parentElement);
})
}
}
};
var chosen = document.getElementsByClassName('selected');
var button = document.getElementById('bookmarklet');
function checkChosen() {
if (chosen.length == 0) {
button.style.backgroundColor = 'lightgray';
} else {
button.style.backgroundColor = '#0051ba';
}
}
checkChosen();
function updateButton() {
// modify actual js code for the bookmarklet
if (chosen.length > 0) {
var text = [...chosen].map((el) => el.firstChild.href.slice(-6, -1)).join(",");
button.href = encodeURI("javascript:("+bookmarklet+")(\""+text+"\")");
} else {
button.href = "";
}
}
function refreshScript() {
checkChosen();
var text = " (" + [...chosen].map((el) => el.firstChild.href.slice(-6, -4)).join(", ") + ")";
button.innerText = "check price" + (chosen.length > 0 ? text : "");
updateButton();
}
function toggleSelection(e) {
e.classList.toggle('selected');
refreshScript();
}
var items = document.getElementsByTagName('li');
[...items].forEach(elem => elem.addEventListener("click", function(el) {
el.preventDefault();
toggleSelection(this);
}));
var regions = document.getElementsByTagName('h3');
[...regions].forEach(elem => elem.addEventListener("click", function(el) {
el.preventDefault();
[...this.nextElementSibling.children].forEach(li => toggleSelection(li));
}));