常用有用js代码

breakon 开源

function breakOn(obj, propertyName, mode, func) {
    // this is directly from https://github.com/paulmillr/es6-shim
    function getPropertyDescriptor(obj, name) {
        var property = Object.getOwnPropertyDescriptor(obj, name);
        var proto = Object.getPrototypeOf(obj);
        while (property === undefined && proto !== null) {
            property = Object.getOwnPropertyDescriptor(proto, name);
            proto = Object.getPrototypeOf(proto);
        }
        return property;
    }

    function verifyNotWritable() {
        if (mode !== 'read')
            throw "This property is not writable, so only possible mode is 'read'.";
    }

    var enabled = true;
    var originalProperty = getPropertyDescriptor(obj, propertyName);
    var newProperty = { enumerable: originalProperty.enumerable };

    // write
    if (originalProperty.set) {// accessor property
        newProperty.set = function(val) {
            if(enabled && (!func || func && func(val)))
                debugger;
            
            originalProperty.set.call(this, val);
        }
    } else if (originalProperty.writable) {// value property
        newProperty.set = function(val) {
            if(enabled && (!func || func && func(val)))
                debugger;

            originalProperty.value = val;
        }
    } else  {
        verifyNotWritable();
    }

    // read
    newProperty.get = function(val) {
          if(enabled && mode === 'read' && (!func || func && func(val)))
            debugger;

        return originalProperty.get ? originalProperty.get.call(this, val) : originalProperty.value;
    }

    Object.defineProperty(obj, propertyName, newProperty);

    return {
      disable: function() {
        enabled = false;
      },

      enable: function() {
        enabled = true;
      }
    };
};

breakOn(document, 'cookie');

##snippet 中假如 jq

function dynamicLoadJs(url, callback) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    if (typeof (callback) == 'function') {
        script.onload = script.onreadystatechange = function() {
            if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
                callback();
                script.onload = script.onreadystatechange = null;
            }
        }
        ;
    }
    head.appendChild(script);
}

function loadc(){
    console.log("已经加载",$("*"))
}

dynamicLoadJs("https://libs.baidu.com/jquery/2.0.0/jquery.min.js", loadc) ;

##tampermonkey 去掉网页限制

// ==UserScript==
// @namespace         https://www.github.com/Cat7373/

// @name              网页限制解除
// @name:en           Remove web limits
// @name:zh           网页限制解除
// @name:zh-CN        网页限制解除
// @name:zh-TW        網頁限制解除
// @name:ja           ウェブの規制緩和

// @description       通杀大部分网站,可以解除禁止复制、剪切、选择文本、右键菜单的限制。
// @description:en    Pass to kill most of the site, you can lift the restrictions prohibited to copy, cut, select the text, right-click menu.
// @description:zh    通杀大部分网站,可以解除禁止复制、剪切、选择文本、右键菜单的限制。
// @description:zh-CN 通杀大部分网站,可以解除禁止复制、剪切、选择文本、右键菜单的限制。
// @description:zh-TW 通殺大部分網站,可以解除禁止復制、剪切、選擇文本、右鍵菜單的限制。
// @description:ja    サイトのほとんどを殺すために渡し、あなたは、コピー切り取り、テキスト、右クリックメニューを選択することは禁止の制限を解除することができます。

// @homepageURL       https://cat7373.github.io/remove-web-limits/
// @supportURL        https://github.com/Cat7373/remove-web-limits/issues/

// @author            Cat73
// @version           1.3
// @license           LGPLv3

// @compatible        chrome Chrome_46.0.2490.86 + TamperMonkey + 脚本_1.3 测试通过
// @compatible        firefox Firefox_42.0 + GreaseMonkey + 脚本_1.2.1 测试通过
// @compatible        opera Opera_33.0.1990.115 + TamperMonkey + 脚本_1.1.3 测试通过
// @compatible        safari 未测试

