我想chrome.storage.local.set
在调用Chrome Storage API时重写以记录存储键的值:
var storage_local_set = chrome.storage.local.set;
chrome.storage.local.set = function(key, value) {
console.log(key);
storage_local_set(key);
}
但是我得到Illegal invocation: Function must be called on an object of type StorageArea
什么时候storage_local_set(key)
被叫。记录密钥后如何存储数据?
为此chrome.storage.sync.set
,我根据给出的解决方案进行了尝试:
const api = chrome.storage.sync;
const { set } = api;
api.set = function (data, callback) {
console.log(data);
set.apply(api, arguments);
};
但这并不钩住API
您需要使用.call()或.apply()提供this
对象:
storage_local_set.apply(this, arguments);
但是,替代中的参数与文档不匹配。如果要匹配文档,请按照以下步骤进行操作:
const api = chrome.storage.local;
const { set } = api;
api.set = function (data, callback) {
console.log(data);
set.apply(api, arguments);
};
如果要更改签名以将键和值分开,请执行以下操作:
const api = chrome.storage.local;
const { set } = api;
api.set = (key, value, callback) => {
console.log(key, value);
set.call(api, {[key]: value}, callback);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句