javascript - 仅在函数完成运行后使用 window.close() 关闭浏览器选项卡
问题描述
我用 Tampermonkey 注入了以下函数(更大的代码的一部分)
document.addEventListener("keydown", function(e) {
if ((e.keyCode == 83 || e.keyCode == 32) && e.ctrlKey) {
e.preventDefault();
[...document.querySelectorAll('button.btn.btn-primary.pull-end')]
.filter(x => x.innerText.includes('aaa'))[0]
.click()
if (window.location.hash.match(/#*/)) {
window.close()
}
}
}, false);
问题是在 click() 函数完成之前选项卡关闭我理解可能我将不得不使用回调但我无法让它工作任何帮助请
解决方案
我最终通过添加超时功能来做到这一点,它工作正常
document.addEventListener("keydown", function(e) {
if ((e.keyCode == 83 || e.keyCode == 32) && e.ctrlKey) {
e.preventDefault();
[...document.querySelectorAll('button.btn.btn-primary.pull-end')]
.filter(x => x.innerText.includes('aaa'))[0]
.click()
if (window.location.hash.match(/#*/)) {
setTimeout(() => window.close(), 1000);
}
}
}, false);
推荐阅读
- visual-c++ - Visual Studio 如何从代码中读取指令?
- android - Kotlin 代码中无法识别 android.arch.lifecycle.ViewModelProviders
- tensorflow - 如何在 Google colab 上安装 tensorflow 对象检测 API?
- cakephp - $this->paginate() 和 $this->Paginator->paginate() 的区别
- c# - Xunit 防止基类执行测试
- c# - 是使用 java 脚本或 jquery 检查 jquery.unobtrusive-ajax.min.js 是否加载到页面中的任何方法
- azure - 使用 Azure API 调用列出 Azure Active Directory 中用户的所有组
- java - 需要在 Linux 服务器中使用 java Robot 类的替代品
- sql - 分组时如何将字符串组装成数组?
- java - SQL查询查找全天状态的变化