// @match             *://*/*
// @grant             none
// @run-at            document-start
// ==/UserScript==
(function() {
  'use strict';

  // 域名规则列表
  var rules = {
    black_rule: {
      name: "black",
      hook_eventNames: "",
      unhook_eventNames: ""
    },
    default_rule: {
      name: "default",
      hook_eventNames: "contextmenu|select|selectstart|copy|cut|dragstart",
      unhook_eventNames: "mousedown|mouseup|keydown|keyup",
      dom0: true,
      hook_addEventListener: true,
      hook_preventDefault: true,
      hook_set_returnValue: true,
      add_css: true
    }
  };
  // 域名列表
  var lists = {
    // 黑名单
    black_list: [
      /.*\.youtube\.com.*/,
      /.*\.wikipedia\.org.*/,
      /mail\.qq\.com.*/,
      /translate\.google\..*/
    ]
  };

  // 要处理的 event 列表
  var hook_eventNames, unhook_eventNames, eventNames;
  // 储存名称
  var storageName = getRandStr('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM', parseInt(Math.random() * 12 + 8));
  // 储存被 Hook 的函数
  var EventTarget_addEventListener = EventTarget.prototype.addEventListener;
  var document_addEventListener = document.addEventListener;
  var Event_preventDefault = Event.prototype.preventDefault;

  // Hook addEventListener proc 把他的事件添加函数转移
  function addEventListener(type, func, useCapture) {
    var _addEventListener = this === document ? document_addEventListener : EventTarget_addEventListener;
    if(hook_eventNames.indexOf(type) >= 0) {
        //使用自己的函数,返回真执行默认操作 "contextmenu|select|selectstart|copy|cut|dragstart",需要setter属性
      _addEventListener.apply(this, [type, returnTrue, useCapture]);// 关闭这些hook按键的函数我自己加的
    } else if(unhook_eventNames.indexOf(type) >= 0) {
      var funcsName = storageName + type + (useCapture ? 't' : 'f');
  //"mousedown|mouseup|keydown|keyup",
      if(this[funcsName] === undefined) {
        this[funcsName] = [];
      _addEventListener.apply(this, [type, useCapture ? unhook_t : unhook_f, useCapture]); //关闭这些hook按键的函数
      }

      this[funcsName].push(func);
    } else {
        //其他默认
      _addEventListener.apply(this, arguments);
    }
  }

  // 清理循环
  function clearLoop() {
    var elements = getElements();

    for(var i in elements) {
      for(var j in eventNames) {
        var name = 'on' + eventNames[j];
        if(elements[i][name] !== null && elements[i][name] !== onxxx) {
           //elements[i][name];//这里是事件函数
          if(unhook_eventNames.indexOf(eventNames[j]) >= 0) {
              //不hook的函数,把原来的函数存起来?
            elements[i][storageName + name] = elements[i][name];
            elements[i][name] = onxxx;
          } else {
             // console.log(elements[i][name]);
            //把事件中hook的函数置为空
            elements[i][name] = null;
          }
        }
      }
    }
  }

  // 返回true的函数
  function returnTrue(e) {
    return true;
  }
  function unhook_t(e) {
    return unhook(e, this, storageName + e.type + 't');
  }
  function unhook_f(e) {
    return unhook(e, this, storageName + e.type + 'f');
  }
  function unhook(e, self, funcsName) {
    var list = self[funcsName];
    for(var i in list) {
      list[i](e);
    }

    e.returnValue = true;
    return true;
  }
  function onxxx(e) {
    var name = storageName + 'on' + e.type;
    this[name](e);//调用存起来的函数,并返回真

   // e.returnValue = true;
    return true;
  }

  // 获取随机字符串
  function getRandStr(chs, len) {
    var str = '';

    while(len--) {
      str += chs[parseInt(Math.random() * chs.length)];
    }

    return str;
  }

  // 获取所有元素 包括document
  function getElements() {
    var elements = Array.prototype.slice.call(document.getElementsByTagName('*'));
    elements.push(document);

    return elements;
  }

  // 添加css
  function addStyle(css) {
    var style = document.createElement('style');
    style.innerHTML = css;
    document.head.appendChild(style);
  }

  // 获取目标域名应该使用的规则
  function getRule(url) {
    function testUrl(list, url) {
      for(var i in list) {
        if(list[i].test(url)) {
          return true;
        }
      }

      return false;
    }

    if(testUrl(lists.black_list, url)) {
      return rules.black_rule;
    }

    return rules.default_rule;
  }

  // 初始化
  function init() {
    // 获取当前域名的规则
    var url = window.location.host + window.location.pathname;
    var rule = getRule(url);

    // 设置 event 列表
    hook_eventNames = rule.hook_eventNames.split("|");
    // TODO Allowed to return value
    unhook_eventNames = rule.unhook_eventNames.split("|");
    eventNames = hook_eventNames.concat(unhook_eventNames);

    // 调用清理 DOM0 event 方法的循环
    if(rule.dom0) {
      setInterval(clearLoop, 30 * 1000);
      setTimeout(clearLoop, 2500);
      window.addEventListener('load', clearLoop, true);
      clearLoop();
    }

    // hook addEventListener
    if(rule.hook_addEventListener) {
      EventTarget.prototype.addEventListener = addEventListener;
      document.addEventListener = addEventListener;
    }

    // hook preventDefault
    if(rule.hook_preventDefault) {
      Event.prototype.preventDefault = function() {
        if(eventNames.indexOf(this.type) < 0) {
            //不在列表中,执行默认的,在列表中的不执行
          Event_preventDefault.apply(this, arguments);
        }
      };
    }

    // Hook set returnValue
    if(rule.hook_set_returnValue) {
      Event.prototype.__defineSetter__('returnValue', function() {
          //在列表中的名字
        if(this.returnValue !== true && eventNames.indexOf(this.type) >= 0) {
          this.returnValue = true;
        }
      });
    }

    console.debug('url: ' + url, 'storageName:' + storageName, 'rule: ' + rule.name);

    // 添加CSS
    if(rule.add_css) {
     addStyle('html, * {-webkit-user-select:text!important; -moz-user-select:text!important;}');
    }
  }

  init();
})();

