Synchronous call in Google Chrome extension

后端 未结 2 1239
再見小時候
再見小時候 2020-12-29 12:36

I\'m working on Google Chrome extension, which has to block/redirect some outgoing requests. For this purpose, I use chrome.webRequest.onBeforeRequest listener.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 13:24

    Another answer on Stack Overflow recommends keeping track of the tabs outside of your listener function, which avoids this problem entirely.

    Example code:

    /* 
     * --------------------------------------------------
     * Keep list of tabs outside of request callback
     * --------------------------------------------------
     */
    var tabs = {};
    
    // Get all existing tabs
    chrome.tabs.query({}, function(results) {
        results.forEach(function(tab) {
            tabs[tab.id] = tab;
        });
    });
    
    // Create tab event listeners
    function onUpdatedListener(tabId, changeInfo, tab) {
        tabs[tab.id] = tab;
    }
    function onRemovedListener(tabId) {
        delete tabs[tabId];
    }
    
    // Subscribe to tab events
    chrome.tabs.onUpdated.addListener(onUpdatedListener);
    chrome.tabs.onRemoved.addListener(onRemovedListener);
    
    /* 
     * --------------------------------------------------
     * Request callback
     * --------------------------------------------------
     */
    // Create request event listener
    function onBeforeRequestListener(details) {
        // *** Remember that tabId can be set to -1 ***
        var tab = tabs[details.tabId];
    
        // Respond to tab information
    }
    
    // Subscribe to request event
    chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestListener, {
        urls: [""],
        types: ["main_frame"]
    }, ["blocking"]);
    

提交回复
热议问题