覆盖Chrome Storage API [英] Override Chrome Storage API
问题描述
我想重写 chrome.storage.local.set
以便在调用Chrome Storage API时记录存储密钥的值:
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_set = chrome.storage.local.set;
chrome.storage.local.set = function(key, value) {
console.log(key);
storage_local_set(key);
}
但是我得到非法调用:在调用
.记录密钥后如何存储数据? storage_local_set(key)
时,必须在StorageArea类型的对象上调用函数
But I get Illegal invocation: Function must be called on an object of type StorageArea
when storage_local_set(key)
gets called. How can I store the data after logging the key?
对于 chrome.storage.sync.set
,这是我根据给定的解决方案尝试的方法:
For chrome.storage.sync.set
this is what I tried based on the solution given:
const api = chrome.storage.sync;
const { set } = api;
api.set = function (data, callback) {
console.log(data);
set.apply(api, arguments);
};
但这并不钩住API
推荐答案
您需要使用.call()或.apply()提供 this
对象:
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);
};
这篇关于覆盖Chrome Storage API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!