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

Event dispatcher doesn't work for swiping videos in TikTok?

我想自动刷抖音视频,所以在Chrome控制台试了下,不行

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

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

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, …}

我很确定 Tiktok 的 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();