Chrome扩展程序:延迟打开标签 [英] Chrome extension: open tabs with delay

查看:205
本文介绍了Chrome扩展程序:延迟打开标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个扩展,它有一个后台页面(和一个与它相关的脚本:eventPage.js)和一个内容脚本(work.js)

内容脚本扫描网页,然后将数据检索到数组中:

  var datas = new Array(); 
var i = 0;
$('。szovegbox_kn tbody')。each(function(){
var data = $(this).first(tr)。find(td:nth-​​child(5) ).html();

if(data!== undefined){
datas [i] = data.replace(/ \s / g,'');
chrome.runtime.sendMessage({
data:datas [i]
});
i ++;
}
});

正如您所看到的,当变量数据为''时,data [i]

这是我的背景页面脚本:

  chrome .runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.data!==未定义){
var data = request.data;
var newURL =http://www.something.com/loadpage.php?dest=+ data;
chrome.tabs.create({
url:newURL
});

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo){
if(changeInfo.status ==='complete'){
window.setTimeout(function() {
chrome.tabs.remove(tabId);
},3000);
}
});
}
});

我想要做的是,当有效的消息(其类型不是未定义的)进入后台页面,选项卡创建延迟n秒,只有一个选项卡被创建,直到选项卡关闭。现在,如果在我的datas []数组中有100条记录,我的背景页面将立即打开所有100个标签。



tl; dr:
将数据数组发送到后台页面,以十秒延迟一个接一个地打开制表符如果您需要更多信息,我可以提供 这听起来像是要节流您的事件处理逻辑。

  var tabCreationDelay = 10000; //毫秒

var createTabThrottled = _.throttle(函数(url){
chrome.tabs.create({
url:url
});
},tabCreationDelay);

chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.data!== undefined){
var data = request.data;
var newURL =http://www.something.com/loadpage.php?dest=+ data;
createTabThrottled(newURL);
}

// ...
});

库中有一些节流算法的实现,如 Underscore Lo-Dash


I am making an extension, which has a background page (and a script associated with it: eventPage.js) and a content script (work.js)

The content script scans the web page, and retreives data in an array:

var datas = new Array();
var i = 0;
$('.szovegbox_kn tbody').each(function () {
    var data = $(this).first("tr").find("td:nth-child(5)").html();

    if (data !== undefined) {
        datas[i] = data.replace(/\s/g, '');
        chrome.runtime.sendMessage({
            data: datas[i]
        });
        i++;
    }
});

As you can see, data[i] gets sent to the background page when the variable data isn't undefined.

This is my background page's script:

chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
    if (request.data !== undefined) {
        var data = request.data;
        var newURL = "http://www.something.com/loadpage.php?dest=" + data;
        chrome.tabs.create({
            url: newURL
        });

        chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
            if (changeInfo.status === 'complete') {
                window.setTimeout(function () {
                    chrome.tabs.remove(tabId);
                }, 3000);
            }
        });
    }
});

What I'd like to do is, that when a valid message (which's type isn't undefined) gets to the background page, the tab creation gets delayed by n seconds, and only ONE tab gets created until the tab gets closed. Right now, if in my datas[] array there's 100 records, my background page opens all 100 tabs instantely. I tried with setTimout, without success.

tl;dr: Data array gets sent to the background page, open tabs one by one with a ten second delay between them.

If you need more info, I can provide

解决方案

It sounds like you want to throttle your event handling logic.

var tabCreationDelay = 10000; //milliseconds

var createTabThrottled = _.throttle(function(url){
    chrome.tabs.create({
        url: url
    });
}, tabCreationDelay);

chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
    if (request.data !== undefined) {
        var data = request.data;
        var newURL = "http://www.something.com/loadpage.php?dest=" + data;
        createTabThrottled(newURL);
    }

    // ...
});

There are implementations of throttling algorithms in libraries like Underscore and Lo-Dash.

这篇关于Chrome扩展程序:延迟打开标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