This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_script.js
52 lines (47 loc) · 1.69 KB
/
inject_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
chrome.storage.sync.get(['cityId', 'storeName'], function(vals) {
findWines(vals['cityId'], vals['storeName']);
});
function findWines(cityId, storeName) {
var links = document.querySelectorAll('.viini_wine .related a:first-child:not(.wineStatus)');
if (links.length==0) {links=document.querySelectorAll('a.wineCode');}
for (var i=0;i<links.length;i++) {
var link = links[i];
link.className += ' wineCode';
var text = link.innerText;
var code = text.substr(0,text.indexOf(','));
var elId = 'viini_' + code;
var el = document.getElementById(elId);
if (!el) {
el = document.createElement('a');
el.className = 'wineStatus';
el.id = 'viini_' + code;
el.style.lineHeight = '200%';
link.parentElement.insertBefore(el, link);
}
el.innerText = 'Saatavuutta haetaan';
fetchAvailability(code);
};
function findStoreData(data) {
for (var i=0; i<data.length; i++) {
if (data[i].StoreName == storeName) { return data[i]; }
}
}
function fetchAvailability(code) {
var xmlHttp = new XMLHttpRequest();
var url = 'http://www.alko.fi/api/product/Availability?productId=' + code + '&cityId=' + cityId + '&language=fi';
xmlHttp.onload = function() {
var link = document.getElementById('viini_' + code);
var storeData = findStoreData(JSON.parse(this.responseText));
var bottleCount = 0;
if (storeData && storeData.StoreName == storeName) {
bottleCount = storeData.Amount;
link.innerText = storeName + ': ' + bottleCount + ' kpl.';
}
else {
link.innerText = 'Jokin män pielee.';
}
};
xmlHttp.open('GET', url);
xmlHttp.send(null);
}
}