-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
161 lines (140 loc) · 4.55 KB
/
inject.js
File metadata and controls
161 lines (140 loc) · 4.55 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
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
(function () {
// --- toString cloaking ---
const nativeFns = new Map();
const origToString = Function.prototype.toString;
Function.prototype.toString = function () {
return nativeFns.get(this) || origToString.call(this);
};
nativeFns.set(Function.prototype.toString, origToString.call(origToString));
function cloak(patched, original) {
nativeFns.set(patched, origToString.call(original));
return patched;
}
// --- fake MediaDeviceInfo that passes instanceof ---
function createDeviceInfo(original, label) {
const info = Object.create(MediaDeviceInfo.prototype);
Object.defineProperties(info, {
deviceId: { value: original.deviceId, enumerable: true },
kind: { value: original.kind, enumerable: true },
label: { value: label, enumerable: true },
groupId: { value: original.groupId, enumerable: true },
});
info.toJSON = function () {
return {
deviceId: this.deviceId,
kind: this.kind,
label: this.label,
groupId: this.groupId,
};
};
return info;
}
// --- spoof MediaStreamTrack.label ---
const origLabelDesc = Object.getOwnPropertyDescriptor(
MediaStreamTrack.prototype,
"label"
);
const trackLabelMap = new WeakMap();
Object.defineProperty(MediaStreamTrack.prototype, "label", {
get: cloak(function () {
const spoofed = trackLabelMap.get(this);
if (spoofed !== undefined) return spoofed;
return origLabelDesc.get.call(this);
}, origLabelDesc.get),
configurable: true,
enumerable: true,
});
// --- config loading ---
const configPromise = new Promise((resolve) => {
const existing = document.documentElement.getAttribute("data-cam-spoofer");
if (existing) {
try {
resolve(JSON.parse(existing));
} catch (_) {
resolve(null);
}
return;
}
const observer = new MutationObserver((mutations) => {
for (const m of mutations) {
if (m.attributeName === "data-cam-spoofer") {
observer.disconnect();
const val =
document.documentElement.getAttribute("data-cam-spoofer");
try {
resolve(JSON.parse(val));
} catch (_) {
resolve(null);
}
return;
}
}
});
observer.observe(document.documentElement, { attributes: true });
setTimeout(() => {
observer.disconnect();
resolve(null);
}, 2000);
});
// --- save originals ---
const origEnumerate =
navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices);
const origGetUserMedia =
navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
async function findSelectedDevice(config) {
const devices = await origEnumerate();
return devices.find(
(d) => d.kind === "videoinput" && d.label === config.selectedLabel
);
}
// --- patch enumerateDevices ---
navigator.mediaDevices.enumerateDevices = cloak(async function () {
const config = await configPromise;
const devices = await origEnumerate();
if (!config || !config.enabled || !config.selectedLabel) {
return devices;
}
const selectedFound = devices.some(
(d) => d.kind === "videoinput" && d.label === config.selectedLabel
);
if (!selectedFound) {
return devices;
}
return devices
.map((d) => {
if (d.kind !== "videoinput") return d;
if (d.label !== config.selectedLabel) return null;
if (config.customLabel) {
return createDeviceInfo(d, config.customLabel);
}
return d;
})
.filter(Boolean);
}, navigator.mediaDevices.enumerateDevices);
// --- patch getUserMedia ---
navigator.mediaDevices.getUserMedia = cloak(async function (constraints) {
const config = await configPromise;
if (!config || !config.enabled || !config.selectedLabel) {
return origGetUserMedia(constraints);
}
if (constraints && constraints.video) {
const selected = await findSelectedDevice(config);
if (selected) {
const video =
typeof constraints.video === "object"
? { ...constraints.video }
: {};
video.deviceId = { exact: selected.deviceId };
constraints = { ...constraints, video };
}
}
const stream = await origGetUserMedia(constraints);
// Spoof track labels to match custom name
if (config.customLabel) {
stream.getVideoTracks().forEach((track) => {
trackLabelMap.set(track, config.customLabel);
});
}
return stream;
}, navigator.mediaDevices.getUserMedia);
})();