LoginSignup
0
0

More than 5 years have passed since last update.

localStorage object hook

Last updated at Posted at 2019-04-13

-localStorageのアクセスを監視して、関数名とデータをチェックする。

;(function(root){
 'use strict'
 ;
 function entry(obj,caller){
  var is={}
  is.function = function(obj){return toString.call(obj) === '[object Function]'}
  ;
  var o =obj,keys=Object.keys(o.__proto__).concat(Object.keys(o)).filter(d=>is.function(o[d]))
  console.log('hook object=>',o);console.log('hook funcitons=>',keys)
  ;
  keys.map(key=>{
   var dump= o[key]
   o[key]=function(){
    var args=Array.from(arguments)
    caller.apply(o,[key].concat(args))
    return dump.apply(o,args)
   }
  })
 }
 ;
 root.objectHook=entry
 ;
 /*usage
var obj=localStorage//console
objectHook(obj,function(){
 var type=arguments[0]
 var args=Array.from(arguments).slice(1)
 let x=document.createElement('pre');
 x.textContent=type+':'+JSON.stringify(args,null,'\t');
 document.body.appendChild(x)
 //
})
localStorage.setItem('xyz','bbbb')
 */
})(this);

-そのオブジェクトがフック可能か

is.function = function(obj){return toString.call(obj) === '[object Function]'}

keys=Object.keys(o.__proto__).concat(Object.keys(o)).filter(d=>is.function(o[d]))

-同じ手法でオブジェクトの関数をダミーに置き換える。

;(function(root){
  function entry(obj){
   var is={}
   is.function = function(obj){return toString.call(obj) === '[object Function]'}
   ;
   var o=obj
   ,dummy=function(){ /**/ }
   ,keys=Object.keys(o.__proto__).concat(Object.keys(o)).filter(d=>is.function(o[d]))
   keys.map(key=>o[key]=dummy)
   ;
  }
  ;
  root.objectEraser =entry
})(this);

objectEraser(console)
console.log('aaoeeeeeeaaaaaaaaaaaaeeeea')
console.warn('eeeee')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0