英文:
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.
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('DOMContentLoaded', function() {
document.querySelector('ds-show-more').setAttribute('is-open','1')
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论