forked from theirnameissam/browser-demo
-
Notifications
You must be signed in to change notification settings - Fork 37
/
renderer.js
149 lines (135 loc) · 4.26 KB
/
renderer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
var ById = function (id) {
return document.getElementById(id);
}
var jsonfile = require('jsonfile');
var favicon = require('favicon-getter').default;
var path = require('path');
var uuid = require('uuid');
var bookmarks = path.join(__dirname, 'bookmarks.json');
var back = ById('back'),
forward = ById('forward'),
refresh = ById('refresh'),
omni = ById('url'),
dev = ById('console'),
fave = ById('fave'),
list = ById('list'),
popup = ById('fave-popup'),
view = ById('view');
function reloadView () {
view.reload();
}
function backView () {
view.goBack();
}
function forwardView () {
view.goForward();
}
function updateURL (event) {
if (event.keyCode === 13) {
omni.blur();
let val = omni.value;
let https = val.slice(0, 8).toLowerCase();
let http = val.slice(0, 7).toLowerCase();
if (https === 'https://') {
view.loadURL(val);
} else if (http === 'http://') {
view.loadURL(val);
} else {
view.loadURL('http://'+ val);
}
}
}
var Bookmark = function (id, url, faviconUrl, title) {
this.id = id;
this.url = url;
this.icon = faviconUrl;
this.title = title;
}
Bookmark.prototype.ELEMENT = function () {
var a_tag = document.createElement('a');
a_tag.href = this.url;
a_tag.className = 'link';
a_tag.textContent = this.title;
var favimage = document.createElement('img');
favimage.src = this.icon;
favimage.className = 'favicon';
a_tag.insertBefore(favimage, a_tag.childNodes[0]);
return a_tag;
}
function addBookmark () {
let url = view.src;
let title = view.getTitle();
favicon(url).then(function(fav) {
let book = new Bookmark(uuid.v1(), url, fav, title);
jsonfile.readFile(bookmarks, function(err, curr) {
curr.push(book);
jsonfile.writeFile(bookmarks, curr, function (err) {
})
})
})
}
function openPopUp (event) {
let state = popup.getAttribute('data-state');
if (state === 'closed') {
popup.innerHTML = '';
jsonfile.readFile(bookmarks, function(err, obj) {
if(obj.length !== 0) {
for (var i = 0; i < obj.length; i++) {
let url = obj[i].url;
let icon = obj[i].icon;
let id = obj[i].id;
let title = obj[i].title;
let bookmark = new Bookmark(id, url, icon, title);
let el = bookmark.ELEMENT();
popup.appendChild(el);
}
}
popup.style.display = 'block';
popup.setAttribute('data-state', 'open');
});
} else {
popup.style.display = 'none';
popup.setAttribute('data-state', 'closed');
}
}
function handleUrl (event) {
if (event.target.className === 'link') {
event.preventDefault();
view.loadURL(event.target.href);
} else if (event.target.className === 'favicon') {
event.preventDefault();
view.loadURL(event.target.parentElement.href);
}
}
function handleDevtools () {
if (view.isDevToolsOpened()) {
view.closeDevTools();
} else {
view.openDevTools();
}
}
function updateNav (event) {
omni.value = view.src;
}
refresh.addEventListener('click', reloadView);
back.addEventListener('click', backView);
forward.addEventListener('click', forwardView);
omni.addEventListener('keydown', updateURL);
fave.addEventListener('click', addBookmark);
list.addEventListener('click', openPopUp);
popup.addEventListener('click', handleUrl);
dev.addEventListener('click', handleDevtools);
view.addEventListener('did-finish-load', updateNav);
// https://github.com/hokein/electron-sample-apps/blob/master/webview/browser/browser.js#L5
// To Do add dev tools open ✔️
// update url ✔️
// add bookmark by pressing button ✔️
// load all bookmarks when list is clicked ✔️
// To Do / Continue
// Feedback when loading
// Feedback with favorite icon to show that bookmark is not-added/added/already-added
// Tabs !:@
// Option to remove bookmarks.