问题描述
chrome 66,在setTimeout回调中触发type为file的input框的点击事件,文件选择框是否弹出和时长有关
操作系统:windows 7
浏览器:chrome 66
1000ms以上:上传的文件框未弹出
1000ms以下:上传的文件框弹出
操作系统:windows 7
浏览器:chrome 75
5000ms以上:上传的文件框未弹出,控制台有提示File chooser dialog can only be shown with a user activation
5000ms以下:上传的文件框弹出
相关代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="button">
<button onclick="clickBtn()">请选择文件</button>
</div>
<div class="input" style="display: none;">
<input type="file" id="fileInput">
</div>
<script>
var clickBtn = function() {
setTimeout(function() {
console.log('input click')
document.querySelector('#fileInput').click();
}, 1000);
}
</script>
</body>
</html>
疑问
1.问什么会因为时长不一样,文件选择框弹出的表现不一致
2.chrome 75,同样都是模拟用户点击,为什么会因为时长不一样,5000ms以上就会打印提示File chooser dialog can only be shown with a user activation
这个问题提的真好!
我甚至为此查阅了chromium的源代码-_-!
为你找到了这篇文章,我觉得这篇文章可以回答你的问题.
https://developers.google.com...
另外,在这里你可以找到最初发现这个问题时候的讨论
https://bugs.webkit.org/show_...
相关的源代码可以定位到这里