Greasy Fork

Greasy Fork is available in English.

YouTube検索結果「全てキューに入れて再生」ボタンを追加

musictonicの代わり

目前为 2020-09-13 提交的版本,查看 最新版本

// ==UserScript==
// @name YouTube検索結果「全てキューに入れて再生」ボタンを追加
// @description musictonicの代わり
// @version      0.1
// @run-at document-idle
// @match *://www.youtube.com/*
// @match *://www.youtube.com/
// @grant none
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @namespace http://greasyfork.icu/users/181558
// ==/UserScript==

(function() {
  const wait = (window.navigator.userAgent.toLowerCase().indexOf('chrome') != -1) ? 250 : 150;

  //URLの変化を監視
  var href = location.href;
  var observer = new MutationObserver(function(mutations) {
    if (href !== location.href) {
      href = location.href;
      $('#playAllButton').remove();
      setTimeout(() => { run() }, 1000);
    }
  });
  observer.observe(document, { childList: true, subtree: true });
  setTimeout(() => { run() }, 1000);
  return;

  function run(node = document) {
    if (location.href == "https://www.youtube.com/") {
      var place = eleget0('//div[@id="center" and @class="style-scope ytd-masthead"]');
    } else if (location.href.indexOf('https://www.youtube.com/results?search_query=') !== -1) {
      var place = eleget0('//div[@id="filter-menu"]');
    } else return;

    if (place) {
      var playAllButton = $('<span style="cursor:pointer;" title="クリックで画面に出ている動画を全てキューに入れて再生" id="playAllButton">Play All</span>')
      playAllButton.insertAfter(place);
      playAllButton.click(playAll);
    }

    function playAll() {
      let d = 0;
      for (let e of elegeta('//yt-icon[@class="style-scope ytd-menu-renderer"]')) {
        setTimeout(() => { e.click() }, d);
        setTimeout(() => {
          let cue = eleget0('//yt-formatted-string[text()="キューに追加"]|//yt-formatted-string[text()="Add to queue"]');
          if (cue) cue.click();
        }, d + wait / 2);
        d += wait;
      }
      cli('//div[contains(@class,\"ytp-miniplayer-play-button-container\")]/button[@aria-label=\"再生(k)\"]|//button[@class="ytp-play-button ytp-button" and @aria-label="Play (k)"]', d);
      d += wait;
      cli('//div[@class="ytp-miniplayer-scrim"]/button[@aria-label="拡大(i)"]|//div[@class="ytp-miniplayer-scrim"]/button[@aria-label="Expand (i)"]', d);
      d += wait;
    }
  }

  function cli(xpath, wait) {
    setTimeout(() => {
      let ele = eleget0(xpath);
      if (ele) ele.click();
    }, wait);
  }

  function elegeta(xpath, node = document) {
    if (!xpath) return [];
    try {
      var array = [];
      var ele = document.evaluate("." + xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
      let j = 0;
      for (var i = 0; i < ele.snapshotLength; i++) {
        let ei = ele.snapshotItem(i);
        if (ei.offsetHeight) array[j++] = ei;
      }
      return array;
    } catch (e) { return []; }
  }

  function eleget0(xpath, node = document) {
    if (!xpath) return null;
    try {
      var ele = document.evaluate(xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
      if (ele.snapshotLength < 1) return "";
      let ei = ele.snapshotItem(0);
      if (ei.offsetHeight) return ei;
      return "";
    } catch (e) { return null; }
  }

})()