-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
154 lines (137 loc) · 4.85 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
let categories = {};
console.log("YouTube Video Sorter: Script loaded");
// Load categories from storage
chrome.storage.sync.get("categories", (data) => {
console.log("YouTube Video Sorter: Fetched categories from storage", data);
if (data.categories) {
categories = data.categories;
attemptAddCustomSortingOptions();
} else {
console.log("YouTube Video Sorter: No categories found in storage");
// Add some default categories for testing
categories = {
Interviews: ["interview", "guest"],
Sketches: ["sketch", "comedy"],
Monologues: ["monologue", "opening"],
};
saveCategories();
}
});
function createChipElement(text, onClick) {
const chip = document.createElement("yt-chip-cloud-chip-renderer");
chip.className =
"style-scope ytd-feed-filter-chip-bar-renderer custom-sort-option";
chip.setAttribute("modern", "");
chip.setAttribute("role", "tab");
chip.setAttribute("tabindex", "0");
chip.setAttribute("chip-style", "STYLE_DEFAULT");
const formattedString = document.createElement("yt-formatted-string");
formattedString.id = "text";
formattedString.className = "style-scope yt-chip-cloud-chip-renderer";
formattedString.setAttribute("ellipsis-truncate", "");
formattedString.setAttribute("ellipsis-truncate-styling", "");
formattedString.setAttribute("title", text);
formattedString.textContent = text;
chip.appendChild(formattedString);
chip.addEventListener("click", (e) => {
e.preventDefault();
onClick();
});
return chip;
}
function addCustomSortingOptions() {
console.log("YouTube Video Sorter: Attempting to add custom sorting options");
const sortContainer = document.querySelector(
"ytd-feed-filter-chip-bar-renderer #chips"
);
if (!sortContainer) {
console.log("YouTube Video Sorter: Sort container not found");
return false;
}
// Check if our options are already added
if (sortContainer.querySelector(".custom-sort-option")) {
console.log("YouTube Video Sorter: Custom options already present");
return true;
}
// Add custom category options
Object.keys(categories).forEach((category) => {
console.log(`YouTube Video Sorter: Adding category option: ${category}`);
const option = createChipElement(category, () => sortVideos(category));
sortContainer.appendChild(option);
});
// Add a "Manage Categories" option
console.log("YouTube Video Sorter: Adding Manage Categories option");
const manageOption = createChipElement(
"Manage Categories",
showCategoryManager
);
sortContainer.appendChild(manageOption);
console.log(
"YouTube Video Sorter: Custom sorting options added successfully"
);
return true;
}
function sortVideos(category) {
console.log(`YouTube Video Sorter: Sorting videos by category: ${category}`);
// Implement sorting logic here
// For now, let's just log the action
alert(`Sorting by ${category}. This feature is not yet implemented.`);
}
function showCategoryManager() {
console.log("YouTube Video Sorter: Showing category manager");
// Implement category management UI here
// For now, let's just log the action
alert("Category management feature is not yet implemented.");
}
function saveCategories() {
console.log("YouTube Video Sorter: Saving categories", categories);
chrome.storage.sync.set({ categories: categories }, () => {
if (chrome.runtime.lastError) {
console.error(
"YouTube Video Sorter: Error saving categories",
chrome.runtime.lastError
);
} else {
console.log("YouTube Video Sorter: Categories saved successfully");
attemptAddCustomSortingOptions();
}
});
}
function attemptAddCustomSortingOptions() {
console.log("YouTube Video Sorter: Attempting to add custom sorting options");
if (!addCustomSortingOptions()) {
console.log(
"YouTube Video Sorter: Failed to add options, retrying in 1 second"
);
setTimeout(attemptAddCustomSortingOptions, 1000);
} else {
console.log(
"YouTube Video Sorter: Successfully added custom sorting options"
);
}
}
// Listen for URL changes and significant DOM changes
let lastUrl = location.href;
console.log(`YouTube Video Sorter: Initial URL: ${lastUrl}`);
new MutationObserver((mutations) => {
const url = location.href;
if (url !== lastUrl) {
console.log(`YouTube Video Sorter: URL changed from ${lastUrl} to ${url}`);
lastUrl = url;
attemptAddCustomSortingOptions();
} else {
for (const mutation of mutations) {
if (
mutation.target.id === "chips" ||
mutation.target.id === "scroll-container"
) {
console.log(
"YouTube Video Sorter: Relevant DOM change detected, attempting to add options"
);
attemptAddCustomSortingOptions();
break;
}
}
}
}).observe(document.body, { childList: true, subtree: true });
console.log("YouTube Video Sorter: MutationObserver set up");