Override Chrome Storage API

后端 未结 1 1873
终归单人心
终归单人心 2021-01-27 13:51

I would like to override chrome.storage.local.set in order to log the value of a storage key when the Chrome Storage API is called:

var storage_local         


        
相关标签:
1条回答
  • 2021-01-27 14:17

    You need to use .call() or .apply() to provide this object:

    storage_local_set.apply(this, arguments);
    

    The parameters in your override don't match the documentation though. If you want to match the documentation then do it like this:

    const api = chrome.storage.local;
    const { set } = api; 
    api.set = function (data, callback) {
      console.log(data);
      set.apply(api, arguments);    
    };
    

    And if you want to change the signature to separate key and value:

    const api = chrome.storage.local;
    const { set } = api; 
    api.set = (key, value, callback) => {
      console.log(key, value);
      set.call(api, {[key]: value}, callback);    
    };
    
    0 讨论(0)
提交回复
热议问题