-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
127 lines (98 loc) · 3.89 KB
/
page.js
File metadata and controls
127 lines (98 loc) · 3.89 KB
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
let win = require("electron").remote.getCurrentWindow();
const {ipcRenderer} = require("electron");
let msgstring = "window-youtubemusic-",
searchel = null;
// search
let search = (function(){
function search(){
this.webview = document.querySelector("webview");
this.element = document.querySelector(".search");
this.elementinput = this.element.querySelector("input");
this.elementspan = this.element.querySelector("span");
this.elementclose = this.element.querySelector("p");
this.webview.addEventListener("found-in-page", this.foundevent.bind(this));
this.elementclose.addEventListener("click", this.hide.bind(this));
this.elementinput.addEventListener("keydown", this.keydown.bind(this));
this.elementinput.addEventListener("keyup", this.keyup.bind(this));
this.shown = 0;
this.match = 0;
this.countdown = "";
}
search.prototype.find = function(text, forward){
if(text) this.webview.findInPage(text, {forward});
else this.clear();
}
search.prototype.foundevent = function(response){
response = response.result;
if(response.activeMatchOrdinal) this.match = response.activeMatchOrdinal;
if(response.finalUpdate){
if(response.matches === 0) this.match = 0;
this.elementspan.innerText = this.match + "/" + response.matches;
}
}
search.prototype.keydown = function(e){
let forward = !e.shiftKey,
text = this.elementinput.value;
switch(e.code){
case "Enter":
this.find(text, forward);
break;
case "Escape":
this.hide();
break;
}
}
search.prototype.keyup = function(e){
let text = this.elementinput.value;
this.find(text, true);
}
search.prototype.clear = function(){
this.webview.stopFindInPage("clearSelection");
this.elementspan.innerText = "";
}
search.prototype.hide = function(){
this.shown = 0;
this.elementinput.blur();
this.clear();
this.elementinput.value = "";
this.element.classList.remove("visible");
}
search.prototype.show = function(){
this.shown = 1;
this.element.classList.add("visible");
this.elementinput.focus();
}
search.prototype.toggle = function(){
if(!this.shown) this.show();
else this.hide();
}
search.prototype.removelisteners = function(){
this.webview.removeEventListener("found-in-page", this.foundevent.bind(this));
this.elementclose.removeEventListener("click", this.hide.bind(this));
this.elementinput.removeEventListener("keydown", this.keydown.bind(this));
this.elementinput.removeEventListener("keyup", this.keyup.bind(this));
}
return search;
})();
// search end
function closeapp(){
searchel.removelisteners();
document.querySelector("webview").removeEventListener("ipc-message", parseMsg);
ipcRenderer.send("ytm-close", "close");
}
function parseMsg(e){
let message = e.channel;
if(message === msgstring + "close") closeapp();
else if(message === msgstring + "mini") win.minimize();
else if(message === msgstring + "toggle") searchel.toggle();
else if(!message.indexOf(msgstring + "discord")) ipcRenderer.send("ytm-discord-rp", message.replace(msgstring + "discord ", ""));
}
window.onload = function(){
let main = document.querySelector(".main");
main.innerHTML = `
<webview src="https://music.youtube.com" preload="${__dirname + "\\youtube.js"}"></webview>
`;
searchel = new search();
let webview = document.querySelector("webview");
webview.addEventListener("ipc-message", parseMsg);
};