Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add video duration filter for recommended videos #2710

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion js&css/extension/www.youtube.com/appearance/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,41 @@ extension.features.relatedVideos = function (anything) {
window.removeEventListener('click', this.relatedVideos, true);
}
}
};

// Apply the duration filter to related videos
var minDuration = extension.storage.get('recommended_videos_min_duration') || 0;

function filterVideos() {
var videos = document.querySelectorAll('ytd-compact-video-renderer, ytd-video-renderer, ytd-grid-video-renderer');

for (var i = 0, l = videos.length; i < l; i++) {
var video = videos[i],
durationElement = video.querySelector('span.ytd-thumbnail-overlay-time-status-renderer');

if (durationElement) {
var durationText = durationElement.textContent.trim(),
durationParts = durationText.split(':'),
durationInSeconds = 0;

for (var j = 0, k = durationParts.length; j < k; j++) {
durationInSeconds = durationInSeconds * 60 + parseInt(durationParts[j]);
}

if (durationInSeconds < minDuration) {
video.style.display = 'none';
} else {
video.style.display = '';
}
}
}
}

filterVideos();

var observer = new MutationObserver(filterVideos);

observer.observe(document.documentElement, {
childList: true,
subtree: true
});
};
43 changes: 43 additions & 0 deletions js&css/extension/www.youtube.com/general/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ extension.features.youtubeHomePage = function (anything) {
option === '/feed/library'
) {
window.addEventListener('click', this.youtubeHomePage, true);
extension.features.filterRecommendedVideosByDuration();
}
}
};
Expand Down Expand Up @@ -578,3 +579,45 @@ extension.features.openNewTab = function () {
}
}
}

/*--------------------------------------------------------------
# FILTER RECOMMENDED VIDEOS BY DURATION
--------------------------------------------------------------*/

extension.features.filterRecommendedVideosByDuration = function () {
var minDuration = extension.storage.get('recommended_videos_min_duration') || 0;

function filterVideos() {
var videos = document.querySelectorAll('ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer');

for (var i = 0, l = videos.length; i < l; i++) {
var video = videos[i],
durationElement = video.querySelector('span.ytd-thumbnail-overlay-time-status-renderer');

if (durationElement) {
var durationText = durationElement.textContent.trim(),
durationParts = durationText.split(':'),
durationInSeconds = 0;

for (var j = 0, k = durationParts.length; j < k; j++) {
durationInSeconds = durationInSeconds * 60 + parseInt(durationParts[j]);
}

if (durationInSeconds < minDuration) {
video.style.display = 'none';
} else {
video.style.display = '';
}
}
}
}

filterVideos();

var observer = new MutationObserver(filterVideos);

observer.observe(document.documentElement, {
childList: true,
subtree: true
});
};
3 changes: 3 additions & 0 deletions menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ chrome.runtime.sendMessage({
document.body.setAttribute('tab', '');
}
});

// Add logic to store the user's preferred minimum video duration for recommendations
satus.storage.set('recommended_videos_min_duration', items.recommended_videos_min_duration || 0);
9 changes: 9 additions & 0 deletions menu/skeleton-parts/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,15 @@ extension.skeleton.main.layers.section.player.on.click = {
component: 'switch',
text: 'forceSDR',
storage: 'player_SDR'
},
recommended_videos_min_duration: {
component: 'slider',
text: 'Minimum Duration for Recommended Videos',
min: 0,
max: 600,
step: 10,
value: 0,
storage: 'recommended_videos_min_duration'
}
},
section_2: {
Expand Down