-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.js
209 lines (173 loc) · 5.16 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
let sourceId = null;
// override getDisplayMedia
navigator.mediaDevices.getDisplayMedia = async () => {
// create MediaStream
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: "desktop",
chromeMediaSourceId: sourceId,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720,
},
},
});
return stream;
};
const showBtn = (btn) => {
btn.style.display = "flex";
};
const hideBtn = (btn) => {
btn.style.display = "none";
};
const getScreenShareBtn = () => {
const btns = document.querySelectorAll("[aria-label='Screen share']");
if (btns.length > 1) {
return Array.from(btns).filter((btn) => !btn.dataset.isVirtual)[0];
} else if (btns.length === 1) {
return btns[0];
}
return null;
};
const getVirtualBtn = () => {
const btns = document.querySelectorAll("[aria-label='Screen share']");
if (btns.length > 1) {
return Array.from(btns).filter((btn) => btn.dataset.isVirtual)[0];
}
return null;
};
const setupBtns = () => {
const screenShareBtn = getScreenShareBtn();
const virtualBtn = getVirtualBtn();
if (virtualBtn) {
return virtualBtn;
}
const virtualShareBtn = screenShareBtn.cloneNode(true);
virtualShareBtn.dataset.isVirtual = true;
virtualShareBtn.title = "Virtual";
screenShareBtn.title = "Original";
screenShareBtn.parentNode.appendChild(virtualShareBtn);
showBtn(virtualShareBtn);
hideBtn(screenShareBtn);
virtualShareBtn.addEventListener("click", (e) => {
e.preventDefault();
sourceSelector();
});
screenShareBtn.addEventListener("click", (e) => {
setTimeout(() => {
setupBtns();
showBtn(screenShareBtn);
hideBtn(virtualShareBtn);
}, 500);
});
return virtualShareBtn;
};
const initShareBtn = () => {
let screenShareBtn = null;
if (window.game && window.game.spaceId) {
screenShareBtn = getScreenShareBtn();
}
if (!screenShareBtn) {
setTimeout(initShareBtn, 1000);
return;
}
setupBtns();
};
initShareBtn();
const findTargetByClass = (target, className) => {
if (target && target.classList && target.classList.contains(className)) {
return target;
} else if (target.parentNode) {
return findTargetByClass(target.parentNode, className);
}
};
const sourceSelector = async () => {
const sources = await window.myCustomGetDisplayMedia();
const selector = buildSourceSelector(sources);
document.body.appendChild(selector);
const sourceEls = selector.querySelectorAll(".source");
for (const sourceEl of sourceEls) {
sourceEl.addEventListener("click", (e) => {
e.stopImmediatePropagation();
const target = findTargetByClass(e.target, "source");
const id = target.id;
sourceId = id;
const screenShareBtn = getScreenShareBtn();
// click this so that it's active
screenShareBtn.click();
setTimeout(() => {
showBtn(getScreenShareBtn());
hideBtn(getVirtualBtn());
}, 500);
// Remove the selector
selector.parentNode.removeChild(selector);
});
}
const closeEl = selector.querySelector(".close");
closeEl.addEventListener("click", (e) => {
selector.parentNode.removeChild(selector);
});
};
const buildSourceSelector = (sources) => {
// Create wrapper
const wrapper = document.createElement("div");
wrapper.setAttribute("id", "snap-source-selector");
const closeButton = document.createElement("div");
closeButton.classList.add("close");
closeButton.innerText = "✕";
wrapper.appendChild(closeButton);
// Create screen wrapper
const screens = document.createElement("div");
screens.classList.add("screens");
const screensTitle = document.createElement("h4");
screensTitle.innerText = "Screens";
// Create window wrapper
const windows = document.createElement("div");
windows.classList.add("windows");
const windowTitle = document.createElement("h4");
windowTitle.innerText = "Windows";
wrapper.appendChild(screensTitle);
wrapper.appendChild(screens);
wrapper.appendChild(windowTitle);
wrapper.appendChild(windows);
// Loop through sources
const sourceEls = sources.map(createSourceEl);
for (const source of sourceEls) {
if (source.type === "screen") {
screens.appendChild(source.el);
} else {
windows.appendChild(source.el);
}
}
return wrapper;
};
const createSourceEl = (source) => {
const wrapper = document.createElement("div");
wrapper.classList.add("source");
wrapper.setAttribute("id", source.id);
const name = document.createElement("p");
name.classList.add("name");
name.innerText = source.name;
wrapper.setAttribute(
"style",
"background-image: url(" + source.thumbnail_data + ")"
);
wrapper.append(name);
return {
type: source.id.indexOf("screen") === 0 ? "screen" : "window",
el: wrapper,
};
};
const addDevToggle = async () => {
const devToolsButton = document.createElement("div");
devToolsButton.setAttribute("id", "snap-dev-tools");
devToolsButton.innerText = ">_";
devToolsButton.addEventListener("click", () => {
window.toggleDevTools();
});
document.body.appendChild(devToolsButton);
};
addDevToggle();