-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.js
71 lines (54 loc) · 1.97 KB
/
widgets.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
function createWidget(imgUrl, url, title) {
let newWidgetDiv = document.createElement("div");
newWidgetDiv.setAttribute("title", title);
let widgetContainer = document.getElementById("widgets_container");
widgetContainer.insertBefore(newWidgetDiv, widgetContainer.lastElementChild);
let newWidgetA = document.createElement("a");
newWidgetA.setAttribute("href", url);
newWidgetDiv.append(newWidgetA);
let newWidgetImg = document.createElement("img");
newWidgetImg.setAttribute("src", imgUrl);
newWidgetA.append(newWidgetImg);
let newWidgetP = document.createElement("p");
newWidgetP.innerText = title;
newWidgetA.append(newWidgetP);
}
const parseCookie = str =>
str
.split(';')
.map(v => v.split('='))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
let widgetForm = document.getElementById("widget_form");
let widgetImg = document.getElementById("widget_img");
let widgetUrl = document.getElementById("widget_url");
let widgetTitle = document.getElementById("widget_title");
let currentCookies = parseCookie(document.cookie)["widgets"];
let widgets;
if (currentCookies != undefined) {
widgets = JSON.parse(currentCookies);
for (let widget of widgets) {
createWidget(widget[0], widget[1], widget[2]);
}
} else {
currentCookies = JSON.stringify([[]]);
widgets = [];
}
widgetForm.onsubmit = function() {
// close the modal:
document.getElementById("widget_modal").style.display = "none";
let url = widgetUrl.value;
let title = widgetTitle.value;
let pathArray = url.split( '/' );
let protocol = pathArray[0];
let host = pathArray[2];
let imgUrl = protocol + '//' + host + "/favicon.ico";
createWidget(imgUrl, url, title);
// save widget to the cookies:
widgets.push([imgUrl, url, title]);
console.log(widgets);
document.cookie = "widgets=" + JSON.stringify(widgets);
return false;
}