##js hook 来源于网络

function Hooks() {
    return {
        initEnv: function() {
            Function.prototype.hook = function(realFunc, hookFunc, context, funcName) {
                var _context = null;
                //函数上下文
                var _funcName = null;
                //函数名

                _context = context || window;
                _funcName = funcName || getFuncName(this);
                _context[realFunc] = this;

                if (_context[_funcName].prototype && _context[_funcName].prototype.isHooked) {
                    console.log("Already has been hooked,unhook first");
                    return false;
                }
                function getFuncName(fn) {
                    // 获取函数名
                    var strFunc = fn.toString();
                    var _regex = /function\s+(\w+)\s*\(/;
                    var patten = strFunc.match(_regex);
                    if (patten) {
                        return patten[1];
                    }
                    ;return '';
                }
                try {
                    eval('_context[_funcName] = function ' + _funcName + '(){\n' + 'var args = Array.prototype.slice.call(arguments,0);\n' + 'var obj = this;\n' + 'hookFunc.apply(obj,args)\n' + 'return _context[realFunc].apply(obj,args);\n' + '};');
                    _context[_funcName].prototype.isHooked = true;
                    return true;
                } catch (e) {
                    console.log("Hook failed,check the params.");
                    return false;
                }
            }
            Function.prototype.unhook = function(realFunc, funcName, context) {
                var _context = null;
                var _funcName = null;
                _context = context || window;
                _funcName = funcName;
                if (!_context[_funcName].prototype.isHooked) {
                    console.log("No function is hooked on");
                    return false;
                }
                _context[_funcName] = _context[realFunc];
                delete _context[realFunc];
                return true;
            }
        },
        cleanEnv: function() {
            if (Function.prototype.hasOwnProperty("hook")) {
                delete Function.prototype.hook;
            }
            if (Function.prototype.hasOwnProperty("unhook")) {
                delete Function.prototype.unhook;
            }
            return true;
        }
    };
}

var hook = Hooks();
hook.initEnv();

// 这个是要执行的正常的函数
function test() {
    alert('test');
}

// 这个是钩子函数。此钩子函数内心戏:
// 我只喜欢test函数,所以我必须出现在她前面(在她前面执行),这样她才能看到我。
function hookFunc() {
    console.log('hookFunc');
}

// hookFunc钩住test
test.hook(test, hookFunc, window, "test");

// 由于钩子函数hookFunc钩住了test函数,所以test执行时,会先执行hookFunc。
test();

おすすめ

転載: blog.csdn.net/u012787710/article/details/86636451