javascript - 我想在 x 分钟不活动后重定向客户当前使用的标签
问题描述
我想在 x 分钟不活动后重定向客户当前使用的选项卡我检查了浏览器活动并且显示良好,即检查浏览器是否处于活动状态 30 秒如果它不活动我想重定向到默认页面。现在代码显示
调用 tabs.update 时出错(可选整数 tabId,对象 updateProperties,可选函数回调):参数“tabId”错误:值必须至少为 0。
我在 manifest.json 文件中添加了权限
"permissions": [
"activeTab",
"storage",
"geolocation",
"tabs",
"storage",
"idle",
"unlimitedStorage",
"browsingData",
"cookies",
"contextMenus",
"webRequest",
"http://*/*",
"https://*/*",
"<all_urls>"
],
chrome.idle.queryState(30, function(state) {
if (state === "active") {
console.log('Computer not locked, user active in the last 5 minutes');
} else {
console.log('sorry Computer locked, user nop active in the last 5 minutes');
chrome.storage.local.get("appsLog", function(obj) {
theAppsLog = obj.appsLog;
$.each(theAppsLog, function(index, element) {
theAppsString = element.app;
chrome.tabs.getSelected(null, function(tab) {
console.log('sorry hhhhhhhhhhhhhhhhhhhh');
chrome.tabs.update(tab.id, {
active: true,
url: chrome.extension.getURL('html/onionid-ban-inactive-app.html')
});
});
});
}
});
解决方案
就像我在上面的评论中提到的那样,该方法getSelected()
自 Chrome 版本 33 以来已被弃用。
而不是这种方法,您可以使用chrome.tabs.query()
.
示例(未测试):
// Define the properties
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
// Getting the tab
var tab = tabs[0];
// Your code here ...
});
您可以在此处获取有关此方法的更多信息:https ://developer.chrome.com/extensions/tabs#method-query
推荐阅读
- generics - Kotlin:我的通用函数中未经检查的强制转换
- amazon-web-services - AWS Step Functions 任务超时
- ssl - /etc/ssl/cert.pem 中的证书今天过期(MacOS Mojave)
- .net - Azure Servicebus 循环竞争消费者
- github - 如何使用 github API 获取 Github 拉取请求评论线程
- javascript - 邮递员预先请求获取令牌不起作用
- node.js - youtube 搜索没有获取 URLS
- android - 我从哪里开始制作安卓应用程序?
- python - 如何在python中添加if else语句以及函数定义
- python - Pandas:从另一列中的字典列表创建新列