事件调度程序不适用于在 TikTok 中滑动视频?



我想自动刷抖音视频,所以我在Chrome控制台上尝试了一下,但不起作用。

var event = new KeyboardEvent('keydown');
document.dispatchEvent(event);

我认为抖音最有可能阻止不受信任的键盘事件触发滚动。如果你运行这个代码

window.addEventListener('keydown', (e) => {
console.log(e)
})
window.dispatchEvent(
new KeyboardEvent('keydown',{
//keycode and code for down arrow
keyCode:40,
code:'ArrowDown'
})
)

您将记录一个KeyboardEvent对象,它看起来像这个

KeyboardEvent {isTrusted: false, key: "", code: "ArrowDown", location: 0, ctrlKey: false, …}

我很确定抖音的keypress/keydown监听器会忽略合成按键(通过检查event.isTrusted(,因此通过模拟向下箭头键自动滚动可能是不可能的。不过,您可以瞄准页面上的下一个按钮并点击它

// this is the class name for the up and down button
let buttonSelector = '.up-and-down';
let buttons  = document.querySelectorAll(buttonSelector)
let prev, next = null;
// if theres one button, then its the next
if(buttons.length == 1)
next = buttons[0]
//if not then the first button is prev, and the last next
else
[prev, next] = buttons
//now click
next && next.click();

最新更新