-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoomSlideCapture.js
140 lines (116 loc) · 4.33 KB
/
ZoomSlideCapture.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
// ==UserScript==
// @name Zoom Slide Recorder
// @namespace https://seanlaidlaw.com
// @version 0.1
// @description Script to capture Screen Share as PNG data every 1s
// @author Sean Laidlaw
// @require https://raw.githubusercontent.com/ericvergnaud/jszip/sync-methods/dist/jszip.js
// @match https://sanger.zoom.us/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant unsafeWindow
// @run-at document-idle
// ==/UserScript==
var images = []
var imagesHashes = []
var intervals = [];
var isCapturing = false
// add the pHash library to page as a <script> tag to <head>
var scr = document.createElement('script');
scr.type = "text/javascript";
scr.src = "https://cdn.jsdelivr.net/npm/phash-js/dist/phash.js";
document.getElementsByTagName('head')[0].appendChild(scr)
function toggleCapture() {
// this is the canvas that Zoom uses to display the screen share
let screen_share = document.getElementById('sharee-container-canvas')
if (isCapturing) {
isCapturing = false
intervals.forEach(clearInterval); // stop capturing screen every 2 second
document.getElementById('startCaptureBtn').innerText = "Start Capture"
// initiate zip file to contain the captured images
var zip = new JSZip();
var zipped = zip.sync(function () { // sync method to avoid async issues in Tapermonkey
var img = zip.folder("images");
for (let i = 0; i < images.length; i++) {
var idx = images[i].indexOf('base64,') + 'base64,'.length;
var content = images[i].substring(idx);
img.file(i + ".png", content, {base64: true});
}
var data = null;
zip.generateAsync({type: "base64"})
.then(function (content) {
data = content;
downloadFile("data:application/zip;base64," + content)
});
});
} else {
isCapturing = true
const interval = setInterval(function () {
// capture state of canvas and append it to the images array
var capture = screen_share.toDataURL('image/png', 1.0);
var file = dataURLtoFile(capture, 'file.png');
pHash.hash(file).then(hash => {
current_hash = hash.toBinary()
console.log(current_hash)
if (imagesHashes.length > 0) {
previousHash = imagesHashes[imagesHashes.length - 1]
console.log("previous hash: " + previousHash)
similarity = calculateSimilarity(current_hash, previousHash)
console.log(similarity)
if (similarity < 85) {
imagesHashes.push(current_hash);
images.push(capture);
console.log("added capture")
}
} else {
imagesHashes.push(current_hash);
images.push(capture);
console.log("added capture")
}
})
}, 2000);
intervals.push(interval);
document.getElementById('startCaptureBtn').innerText = "Capturing..."
}
}
// this creates a button linking to a file and then clicks it to download it
// it avoids the error caused by browser blocking redirect to a file
function downloadFile(filePath) {
var link = document.createElement('a');
link.href = filePath;
link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
link.click();
}
function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type: mime});
}
function calculateSimilarity(hash1, hash2) {
let similarity = 0;
hash1Array = hash1.split("");
hash1Array.forEach((bit, index) => {
hash2[index] === bit ? similarity++ : null;
});
return parseInt((similarity / hash1.length) * 100);
}
(function () {
// this uses a manual 5000ms timer before running this code, due to the webpage dynamically loading.
// without this it runs instantly before any of the targeted elements appear
setTimeout(function () {
//--- Add a button to the Zoom toolbar to toggle capture of screen share.
var shareBtn = document.createElement('div');
shareBtn.innerHTML = '<button id="startCaptureBtn" type="button">Start Capture</button>';
shareBtn.setAttribute('class', 'footer-button-base__button')
document.getElementById('foot-bar').firstChild.appendChild(shareBtn)
//--- Activate the newly added button.
document.getElementById("startCaptureBtn").addEventListener(
"click", toggleCapture, false
);
}, 5000); // 5 seconds will elapse and Code will execute.
})();