cableav增加分类搜索排序默认最高画质

增加分类搜索和搜索结果排序.默认最高画质自动播放

// ==UserScript==
// @name         cableav增加分类搜索排序默认最高画质
// @namespace    http://cableav.tv/
// @version      1.1
// @description  增加分类搜索和搜索结果排序.默认最高画质自动播放
// @author       You
// @match        https://cableav.tv/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=cableav.tv
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
//增加分类搜索
   function appendSearchParamToLinks(currentPageUrl) {
  // 提取搜索参数
  const searchParam = currentPageUrl.split('?')[1] || '';

  // 检查指定元素是否存在
  const categoryList = document.querySelector('#categories-2 > div > ul');

  if (categoryList && searchParam) {
    // 获取所有 li 元素下的 a 标签
    const links = categoryList.querySelectorAll('li > a');

    // 遍历所有链接
    links.forEach(link => {
      // 获取原始 href
      let href = link.getAttribute('href');

      // 检查 URL 是否已经包含查询参数
      if (href.includes('?')) {
        // 如果已经有查询参数,添加 &
        href += `&${searchParam}`;
      } else {
        // 如果没有查询参数,添加 ?
        href += `?${searchParam}`;
      }

      // 更新 href 属性
      link.setAttribute('href', href);
    });
  }
}

//增加分类搜索后的排序
function insertAndUpdateSortBlock() {
  // HTML template
  const sortBlockHTML = `
    <div class="category-sort font-size-12">
      <ul class="sort-block sort-block-control">
        <li class="sort-block-list">
          <span class="default-item" data-sort="latest">
            <span>Sort by:</span>
            <span id="current-sort">Latest</span>
            &nbsp;
            <i class="fa fa-angle-double-down" aria-hidden="true"></i>
          </span>
          <ul class="sort-items">
            <li class="sort-item"><a href="#" data-query="latest" title="Latest">Latest</a></li>
            <li class="sort-item"><a href="#" data-query="comment" title="Most commented">Most commented</a></li>
            <li class="sort-item"><a href="#" data-query="view" title="Most viewed">Most viewed</a></li>
            <li class="sort-item"><a href="#" data-query="like" title="Most liked">Most liked</a></li>
            <li class="sort-item"><a href="#" data-query="title" title="Title">Title</a></li>
          </ul>
        </li>
      </ul>
    </div>
  `;

  // Find the target element
  const targetElement = document.querySelector('#main-content > div > div.archive-heading');

   if (targetElement) {
    // Insert the HTML
    targetElement.insertAdjacentHTML('beforeend', sortBlockHTML);

    // Get the current page URL
    const currentURL = new URL(window.location.href);

    // Get the current sort query from URL or default to 'latest'
    const currentSort = currentURL.searchParams.get('archive_query') || 'latest';

    // Update the default displayed sort option
    const currentSortSpan = targetElement.querySelector('#current-sort');

    // Update all links in the sort block
    const sortItems = targetElement.querySelectorAll('.sort-items a');
    sortItems.forEach(link => {
      const query = link.getAttribute('data-query');

      // Update the default displayed sort option
      if (query === currentSort) {
        currentSortSpan.textContent = link.textContent;
      }

      // Clone the current URL to avoid modifying the original
      const newURL = new URL(currentURL);

      // Set or update parameters
      newURL.searchParams.set('archive_query', query);
      newURL.searchParams.set('alphabet_filter', '');

      // Update the link's href
      link.href = newURL.toString();
    });
  } else {
    console.error('Target element not found');
  }
}

    //默认选择最高画质
    function clickLastVideoSource() {
  // 等待一小段时间以确保页面完全加载
  setTimeout(() => {
    // 查找 .fluid_video_sources_list 元素
    const sourcesList = document.querySelector('.fluid_video_sources_list');
    const buttonplay = document.querySelector('.fluid_button_play');
    const buttonvideosource = document.querySelector('.fluid_button_video_source');
    buttonplay.click();
    if (sourcesList) {
      // 获取所有子 div 元素
      const sourceDivs = sourcesList.querySelectorAll('div');

      if (sourceDivs.length > 0) {
        // 获取最后一个 div
        const lastSourceDiv = sourceDivs[sourceDivs.length - 1];

        // 模拟点击最后一个 div
        lastSourceDiv.click();
          buttonvideosource.click();
          buttonplay.click();


        console.log('Clicked the last video source option');
      } else {
        console.log('No source options found');
      }
    } else {
      console.log('.fluid_video_sources_list not found');
    }
  }, 1000); // 等待 1 秒,可以根据需要调整
}

        //判断是否是搜索页面
    const currentPageUrl =window.location.href;
    const hasSParam = currentPageUrl.indexOf('s=') !== -1;
if (hasSParam) {
appendSearchParamToLinks(currentPageUrl);
    insertAndUpdateSortBlock();
}

document.addEventListener('load', clickLastVideoSource());
})();