我正在尝试为所有本地存储键添加一个“标签名称空间”,以便为多个标签之间的站点实现本地存储的唯一性。
在一个简单的HTML5页面中,假设一个网页具有以下脚本:
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value) {
console.log("intercepted set local storage " + key + " = " + value);
this._setItem(key, value);
}
Storage.prototype._getItem = Storage.prototype.getItem;
Storage.prototype.getItem = function(key) {
console.log("intercepted get local storage " + key);
return this._getItem(key);
}
这为用户提供了拦截所有获取/设置到本地存储的机会。
现在,假设您有一个扩展程序执行相同的操作:
var actualCode = `Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value) {
console.log("intercepted set local storage " + key + " = " + value);
this._setItem(key, value);
}
Storage.prototype._getItem = Storage.prototype.getItem;
Storage.prototype.getItem = function(key) {
console.log("intercepted get local storage " + key);
return this._getItem(key);
}
console.log("local storage get/set injector completed");
`;
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();
在使用上述脚本加载页面时启用此扩展时,将导致堆栈溢出:
Uncaught RangeError: Maximum call stack size exceeded
at Storage.getItem [as _getItem] (<anonymous>:48:37)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
是否有某种方法可以检测到某人已经进行了拦截的本地存储并防止该问题发生?