-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
399 lines (306 loc) · 13.2 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
let $gallery = document.querySelector('.gallery');
let $searchTextBox = document.querySelector('#search');
let $modal = document.querySelector('#settings');
let modalSettings = new bootstrap.Modal($modal, { focus: true });
let lightBoxList = [];
let Config = {
State: '',
nextIndex: 0,
curSearch: '',
newSearch: '',
inSearchFunc: false,
lightBoxOpen: false,
keyMap: { },
appSettings: { }
};
//setTimeout(() => Config.newSearch = 'brannigans big book of war', 1000);
document.addEventListener('DOMContentLoaded', async () => {
await runSetup();
const searchString = new URL(window.location.href).searchParams.get('searchString');
if (!Misc.IsNullOrWhitespace(searchString) ) {
console.log(`Found search string: ${searchString}`);
$searchTextBox.value = searchString;
Config.newSearch = $searchTextBox.value.trim();
$searchTextBox.focus();
}
});
function setupObserver() {
let observer = new MutationObserver(function(mutations) {
// detect any new img elements that don't have load attribute
let $images = Array.from( document.querySelectorAll('img') ).filter(i => !i.hasAttribute('data-onload') );
for(let $img of $images) {
console.log('Connecting event listener to img element');
$img.addEventListener('load', (event) => {
let target = event.target;
target.setAttribute('data-originalwidth', target.naturalWidth);
target.setAttribute('data-originalheight', target.naturalHeight);
});
$img.setAttribute('data-onload', 'true');
}
});
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});
}
async function setupSettings() {
let settings = await window.ipcRenderer.invoke('load-settings');
Config.State = 'Ready';
Config.appSettings = settings;
document.querySelector('body').style.backgroundColor = Config.appSettings.COLOR_SETTINGS;
}
function setupHandlers() {
document.querySelector('#load-settings').addEventListener('click', (event) => {
document.querySelector('#cse_clientid').value = Config.appSettings.CSE_ID;
document.querySelector('#cse_key').value = Config.appSettings.CSE_KEY;
document.querySelector('#settings_color').value = Config.appSettings.COLOR_SETTINGS;
document.querySelector('#banned_domains').value = Config.appSettings.BANNED_DOMAINS.join('\n');
document.querySelector('#no_ai').checked = Config.appSettings.NO_AI;
modalSettings.show();
});
document.querySelector('#save-settings').addEventListener('click', (event) => {
Config.appSettings.CSE_ID = document.querySelector('#cse_clientid').value;
Config.appSettings.CSE_KEY = document.querySelector('#cse_key').value;
Config.appSettings.COLOR_SETTINGS = document.querySelector('#settings_color').value;
Config.appSettings.BANNED_DOMAINS = document.querySelector('#banned_domains')?.value.split('\n')?.map(v => v.trim()) ?? [];
Config.appSettings.NO_AI = document.querySelector('#no_ai').checked;
document.querySelector('body').style.backgroundColor = Config.appSettings.COLOR_SETTINGS;
window.ipcRenderer.send('save-settings', Config.appSettings);
modalSettings.hide();
});
document.getElementById('fetch-images').addEventListener('click', async () => search());
document.addEventListener('keydown', event => {
if(!(event.key in Config.keyMap)) {
Config.keyMap[event.key] = true;
if (event.key === 'Escape') {
if(!Config.lightBoxOpen)
window.ipcRenderer.send('close');
else {
Config.lightBoxOpen = false;
}
}
}
});
document.addEventListener('keyup', (event) => {
delete Config.keyMap[event.key];
});
let fnInputChanged = Misc.debounce(() => {
Config.newSearch = $searchTextBox.value.trim();
}, 1500);
$searchTextBox.addEventListener('input', fnInputChanged);
$searchTextBox.addEventListener('keyup', (event) => {
if(event.key === 'Enter') {
//Config.State = 'Search';
Config.newSearch = $searchTextBox.value.trim();
console.log(`Searching for ${Config.newSearch}`);
}
});
document.addEventListener('resize', () => {
});
$searchTextBox.focus();
setTimeout(LogicLoop, 1000);
}
async function runSetup() {
await setupSettings();
setupObserver();
setupHandlers();
}
let lightBox = undefined;
async function copyToClipboard(event) {
console.info('Copying to clipboard', event);
let $img;
if(event instanceof HTMLImageElement)
$img = event;
else
$img = event.target.closest('.ginner-container').querySelector('img');
// this img is completely loaded, but we don't have the actual qualifications of this file
let canvas = document.createElement('canvas');
canvas.height = parseInt( $img.getAttribute('data-originalheight') );
canvas.width = parseInt( $img.getAttribute('data-originalwidth') );
let context = canvas.getContext('2d');
context.drawImage($img, 0, 0);
try {
let blob = await new Promise(resolve => canvas.toBlob(resolve));
await navigator.clipboard.write([
new ClipboardItem({
'image/png': blob
})
]);
if(!(event instanceof HTMLImageElement))
event.target.textContent = 'Copied';
$img.classList.add('copied-image');
let $checkbox = document.querySelector('.checkbox');
// $checkbox.hidden = false;
let rect = $img.getBoundingClientRect();
$checkbox.style.left = `${Math.round(rect.left + rect.width / 2)}px`;
$checkbox.style.top = `${Math.round(rect.top + rect.height / 2)}px`;
}
catch (e)
{
console.error(e);
}
}
function initializeLightBox() {
// if(lightBox !== undefined)
// {
// lightBox.destroy();
// lightBox = undefined;
// }
lightBox = GLightbox({
selector: `.glightbox-${lightBoxList.length}`,
touchNavigation: true,
loop: true,
autoplayVideos: true,
touchFollowAxis: true,
zoomable: false,
draggable: false,
skin: 'paddedbox' // creates a class called glightbox-paddedbox
});
// slide_after_load
lightBox.on('slide_changed', e => {
console.info('slide changed', e);
// current.slideNode.querySelector('img') instanceof HTMLImageElement
// current.slideNode.nextSibling = div.gslide.loaded.current
// workaround because glightbox e.current is usually one image behind
let node = undefined;
if(e?.current?.slideNode?.previousSibling?.classList?.contains('current'))
node = e.current.slideNode.previousSibling;
else if(e?.current?.slideNode?.nextSibling?.classList?.contains('current'))
node = e.current.slideNode.nextSibling;
else
node = e.current.slideNode;
copyToClipboard(node.querySelector('img'));
//console.info(current.slideNode.querySelector('img'));
//copyToClipboard(current.slideNode.querySelector('img'));
});
// this method is useless because it runs multiple times (possibly because of preloading??)
lightBox.on('slide_after_load', e => {
// console.info('slide_after_load', e);
// console.info(e.slideNode.querySelector('img'));
// current.slideNode.querySelector('img') instanceof HTMLImageElement
//copyToClipboard(current.slideNode.querySelector('img'));
});
lightBox.on('open', (e) => {
Config.lightBoxOpen = true;
// _imgNode.setAttribute('style', "max-height: calc(100vh - ".concat(descHeight, "px)"));
});
lightBox.on('close', (e) => {
console.log('glightbox closed');
Config.lightBoxOpen = false;
});
lightBoxList.push(lightBox);
}
async function LogicLoop() {
//console.log("Logic Loop");
if(!Config.inSearchFunc) {
Config.inSearchFunc = true;
let result = await search();
Config.inSearchFunc = false;
}
setTimeout(LogicLoop, 500);
}
// imgType = clipart, face, lineart, stock, photo, animated
function buildGoogleUrl({cseId, cseKey, searchText, imgType = undefined, transparencyOnly = false, startIndex = 0, noAI = false}) {
if(noAI) {
searchText = `${searchText} before:2022`;
}
let url = `https://www.googleapis.com/customsearch/v1?q=${searchText}&start=${startIndex}&cx=${cseId}&searchType=image&key=${cseKey}&filter=1&safe=active`;
if(imgType !== undefined)
url += `&imgType=${imgType}`;
if(transparencyOnly)
url += '&imgColorType=trans';
return url;
}
function createContainer() {
let $el = Misc.htmlToElement(`
<div class="image_container placeholder" data-fullimage="" data-filled="false">
<a href="javascript:void(0);" class="glightbox-${lightBoxList.length}" data-type="image" data-glightbox="description: .custom-desc1">
<img alt="" src="" class="image">
</a>
</div>`);
$el.querySelector('img').addEventListener('load', (event) => {
console.log('Thumbnail loaded');
Misc.toggleClass(event.target.closest('div.placeholder'), 'flexible', ['flexible', 'placeholder']);
});
return $el;
}
async function search() {
if(!Misc.IsNullOrWhitespace(Config.newSearch) && Config.newSearch !== Config.curSearch)
{
console.log(`Search param has changed from ${Config.curSearch} to ${Config.newSearch}`);
Config.nextIndex = 0;
Config.curSearch = Config.newSearch;
Misc.removeInsideElement($gallery);
window.scrollTo(0, 0);
// destroy all light boxes
for(let l of lightBoxList)
l.destroy();
lightBoxList = [];
}
if(Misc.IsNullOrWhitespace(Config.curSearch) || Config.nextIndex === undefined) return;
// let the process begin
let $container;
let containers = Array.from(document.querySelectorAll('.image_container'));
while (!containers.some(c => Misc.isBelow(c))) {
console.log('No bottom level image containers, creating additional ones');
$container = createContainer();
$gallery.appendChild($container);
containers = Array.from(document.querySelectorAll('.image_container'));
}
let modifiedGallery = false;
let modifiedContainers = [];
while (Config.nextIndex !== undefined &&
Array.from(document.querySelectorAll('div[data-filled="false"]')).length > 0) {
// run a query and prepare to populate these
console.log(`Unfilled elements found - fetching from API at index ${Config.nextIndex}`);
try {
let response = await fetch(buildGoogleUrl(
{
cseId: Config.appSettings.CSE_ID,
cseKey: Config.appSettings.CSE_KEY,
searchText: Config.curSearch,
startIndex: Config.nextIndex,
noAI: Config.appSettings.NO_AI
}));
let json = await response.json();
Config.nextIndex = json?.queries?.nextPage?.[0]?.startIndex;
console.info(json);
if(Array.isArray(json?.items)) {
const isBanned = hostname => Config.appSettings.BANNED_DOMAINS.map(d => new RegExp(d, 'gi')).some(re => re.test(hostname));
const searchResults = json.items.map(itm => new GoogleClasses.GoogleItemResult(itm))
.filter(item => !isBanned(new URL(item.link).hostname) );
// filter out results by Config.appSettings.BANNED_DOMAINS
for (const item of searchResults) {
// item?.image?.thumbnailLink
$container = document.querySelector('div[data-filled="false"]');
if ($container === null) {
$container = createContainer();
$gallery.appendChild($container);
}
let $img = $container.querySelector('img');
$img.src = item.image.thumbnailLink;
$container.setAttribute('data-filled', 'true');
$container.setAttribute('data-fullimage', item.link);
modifiedGallery = true;
modifiedContainers.push($container);
}
}
} catch (err) {
console.error(err);
break;
}
// we might not have been able to fill all the elements, in this case we need to rerun again
}
// don't insert the <a> elements until we call lightbox otherwise the <a> tags will redirect to the files
if(modifiedGallery)
{
modifiedContainers.forEach(container => {
let href = container.getAttribute('data-fullimage');
container.querySelector('a').href = href;
});
initializeLightBox();
}
}