简体   繁体   English

捕获(非屏幕截图)的屏幕截图

[英]Capture (screenshot) of inactive tab

Would like to capture image of possibly inactive tab. 想要捕获可能不活动的选项卡的图像。

Problem is that when using the approach displayed here the tab often does not get time to load before capture is done, resulting in failure. 问题是,当使用此处显示的方法时,选项卡通常在捕获完成之前没有时间加载,从而导致失败。

The chrome.tabs.update() call-back is executed before the tab can be captured. 在捕获选项卡之前执行chrome.tabs.update()回调。

I have also tried to add listeners to events like tabs.onActivated and tabs.onHighlighted and doing the capture when those are fired, but the result is the same. 我还尝试将监听器添加到tabs.onActivatedtabs.onHighlighted等事件中,并在触发这些事件时进行捕获,但结果是相同的。 And, as a given by that, I have also tried highlighted instead of active on chrome.tabs.update() – and combination of both; 而且,作为一个给定的,我也尝试highlighted而不是active chrome.tabs.update() - 和两者的组合; with listeners and call-backs. 有听众和回电。

The only way to make it work partially better is by using setTimeout() , but that is very hackish, not reliable and ugly. 使其更好地工作的唯一方法是使用setTimeout() ,但这非常hackish,不可靠和丑陋。 The fact one have to activate a tab before capture is somewhat noisy – but if one have to add delays the issue becomes somewhat worse. 在捕获之前必须激活选项卡的事实有些嘈杂 - 但如果必须添加延迟,则问题会变得更糟。

This is more like a convenience feature for my extension, but would be nice to make it work. 这更像是我的扩展的一个便利功能,但是很高兴它可以工作。

/* Not the real code, but the same logic. */

var request_tab = 25,
    request_win = 123
    old_tab;

/* Check which tab is active in requested window. */
chrome.tabs.query({
    active   : true,
    windowId : request_win
}, function (re) {

    old_tab = re[0].id;

    if (old_tab !== request_tab) {
        /* Requested tab is inactive. */
        /* Activate requested tab.    */
        chrome.tabs.update(request_tab, {
            active: true
        }, function () {     
            /* Request capture */                          /* CB TOO SOON! */
            chrome.tabs.captureVisibleTab(request_window, {
                format : 'png'
            }, function (data) {
                /* ... data ...  */

                /* Put back old tab */
                chrome.tabs.update(old_tab, {
                    active: true
                });
            })
        });
    } else {
        /* Requested tab is active. */
        /* Request capture. */
        chrome.tabs.captureVisibleTab(request_window, {
            format : 'png'
        }, function (data) {
            /* ... data ...  */
        })
    }
});

Since that you are updating the tab using the chrome.tabs.update() method, the callback will be called as soon as the tab properties are changed, but, obviously, before the page is loaded . 由于您使用chrome.tabs.update()方法更新选项卡,因此只要更改选项卡属性,就会调用回调,但显然, 在加载页面之前 To work around this you should remember that the tab isn't yet ready and, using the chrome.tabs.onUpdated event, check when it's ready and you can use chrome.tabs.captureVisibleTab() . 要解决此问题,您应该记住该选项卡尚未就绪,并使用chrome.tabs.onUpdated事件检查它何时准备就绪 ,您可以使用chrome.tabs.captureVisibleTab()

Here is the solution: 这是解决方案:

var request_tab = 25,
    request_win = 123,
    waiting = false,
    // ^^^ Variable used to check if tab has loaded
    old_tab;

// Check which tab is active in requested window.
chrome.tabs.query({
    active   : true,
    windowId : request_win
}, function (re) {

    old_tab = re[0].id;

    if (old_tab !== request_tab) {
        // Requested tab is inactive
        // Activate requested tab
        chrome.tabs.update(request_tab, { active: true });    

        // Tab isn't ready, you can't capture yet
        // Set waiting = true and wait...
        waiting = true;

    } else {
        // Requested tab is active
        // Request capture
        chrome.tabs.captureVisibleTab(request_window, {
            format : 'png'
        }, function (data) {
            // Elaborate data...
        })
    }
});

chrome.tabs.onUpdated.addListener(function(tabID, info, tab) {

    // If the tab wasn't ready (waiting is true)
    // Then check if it's now ready and, if so, capture
    if (waiting && tab.status == "complete" && tab.id == request_tab) {

        // Now you can capture the tab
        chrome.tabs.captureVisibleTab(request_window, {
            format : 'png'
        }, function (data) {

            // Elaborate data...

            // Put back old tab
            // And set waiting back to false
            chrome.tabs.update(old_tab, { active: true });
            waiting = false;
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM