-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontent.js
108 lines (94 loc) · 4.12 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
console.log("running");
import { genExplanation, genStudyGuide } from './langchain.js';
// Create modal
const modal = document.createElement('div');
modal.className = "modal";
modal.innerHTML =
`<div class="modal-header">
<a target="_blank" style="width: 24px;height: 24px;">
<img style="width:24px;height:24px;" src="${chrome.runtime.getURL('images/v-48.png')}">
</a>
<div class="title">Explain</div>
<button id="close-button">×</button>
</div>
<div id="response">
</div>`
let modalAdded = false;
// Create selectAndExplain button
const selectAndExplainButton = document.createElement('button');
selectAndExplainButton.className = "ytp-fullscreen-button ytp-button";
selectAndExplainButton.setAttribute("data-priority", "0");
selectAndExplainButton.setAttribute("data-title-no-tooltip", "Select and Explain");
selectAndExplainButton.setAttribute("title", "Select and Explain");
selectAndExplainButton.innerHTML = `<img style="height:100%;" src="${chrome.runtime.getURL('images/StudyGuide.png')}">`
// Attach a click event to the button
selectAndExplainButton.addEventListener('click', async function () {
// Add modal to page if not already inserted
const secondaryBox = document.querySelector("#secondary");
if (secondaryBox && !modalAdded) {
secondaryBox.prepend(modal);
modalAdded = true;
document.getElementById("close-button").addEventListener('click', function () {
modal.remove();
modalAdded = false;
});
}
// Loading response
const response = document.querySelector("#response");
if (response) {
response.textContent = "Analyzing image...";
}
const video = document.querySelector("#movie_player > div.html5-video-container > video");
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const screenshotDataURL = canvas.toDataURL('image/png');
const explanation = await genExplanation(screenshotDataURL);
console.log("explanation");
console.log(explanation);
// Update the content of the #response element
if (response) {
response.textContent = explanation;
}
});
// Create generateStudyGuide button
// const generateStudyGuideButton = document.createElement('button');
// generateStudyGuideButton.className = "ytp-fullscreen-button ytp-button";
// generateStudyGuideButton.setAttribute("data-priority", "0");
// generateStudyGuideButton.setAttribute("data-title-no-tooltip", "Generate Study Guide");
// generateStudyGuideButton.setAttribute("title", "Generate Study Guide");
// generateStudyGuideButton.innerHTML = `<img style="height:100%;" src="${chrome.runtime.getURL('images/StudyGuide.png')}">`
// // Attach a click event to the button
// generateStudyGuideButton.addEventListener('click', async function () {
// // Add modal to page if not already inserted
// const secondaryBox = document.querySelector("#secondary");
// if (secondaryBox && !modalAdded) {
// secondaryBox.prepend(modal);
// modalAdded = true;
// document.getElementById("close-button").addEventListener('click', function () {
// modal.remove();
// modalAdded = false;
// });
// }
// // Loading response
// const response = document.querySelector("#response");
// if (response) {
// response.textContent = "Generating study guide...";
// }
// const studyguide = await genExplanation();
// console.log("studyguide");
// console.log(studyguide);
// // Update the content of the #response element
// if (response) {
// response.textContent = studyguide;
// }
// });
// Append the buttons to the YouTube page
const controlBar = document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls");
if (controlBar) {
controlBar.prepend(selectAndExplainButton);
// controlBar.prepend(generateStudyGuideButton);
}