【问题标题】:Userscript to click "Show more..." button on YouTube using TamperMonkey使用 TamperMonkey 在 YouTube 上单击“显示更多...”按钮的用户脚本
【发布时间】:2016-06-19 20:02:36
【问题描述】:

我想使用 Tampermonkey 在 Youtube 用户页面上单击“显示更多”按钮。我也已经在网站上进行了研究,但没有找到满意的答案。

按钮的 HTML:

<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-default load-more-button yt-uix-load-more browse-items-load-more-button" type="button" onclick=";return false;" aria-label="Carregar mais
" data-uix-load-more-href="/browse_ajax?action_continuation=1&amp;continuation=4qmFsgJAEhhVQ2UwSXlLNG50Z2RQVFRqc3hqdnlIUGcaJEVnWjJhV1JsYjNNZ0FEZ0JZQUZxQUhvQk1yZ0JBQSUzRCUzRA%253D%253D" data-uix-load-more-target-id="channels-browse-content-grid"><span class="yt-uix-button-content">  <span class="load-more-loading hid">
      <span class="yt-spinner">
      <span title="Carregando ícone" class="yt-spinner-img  yt-sprite"></span>

Carregando...
  </span>

  </span>
  <span class="load-more-text">
    Carregar mais

  </span>
</span></button>

我首先尝试了这段代码:

// ==UserScript==
// @name         YouTube
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.youtube.com/user/*/videos
// @grant        none
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js
// ==/UserScript==

$(document).ready(
   $(function(){
    document.getElementsByClassName("yt-uix-button yt-uix-button-size-default yt-uix-button-default load-more-button yt-uix-load-more browse-items-load-more-button")[0].click();
});
);

我也试过这个:

// ==UserScript==
// @name         YouTube
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.youtube.com/user/*/videos
// @grant        none
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js
// ==/UserScript==

$(document).ready(        

    setTimeout(function() {
    document.querySelector('#yt-uix-button yt-uix-button-size-default yt-uix-button-default load-more-button yt-uix-load-more browse-items-load-more-button').click()
}, 1000)

但是这两个代码似乎根本不起作用。有人可以解释一下如何解决这个问题吗?我必须把什么代码放在那里?我对JS一无所知。

【问题讨论】:

    标签: javascript jquery greasemonkey userscripts tampermonkey


    【解决方案1】:

    截至 2018 年 2 月 20 日,您匹配的网址 (https://www.youtube.com/user/*/videos) 实际上会返回依赖无限滚动的 HTML 页面。正如您所提到的,没有“显示更多”HTML 按钮。这意味着:

    document.querySelector('#yt-uix-button yt-uix-button-size-default yt-uix-button-default load-more-button yt-uix-load-more browse-items-load-more-button');
    

    将返回null,当你执行.click() 时,你只会得到一个TypeError。显然,这不是您想要的。

    如果您想使用 Javascript 在页面上“显示更多”视频,则必须改为:

    document.querySelector("yt-next-continuation").trigger();
    

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 2016-09-17
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      相关资源
      最近更新 更多