Rx.js, handling Chrome Extension webrequest API callback functions

99封情书 提交于 2019-12-11 06:01:25

问题


I'm trying to use Rx.js to handle the flow of Chrome extension webrequest API.

Each webrequest addListener() call takes a mandatory callback function as the first parameter. This sends request objects to the function. However, the callback can return a webRequest.BlockingResponse that determines the further life cycle of the request.

I'm struggling to handle the blocking response as part of the observable.

This code works well for examining all image requests, for example

onBeforeRequestHandler = function() {

        var filteredURLs = ["http://*/*", "https://*/*"]; 
        var resourceTypes = ["image"]; 
        var filter = { urls: filteredURLs, types: resourceTypes };
        var options = ["blocking"];

        return Rx.Observable.create(observer => {
            var listener = chrome.webRequest.onBeforeRequest.addListener(
                function requestHandler(obj) {
                    observer.next(obj);
                },
                filter, options);
            return unsubscribe => {
                chrome.webRequest.onBeforeRequest.removeListener(listener);
            };
        });

};

I can then use all the Rx.js operators to manipulate the requests by doing this:

var source = onBeforeRequestHandler();
source.subscribe();

etc.

However, if during the course of working the images, I wish to cancel the request, I somehow need to return a blocking response object, like this {cancel:true} to the observable that is wrapping the chrome.webRequest.onBeforeRequest.addListener callback function.

At the moment I have no clue how to do this.

Any help much appreciated.

来源:https://stackoverflow.com/questions/48928351/rx-js-handling-chrome-extension-webrequest-api-callback-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!