首页 > 解决方案 > 我想在 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')
                });

            });
        });
    }
        });

标签: javascriptphp

解决方案


就像我在上面的评论中提到的那样,该方法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


推荐阅读