tampermonkey-script/sortbylikecount.js

112 lines
3.8 KiB
JavaScript

// ==UserScript==
// @name Sort Videos by Like Count
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Sort videos by like count on a specified website
// @author NoName
// @match https://jable.tv/*
// @match https://jable.tv
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract the like count from the sub-title element
function getLikeCount(subTitleElement) {
if (subTitleElement) {
const textContent = subTitleElement.textContent.trim();
console.log('Full text content:', textContent);
// Extract all numbers from the text content
const numbers = textContent.match(/\d+/g);
if (numbers && numbers.length > 0) {
const likeCount = parseInt(numbers[numbers.length - 1].replace(/\s/g, ''), 10);
console.log('Parsed like count:', likeCount);
return isNaN(likeCount) ? 0 : likeCount;
}
}
return 0;
}
// Function to sort the videos within each parent div
function sortVideosInParent(parent) {
console.log('Sorting videos in parent:', parent);
const videoContainers = parent.querySelectorAll('.video-img-box');
const videos = Array.from(videoContainers).map(v => {
const subTitle = v.querySelector('.sub-title');
if (!subTitle) {
return null;
}
const vParent = v.parentElement; // 获取 v 的上一级 div
const likeCount = getLikeCount(subTitle);
console.log('Video element:', vParent, 'Like count:', likeCount);
return { element: vParent, likeCount: likeCount };
}).filter(video => video !== null);
videos.sort((a, b) => b.likeCount - a.likeCount);
videos.forEach(video => {
parent.appendChild(video.element);
});
}
// Function to find all parent divs and sort their child videos
function sortAllVideos() {
const videoContainers = document.querySelectorAll('.video-img-box');
const parents = new Set();
videoContainers.forEach(v => {
const parent = v.parentElement.parentElement;
console.log('Found parent:', parent);
parents.add(parent);
});
parents.forEach(parent => {
sortVideosInParent(parent);
});
}
// Add a sort button after the element with class "inactive-color fs-2 mb-0"
function addSortButton() {
const targetElement = document.querySelector('.inactive-color.fs-2.mb-0');
if (targetElement) {
const sortItem = document.createElement('li');
const sortLink = document.createElement('a');
sortLink.href = '#';
sortLink.textContent = '排序';
sortLink.style.marginLeft = '10px';
sortLink.addEventListener('click', function(event) {
event.preventDefault();
sortAllVideos();
});
sortItem.appendChild(sortLink);
targetElement.parentNode.insertBefore(sortItem, targetElement.nextSibling);
console.log('Sort button added after the target element');
}
}
// Add CSS to override the preview thumb image container dimensions
function addCustomStyles() {
const style = document.createElement('style');
style.textContent = `
.plyr__preview-thumb__image-container {
width: 88px !important;
height: 50px !important;
overflow: hidden;
}
`;
document.head.appendChild(style);
console.log('Custom styles added for preview thumb image container');
}
// Run the sort function after the DOM content has loaded
window.addEventListener('load', function() {
addCustomStyles();
addSortButton();
sortAllVideos();
})
})();