gpt4 book ai didi

Javascript:什么算作 "User interacting with webpage?"

转载 作者:行者123 更新时间:2023-12-04 12:33:45 24 4
gpt4 key购买 nike

我正在尝试构建一个 chrome 扩展,当页面上的值发生更改时播放音频文件以提醒用户它已更改。有时它可以工作,但有时它说“play()”方法不被允许,因为“用户没有与网页交互”。
它通常会在第一次加载网页时工作,但是当在同一个站点中导航离开时,它通常会抛出这个错误。
这个问题可以分成两个较小的问题:

  • 什么是 Chrome 中的“用户交互”?我似乎找不到任何关于此的信息。
  • 如果设置了“用户交互”标志,有什么方法可以使用 Javascript 进行检查?
  • 最佳答案

    这与 Web Audio API 有关。来自 Mozilla Web Docs :

    Browsers have started to implement an autoplay policy, which ingeneral can be summed up as:

    "Create or resume context from inside a user gesture".



    这是为了防止网页在没有用户交互的情况下在页面加载时发出声音。文档继续说明:

    But what does that mean in practice? A user gesture has beeninterpreted to mean a user-initiated event, normally a click event.Browser vendors decided that Web Audio contexts should not be allowedto automatically play audio; they should instead be started by a user.This is because autoplaying audio can be really annoying andobtrusive. But how do we handle this?

    When you create an audio context (either offline or online) it iscreated with a state, which can be suspended, running, or closed.

    When working with an AudioContext, if you create the audio contextfrom inside a click event the state should automatically be set torunning.


    来源: Web Audio API best practices

    关于Javascript:什么算作 "User interacting with webpage?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63475537/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com