Proxy callback function of Chrome extension history API

220 Views Asked by At

I would like to proxy the callback function for chrome.history.search to get the history items.

Here is an example:

chrome.history.search({text: '', maxResults: 10}, function(data) {
    // ...
});

For this example I want to capture the 10 most recently visited URLs.

Here is what I tried:

chrome.history.search = new Proxy(chrome.history.search, {
    apply: (target, thisArg, argumentsList) => {
      console.log(argumentsList[1])     // this gives me the callback function not the data items
      return target.apply(thisArg, argumentsList)
    }
  })

How do I improve this to proxy the callback function of chrome.history.API and log the 10 most recently visited URLs that are passed to the callback function?

1

There are 1 best solutions below

0
On BEST ANSWER

Do it inside your own custom callback, then call the original callback if it was present:

chrome.history.search = new Proxy(chrome.history.search, {
  apply(target, thisObj, args) {
    const cb = typeof args[args.length - 1] === 'function' && args.pop();
    return target.call(thisObj, ...args, res => {
      console.log(res);
      if (cb) cb(res);
    });
  },
});