花了点时间去调这个 BUG,好像是 Windows 版本欧路的问题。原先的代码我在 Windows 上测试了确实不行,但是 iPhone/iPad 上都是可以的。
还是让 ChatGPT 帮我加个按钮调日志,监听点击的脚本竟然运行了两次,于是乎变成了点击没反应。
以下是修改后的代码,测试了 Windows/iPhone/iPad 都是能用的。
document.addEventListener('DOMContentLoaded', () => {
if (window.scriptExecuted) return;
window.scriptExecuted = true;
const elementsSelector = '.x-gs, .collapse';
const toggleVisibility = (elements, display) => elements.forEach(e => e.style.display = display);
setTimeout(() => {
// Expand all collapsible elements with the specified title
document.querySelectorAll('.collapse[title="Oxford Collocations Dictionary"] .heading')
.forEach(headingElement => headingElement.click());
// Hide elements with the specified class by default
document.querySelectorAll(elementsSelector).forEach(e => e.style.display = 'none');
// Add click event listeners to elements with the specified class
document.querySelectorAll('.def.translation_individual').forEach(element => {
element.addEventListener('click', function () {
const elements = this.parentElement.querySelectorAll(elementsSelector);
const display = elements[0].style.display === 'none' ? 'block' : 'none';
toggleVisibility(elements, display);
});
});
// Add click event listener to the gear menu
const gearMenu = document.getElementById('_OALD9_gear');
if (gearMenu) {
const gearHead = gearMenu.querySelector('._gear-ico');
if (gearHead) {
gearHead.addEventListener('click', function () {
const elements = document.querySelectorAll(elementsSelector);
const display = elements[0].style.display === 'none' ? 'block' : 'none';
toggleVisibility(elements, display);
});
}
}
}, 0);
});
测试 Windows 的时候,直接点击某个义项上的文本就可以展开/折叠(可能会误触到选词,不过没想到什么改进的方法了)。其他触控的平台 iPhone/iPad,还是点义项结尾旁边的空白处。
这段代码设置的是,默认隐藏例句、自动展开 Collocations
,可以根据需求修改或者注释掉。