【问题标题】:Proxy callback function of Chrome extension history APIChrome扩展历史API的代理回调函数
【发布时间】:2021-03-07 04:48:13
【问题描述】:

我想代理chrome.history.search的回调函数来获取历史记录。

这是一个例子:

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

对于本例,我想捕获最近访问的 10 个 URL。

这是我尝试过的:

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)
    }
  })

如何改进它以代理 chrome.history.API 的回调函数并记录传递给回调函数的 10 个最近访问的 URL?

【问题讨论】:

    标签: javascript google-chrome-extension proxy browser-history es6-proxy


    【解决方案1】:

    在您自己的自定义回调中执行此操作,然后调用原始回调(如果存在):

    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);
        });
      },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多