-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
119 lines (102 loc) · 3.95 KB
/
Copy pathcontent.js
File metadata and controls
119 lines (102 loc) · 3.95 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
// content.js
const hideRecommendations = () => {
const selectors = [
'ytd-watch-next-secondary-results-renderer',
'ytd-compact-video-renderer',
'ytd-compact-autoplay-renderer'
];
selectors.forEach(selector => {
document.querySelectorAll(selector).forEach(el => el.style.display = 'none');
});
};
let isNotesVisible = true; // Assume notes are visible by defaultå
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action === "toggleRecommendationsAndNotes") {
isNotesVisible = !isNotesVisible; // Toggle the visibility state
toggleNotesAndRecommendations(isNotesVisible);
}
}
);
function toggleNotesAndRecommendations(visible) {
// Logic to show/hide recommendations - adjust according to your implementation
document.querySelectorAll('ytd-watch-next-secondary-results-renderer, ytd-compact-video-renderer, ytd-compact-autoplay-renderer')
.forEach(el => el.style.display = visible ? 'none' : 'block'); // Adjust display logic as needed
// Show/hide note-taking area
const noteContainer = document.getElementById('customNoteContainer');
if (noteContainer) {
noteContainer.style.display = visible ? 'block' : 'none';
}
}
const toggleNoteTakingArea = (show) => {
const existingContainer = document.getElementById('customNoteContainer');
if (show) {
if (!existingContainer) {
const noteContainer = document.createElement('div');
noteContainer.id = 'customNoteContainer';
noteContainer.style.cssText = `
position: fixed;
top: 56px;
right: 0;
width: 350px;
height: calc(100vh - 56px);
background-color: #f1f1f1;
padding: 20px;
box-shadow: -2px 0 2px rgba(0,0,0,0.1);
overflow-y: auto;
z-index: 1000;
`;
const textArea = document.createElement('textarea');
textArea.id = 'noteTextArea';
textArea.style.cssText = 'width: 100%; height: 90%;';
noteContainer.appendChild(textArea);
document.body.appendChild(noteContainer);
// Create the Save button
const saveButton = document.createElement('button');
saveButton.textContent = 'Save Notes';
saveButton.style.cssText = 'width: 100%; margin-top: 10px;';
saveButton.onclick = saveNotes;
noteContainer.appendChild(textArea);
noteContainer.appendChild(saveButton);
document.body.appendChild(noteContainer);
}
} else if (existingContainer) {
existingContainer.remove();
}
};
// Function to save notes
const saveNotes = () => {
const textArea = document.getElementById('noteTextArea');
const text = textArea.value;
// Prompt the user for a filename
const fileName = prompt("Save as:", "MyYouTubeNotes.txt");
if (fileName) { // Proceed only if the user didn't cancel the prompt
// Create a Blob with the text
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
// Create a link and set the URL as the href
const a = document.createElement('a');
a.href = url;
a.download = fileName; // Use the user-provided filename
document.body.appendChild(a);
a.click();
// Clean up
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
};
const checkForVideoPlayerAndPage = () => {
// Improved check based on URL path to determine if on a video page
const isVideoPage = window.location.pathname.startsWith('/watch');
hideRecommendations();
toggleNoteTakingArea(isVideoPage);
};
// MutationObserver to observe body for page navigation changes
const observer = new MutationObserver(checkForVideoPlayerAndPage);
observer.observe(document.body, { childList: true, subtree: true });
// Listen for history state updates to handle SPA navigation
window.addEventListener('popstate', checkForVideoPlayerAndPage);
window.addEventListener('pushState', checkForVideoPlayerAndPage);
window.addEventListener('replaceState', checkForVideoPlayerAndPage);
// Initial check
checkForVideoPlayerAndPage();