-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcontent.js
131 lines (107 loc) · 3.69 KB
/
content.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
let ON_CALL = false;
let IS_SUBTITLE_ON = false;
let MEET_CODE;
let script = [];
let last_speaker = "";
chrome.storage.sync.set({
ON_CALL: false,
subtitleWarning: false
})
const docObserver = new MutationObserver(() => {
if (document.body.querySelector("div[jscontroller='kAPMuc']")) {
ON_CALL = true;
// Remove observer
docObserver.disconnect();
chrome.runtime.sendMessage({
todo: "activate"
});
chrome.storage.sync.set({
ON_CALL: true,
})
callStarts();
} else {
ON_CALL = false;
chrome.storage.sync.set({
ON_CALL: false,
})
}
});
docObserver.observe(document.body, {
childList: true,
subtree: true,
});
function whenSubtitleOff() {
chrome.storage.sync.set({
subtitleWarning: true,
});
};
function callStarts() {
const subtitleDiv = document.querySelector("div[jscontroller='TEjq6e']");
MEET_CODE = window.location.pathname;
MEET_CODE = MEET_CODE.substr(1, MEET_CODE.length - 1);
chrome.storage.sync.get(["meet_code", "script"], function (result) {
if (result.meet_code && result.meet_code == MEET_CODE) {
script = result.script;
}
chrome.storage.sync.set({
script: script
})
})
// To notify the first time
IS_SUBTITLE_ON = subtitleDiv.style.display === "none" ? false : true;
if (IS_SUBTITLE_ON) whenSubtitleOn();
else whenSubtitleOff();
const subtitleOnOff = new MutationObserver(() => {
IS_SUBTITLE_ON = subtitleDiv.style.display === "none" ? false : true;
if (IS_SUBTITLE_ON) whenSubtitleOn();
else whenSubtitleOff();
});
subtitleOnOff.observe(subtitleDiv, {
attributes: true,
attributeOldValue: true,
attributeFilter: ["style"],
});
};
function whenSubtitleOn() {
chrome.storage.sync.set({
subtitleWarning: false,
meet_code: MEET_CODE
});
// DOM element containing all subtitles
const subtitleDiv = document.querySelector("div[jscontroller='TEjq6e']");
const subtitleObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.target.classList && mutation.target.classList.contains("iTTPOb")) {
if (mutation.addedNodes.length) {
var newNodes = mutation.addedNodes;
var speaker = newNodes["0"]?.parentNode?.parentNode?.parentNode?.querySelector(".zs7s8d.jxFHg")?.textContent;
if (speaker) {
setTimeout(function () {
if (newNodes.length) {
if (last_speaker != speaker) {
script.push(speaker + " : " + newNodes["0"].innerText + "\r\n");
last_speaker = speaker;
} else {
var lastText = script.pop();
lastText = lastText.slice(0, -2);
lastText = lastText + newNodes["0"].innerText + "\r\n";
script.push(lastText);
}
chrome.storage.sync.set({
script: script,
})
}
}, 10000);
}
}
}
});
});
// Start observing subtitle div
subtitleObserver.observe(subtitleDiv, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
};