Iwara Custom Sort

Automatically sort video results in a page on /videos, /images, /subscriptions, and sidebars using customizable sort function.

当前为 2019-02-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name     Iwara Custom Sort
// @version  0.113
// @grant    GM.setValue
// @grant    GM.getValue
// @grant    GM.deleteValue
// @match    https://ecchi.iwara.tv/*
// @match    https://www.iwara.tv/*
// @match    http://ecchi.iwara.tv/*
// @match    http://www.iwara.tv/*
// @description  Automatically sort video results in a page on /videos, /images, /subscriptions, and sidebars using customizable sort function.
// @namespace https://greasyfork.org/users/245195
// ==/UserScript==

/* jshint esversion: 6 */

const logDebug = (...args) => {
  const debugging = true;
  if (debugging) {
  	console.log(...args);
  }
}

logDebug('Parsed.');

const sortValueInput = document.createElement('input');

const parsePrefixed = (str) => {
  return Number.parseFloat(str) * (str.includes('k') ? 1000 : 1);
}

const sortVideos = (videosContainer) => {
  const videoDivs = Array.from(videosContainer.querySelectorAll('.clearfix'));
  const views = videoDivs.map(div => div.querySelector('.glyphicon-eye-open'))
    .map(div => div ? parsePrefixed(div.parentElement.textContent) : 0);
  const likes = videoDivs.map(div => div.querySelector('.glyphicon-heart'))
    .map(div => div ? parsePrefixed(div.parentElement.textContent) : 0);
  const videoEntries = Object.entries(videoDivs);
  GM.setValue('sortValue', sortValueInput.value);
  const evalSortValue = (views, likes) => {
    const ratio = Math.min(likes / Math.max(1, views), 1);
    return eval(sortValueInput.value);
  }
  videoEntries.sort((entryA, entryB) => {
    return evalSortValue(views[entryB[0]], likes[entryB[0]]) - evalSortValue(views[entryA[0]], likes[entryA[0]]);
  });
  videoDivs.map(div => div.parentElement)
    .forEach((div, index) => {
    div.append(videoEntries[index][1]);
  });
};

const sortAllVideos = () => {
  let gridCount = 0;
  document.querySelectorAll('.views-responsive-grid').forEach((grid) => {
    sortVideos(grid);
    gridCount++;
  });
  logDebug(`${gridCount}grids sorted.`);
};

const requestMorePages = (URL, pageCount) => {
  const params = URL.searchParams;
  const page = params.has('page') ? params.get('page') : 0;
  logDebug(page, pageCount);
}

(async () => {
  const currentURL = new URL(location);
  if (/\/(videos|images|subscriptions)/.test(currentURL.pathname)) {
    const additionalPageCount = 2;
    requestMorePages(currentURL, additionalPageCount);
  }
  sortValueInput.type = 'text';
  sortValueInput.value = await GM.getValue('sortValue', '100 * ratio + Math.sqrt(likes) / 25');
  const uiContainer = (() => {
    const temp = document.querySelector('.list-inline');
    if (temp) {
      return temp;
    } else {
      return document.querySelector('#user-links');
    }
  })();
  uiContainer.insertAdjacentElement('afterbegin', sortValueInput);
  const sortButton = document.createElement('button');
  sortButton.innerHTML = 'Sort';
  uiContainer.insertAdjacentElement('afterbegin', sortButton);
  sortButton.addEventListener('click', sortAllVideos);
  sortAllVideos();
})();