JavaScript自动点击”加载更多”按钮

huangapple go评论68阅读模式
英文:

Javascript to auto-click "Load more"-Button

问题

我正在运行 onlineyoga.ch 网站,该网站使用第三方软件。

我的问题出现在播放器页面上:
https://onlineyoga.ch/programs/entspannungsinseln-im-alltag

由于我无法更改 HTML 代码,我想运行一个自动点击“显示更多”按钮的 JavaScript,该按钮名为“Mehr anzeigen”,我可以将其添加到自定义脚本部分。

查看截图

我尝试了几种方法,但没有找到可用的代码:

例如:

// 等待 DOM 完全加载
document.addEventListener('DOMContentLoaded', function() {
  // 使用类 'button left' 查找按钮
  var button = document.querySelector('.button.left ds-button');

  // 检查是否找到元素
  if (button) {
    // 模拟点击按钮
    button.click();

    // 显示通知
    window.alert("Button has been clicked!");
  }
});

问题是,如果我在 Chrome 控制台中测试我的脚本,我总是得到一个“未定义”的返回,点击没有发生。所以我无法进一步处理。
查看使用控制台进行 Chrome 脚本测试的截图

你知道我如何实现我的目标,以及如何使用 JavaScript 使其可点击,并可以在 https://onlineyoga.ch/programs/entspannungsinseln-im-alltag 上成功测试吗?

英文:

I'm running the site onlineyoga.ch which uses a third party software.

My issue is on the player page:
https://onlineyoga.ch/programs/entspannungsinseln-im-alltag

Since I can't change the HTML Code, I would like to run a Javascript that auto-clicks the show more button called "Mehr anzeigen", which I can add to the custom scripts section.

See Screenshot

I tried several approaches, but didn't find a working code:

For example:

// Wait until the DOM is fully loaded
document.addEventListener('DOMContentLoaded', function() {
  // Find the button using the class 'button left'
  var button = document.querySelector('.button.left ds-button');

  // Check if the element was found
  if (button) {
    // Simulate a click on the button
    button.click();

    // Display a notification
    window.alert("Button has been clicked!");
  }
});

The problem is, if I test my scripts in the chrome console, I always get a "undefined" back and the click does not happen. So I can't go further with this..
See Screenshot of Chorme Script testing with Console

Do you know how I can reach my goal and make this clickable with a Javascript and can sucessfully test it on https://onlineyoga.ch/programs/entspannungsinseln-im-alltag ?

答案1

得分: 0

你可以使用以下代码来显示内容:

document.querySelector('ds-show-more').setAttribute('is-open', '1');
document.addEventListener('DOMContentLoaded', function() {
  document.querySelector('ds-show-more').setAttribute('is-open', '1');
});
英文:

you can show the content using

> document.querySelector('ds-show-more').setAttribute('is-open','1')

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

document.addEventListener(&#39;DOMContentLoaded&#39;, function() {
document.querySelector(&#39;ds-show-more&#39;).setAttribute(&#39;is-open&#39;,&#39;1&#39;)
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 13:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定