仿真 /绕过以下内容:文件选择器对话框只能通过用户激活显示

发布于 2025-02-05 00:37:59 字数 734 浏览 1 评论 0原文

我知道此消息告诉我问题...但是我只是在一个小型有趣的项目中弄乱,并想删除页面加载时打开文件输入所需的额外点击。

以下(和jsfiddle https://jsfiddle.net/qu52ksef/

<label id="foo" for="input">Open</label>
<input id="input" name="input" type="file">

<script>
    ready(function() {
        document.querySelector("#foo").click();
    });

    function ready(fn) {
        if (document.readyState != 'loading'){
                fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }
</script>

) :只能通过用户激活显示文件选择器对话框。

无论如何,是否可以通过此检查作为一种“ hack”?这不会只是我正在玩的一个小宠物项目。

谢谢,

I know this message is telling me the problem... but I'm just messing around with a small fun project and would like to remove the extra click that's required to open a file input when the page loads.

The following (and jsfiddle https://jsfiddle.net/qu52ksef/)

<label id="foo" for="input">Open</label>
<input id="input" name="input" type="file">

<script>
    ready(function() {
        document.querySelector("#foo").click();
    });

    function ready(fn) {
        if (document.readyState != 'loading'){
                fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }
</script>

This will fire the following in Chrome: File chooser dialog can only be shown with a user activation.

Is there anyway to by pass this check as a sort of 'hack'?. This won't be going into production just a little pet project I'm playing with.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2025-02-12 00:37:59

使用它

const showFilePicker = async () => window.showOpenFilePicker({
  types: [
    {
      description: "Выберите изображения",
      accept: {
        "image/*": [".png", ".jpeg", ".jpg"],
      },
    },
  ],
  excludeAcceptAllOption: true,
  multiple: true,
});

use it

const showFilePicker = async () => window.showOpenFilePicker({
  types: [
    {
      description: "Выберите изображения",
      accept: {
        "image/*": [".png", ".jpeg", ".jpg"],
      },
    },
  ],
  excludeAcceptAllOption: true,
  multiple: true,
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文