1 Star 0 Fork 0

小兔子快跑/learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
监听localstorage.html 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
sometime 提交于 2022-05-22 17:23 . save
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let arr = ['setItem', 'clear', 'getItem', 'removeItem']
arr.forEach(item => {
let obj = {}
obj[item] = localStorage[item]
let event = new Event(item)
localStorage[item] = function (key, value) {
event.newData = { key, value }
window.dispatchEvent(event)
Reflect.apply(obj[item], this, [key, value])
}
})
window.addEventListener('setItem', (e) => {
console.log('setItem', e)
})
window.addEventListener('getItem', (e) => {
console.log('getItem', e)
})
window.addEventListener('clear', (e) => {
console.log('clear', e)
})
window.addEventListener('removeItem', (e) => {
console.log('removeItem', e)
})
localStorage.setItem('a', '1212')
localStorage.getItem('a'),
localStorage.removeItem('a')
localStorage.clear()
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yezhu219/learn.git
[email protected]:yezhu219/learn.git
yezhu219
learn
learn
master

搜索帮助