Everything-Hook

it can hook everything

目前為 2019-09-05 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/372672/730906/Everything-Hook.js

  1. // ==UserScript==
  2. // @name Everything-Hook
  3. // @namespace https://gitee.com/HGJing/everthing-hook/
  4. // @updateURL https://gitee.com/HGJing/everthing-hook/raw/master/src/everything-hook.js
  5. // @version 0.5.9048
  6. // @include *
  7. // @description it can hook everything
  8. // @author Cangshi
  9. // @match http://*/*
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. /**
  15. * ---------------------------
  16. * Time: 2017/9/20 18:33.
  17. * Author: Cangshi
  18. * View: http://palerock.cn
  19. * ---------------------------
  20. */
  21. 'use strict';
  22. /**
  23. * Every-Utils
  24. * version:0.0.2001
  25. */
  26. (function (global, factory) {
  27.  
  28. "use strict";
  29.  
  30. if (typeof module === "object" && typeof module.exports === "object") {
  31.  
  32. // For CommonJS and CommonJS-like environments where a proper `window`
  33. // is present, execute the factory and get jQuery.
  34. // For environments that do not have a `window` with a `document`
  35. // (such as Node.js), expose a factory as module.exports.
  36. // This accentuates the need for the creation of a real `window`.
  37. // e.g. var jQuery = require("jquery")(window);
  38. // See ticket #14549 for more info.
  39. module.exports = global.document ?
  40. factory(global, true) :
  41. function (w) {
  42. if (!w.document) {
  43. throw new Error("eUtils requires a window with a document");
  44. }
  45. return factory(w);
  46. };
  47. } else {
  48. factory(global);
  49. }
  50.  
  51. }(typeof window !== "undefined" ? window : this, function (_global, noGlobal) {
  52.  
  53. // base
  54. var BaseUtils = {
  55. /**
  56. * 对象是否为数组
  57. * @param arr
  58. */
  59. isArray: function (arr) {
  60. return Array.isArray(arr) || Object.prototype.toString.call(arr) === "[object Array]";
  61. },
  62. /**
  63. * 判断是否为方法
  64. * @param func
  65. * @return {boolean}
  66. */
  67. isFunction: function (func) {
  68. if (!func) {
  69. return false;
  70. }
  71. return typeof func === 'function';
  72. },
  73. /**
  74. * 判断是否是一个有效的对象
  75. * @param obj
  76. * @return {*|boolean}
  77. */
  78. isExistObject: function (obj) {
  79. return obj && (typeof obj === 'object');
  80. },
  81. isString: function (str) {
  82. if (str === null) {
  83. return false;
  84. }
  85. return typeof str === 'string';
  86. },
  87. uniqueNum: 1000,
  88. /**
  89. * 根据当前时间戳生产一个随机id
  90. * @returns {string}
  91. */
  92. buildUniqueId: function () {
  93. var prefix = new Date().getTime().toString();
  94. var suffix = this.uniqueNum.toString();
  95. this.uniqueNum++;
  96. return prefix + suffix;
  97. }
  98. };
  99.  
  100. //
  101. var serviceProvider = {
  102. _parseDepends: function (depends) {
  103. var dependsArr = [];
  104. if (!BaseUtils.isArray(depends)) {
  105. return;
  106. }
  107. depends.forEach(function (depend) {
  108. if (BaseUtils.isString(depend)) {
  109. dependsArr.push(serviceProvider[depend.toLowerCase()]);
  110. }
  111. });
  112. return dependsArr;
  113. }
  114. };
  115.  
  116. //
  117. var factory = function (name, depends, construction) {
  118. if (!BaseUtils.isFunction(construction)) {
  119. return;
  120. }
  121. serviceProvider[name.toLowerCase()] = construction.apply(this, serviceProvider._parseDepends(depends));
  122. };
  123.  
  124. var depend = function (depends, construction) {
  125. if (!BaseUtils.isFunction(construction)) {
  126. return;
  127. }
  128. construction.apply(this, serviceProvider._parseDepends(depends));
  129. };
  130.  
  131. factory('BaseUtils', [], function () {
  132. return BaseUtils;
  133. });
  134.  
  135. // logger
  136. factory('logger', [], function () {
  137. return console;
  138. });
  139.  
  140. // DateTimeUtils
  141. factory('DateTimeUtils', ['logger'], function (logger) {
  142. return {
  143. /**
  144. * 打印当前时间
  145. */
  146. printNowTime: function () {
  147. var date = new Date();
  148. console.log(this.pattern(date, 'hh:mm:ss:S'));
  149. },
  150. /**
  151. * 格式化日期
  152. * @param date
  153. * @param fmt
  154. * @returns {*}
  155. */
  156. pattern: function (date, fmt) {
  157. var o = {
  158. "M+": date.getMonth() + 1, //月份
  159. "d+": date.getDate(), //日
  160. "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, //小时
  161. "H+": date.getHours(), //小时
  162. "m+": date.getMinutes(), //分
  163. "s+": date.getSeconds(), //秒
  164. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  165. "S": date.getMilliseconds() //毫秒
  166. };
  167. var week = {
  168. "0": "/u65e5",
  169. "1": "/u4e00",
  170. "2": "/u4e8c",
  171. "3": "/u4e09",
  172. "4": "/u56db",
  173. "5": "/u4e94",
  174. "6": "/u516d"
  175. };
  176. if (/(y+)/.test(fmt)) {
  177. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  178. }
  179. if (/(E+)/.test(fmt)) {
  180. fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[date.getDay() + ""]);
  181. }
  182. for (var k in o) {
  183. if (new RegExp("(" + k + ")").test(fmt)) {
  184. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  185. }
  186. }
  187. return fmt;
  188. },
  189. /**
  190. * 以当前时间获取id
  191. * @returns {number}
  192. */
  193. getCurrentId: function () {
  194. var date = new Date();
  195. return date.getTime();
  196. },
  197. /**
  198. * 获取指定时间距离现在相差多久
  199. * @param date {number|Date}
  200. * @param isCeil{boolean=} 是否对结果向上取整,默认[false]
  201. * @param type {string=} 单位可取值['day','month','year']默认'day'
  202. * @returns {number}
  203. */
  204. getNowBetweenADay: function (date, isCeil, type) {
  205. if (!type) {
  206. type = 'day'
  207. }
  208. if (typeof date === 'number') {
  209. date = new Date(date);
  210. }
  211. if (!(date instanceof Date)) {
  212. throw new TypeError('该参数类型必须是Date')
  213. }
  214. var time = date.getTime();
  215. var now = new Date();
  216. var nowTime = now.getTime();
  217. if (nowTime - time < 0) {
  218. logger.warn('需要计算的时间必须在当前时间之前');
  219. }
  220. var result = 0;
  221. switch (type) {
  222. default:
  223. case 'day':
  224. result = (nowTime - time) / (1000 * 60 * 60 * 24);
  225. break;
  226. case 'month':
  227. var yearDifference = now.getFullYear() - date.getFullYear();
  228. if (yearDifference > 0) {
  229. result += yearDifference * 12;
  230. }
  231. result += now.getMonth() - date.getMonth();
  232. break;
  233. case 'year':
  234. result += now.getFullYear() - date.getFullYear();
  235. break;
  236. }
  237. if (!isCeil) {
  238. return Math.floor(result);
  239. } else {
  240. if (result === 0 && isCeil) {
  241. result = 1;
  242. }
  243. return Math.ceil(result);
  244. }
  245. }
  246. }
  247. });
  248.  
  249. // ArrayUtils
  250. factory('ArrayUtils', ['BaseUtils'], function (BaseUtils) {
  251. return {
  252. isArrayObject: function (arr) {
  253. return BaseUtils.isArray(arr);
  254. },
  255. /**
  256. * 遍历数组
  257. * @param context {Object}
  258. * @param arr {Array}
  259. * @param cb {Function} 回调函数
  260. */
  261. ergodicArrayObject: function (context, arr, cb) {
  262. if (!context) {
  263. context = window;
  264. }
  265. if (!BaseUtils.isArray(arr) || !BaseUtils.isFunction(cb)) {
  266. return;
  267. }
  268. for (var i = 0; i < arr.length; i++) {
  269. var result = cb.call(context, arr[i], i);
  270. if (result && result === -1) {
  271. break;
  272. }
  273. }
  274. },
  275. /**
  276. * 获取数组对象的一个属性发起动作
  277. * @param context {Object}
  278. * @param arr {Array}
  279. * @param propertyName {String}
  280. * @param cb {Function}
  281. * @param checkProperty {boolean} 是否排除不拥有该属性的对象[default:true]
  282. */
  283. getPropertyDo: function (context, arr, propertyName, cb, checkProperty) {
  284. if (checkProperty === null) {
  285. checkProperty = true;
  286. }
  287. this.ergodicArrayObject(context, arr, function (ele) {
  288. if (!checkProperty || ele.hasOwnProperty(propertyName)) {
  289. cb.call(context, ele[propertyName], ele);
  290. }
  291. })
  292. },
  293. /**
  294. * [私有方法]将多个键值对对象转换为map
  295. * @param arr {Array}
  296. * @returns {{}}
  297. */
  298. parseKeyValue: function (arr) {
  299. var map = {};
  300. if (!(BaseUtils.isArray(arr))) {
  301. return map;
  302. }
  303. this.ergodicArrayObject(this, arr, function (ele) {
  304. if (ele.key === null) {
  305. return;
  306. }
  307. if (!map.hasOwnProperty(ele.key)) {
  308. map[ele.key] = ele.value;
  309. }
  310. });
  311. return map;
  312. },
  313. /**
  314. * 获取数组的哈希码
  315. * @param arr {Array}
  316. * @returns {number}
  317. */
  318. getHashCode: function (arr) {
  319. var str = arr.toString();
  320. var hash = 31;
  321. if (str.length === 0) return hash;
  322. for (var i = 0; i < str.length; i++) {
  323. var char = str.charCodeAt(i);
  324. hash = ((hash << 5) - hash) + char;
  325. hash = hash & hash; // Convert to 32bit integer
  326. }
  327. return hash;
  328. },
  329. /**
  330. * 通过数组中每个对象的指定属性生成一个新数组
  331. * @param arr {Array}
  332. * @param propertyName {String}
  333. */
  334. parseArrayByProperty: function (arr, propertyName) {
  335. var result = [];
  336. if (!this.isArrayObject(arr)) {
  337. return result;
  338. }
  339. this.getPropertyDo(this, arr, propertyName, function (value) {
  340. result.push(value);
  341. }, true);
  342. return result;
  343. },
  344. /**
  345. * 数组对象是否包含一个对象
  346. * @param arr {Array}
  347. * @param obj
  348. * @param cb {function=}
  349. * @returns {boolean}
  350. */
  351. isContainsObject: function (arr, obj, cb) {
  352. var isContainsObject = false;
  353. this.ergodicArrayObject(this, arr, function (value, i) {
  354. if (obj === value) {
  355. isContainsObject = true;
  356. if (BaseUtils.isFunction(cb)) {
  357. cb.call(window, i);
  358. }
  359. return -1;
  360. }
  361. });
  362. return isContainsObject;
  363. },
  364. /**
  365. * 获取数组中的最大值
  366. * @param arr 若数组中的对象还是数组,则按里面数组的每个对象进行多级比较
  367. * @param cb
  368. * @returns {*}
  369. */
  370. getMaxInArray: function (arr, cb) {
  371. var maxObject = null;
  372. var maxIndex = -1;
  373. while (maxObject === null && maxIndex < arr.length) {
  374. maxObject = arr[++maxIndex]
  375. }
  376. for (var i = maxIndex + 1; i < arr.length; i++) {
  377. // 若是比较对象都是数组,则对每个数组的第一个元素进行比较,若相同,则比较第二个元素
  378. if (maxObject !== null && this.isArrayObject(maxObject) && this.isArrayObject(arr[i])) {
  379. var classLength = maxObject.length;
  380. var classLevel = 0;
  381. // console.log(maxObject[classLevel],arr[i][classLevel]);
  382. while (maxObject[classLevel] === arr[i][classLevel] && classLevel < classLength) {
  383. classLevel++
  384. }
  385. if (maxObject[classLevel] !== null && maxObject[classLevel] < arr[i][classLevel]) {
  386. maxObject = arr[i];
  387. maxIndex = i;
  388. }
  389. continue;
  390. }
  391. if (maxObject !== null && maxObject < arr[i]) {
  392. maxObject = arr[i];
  393. maxIndex = i;
  394. }
  395. }
  396. if (BaseUtils.isFunction(cb)) {
  397. cb.call(this, maxObject, maxIndex);
  398. }
  399. return maxObject;
  400. },
  401. /**
  402. * 获取数组中的总值
  403. * @param arr{Array<number>}
  404. * @param cb {function=}
  405. */
  406. getSumInArray: function (arr, cb) {
  407. if (!this.isArrayObject(arr)) {
  408. return;
  409. }
  410. var sum = 0;
  411. var count = 0;
  412. this.ergodicArrayObject(this, arr, function (value) {
  413. if (typeof value === 'number' && !Number.isNaN(value)) {
  414. sum += value;
  415. count += 1;
  416. }
  417. });
  418. if (BaseUtils.isFunction(cb)) {
  419. cb.call(window, sum, count);
  420. }
  421. return sum;
  422. },
  423. /**
  424. * 获取数组中的平均值
  425. * @param arr{Array<number>}
  426. */
  427. getAverageInArray: function (arr) {
  428. var average = 0;
  429. this.getSumInArray(arr, function (sum, i) {
  430. i === 0 || (average = sum / i);
  431. });
  432. return average;
  433. },
  434. /**
  435. * 为数组排序
  436. * @param arr
  437. * @param order
  438. * @param sortSetting {object=}
  439. */
  440. sortingArrays: function (arr, order, sortSetting) {
  441. if (!this.isArrayObject(arr)) {
  442. return;
  443. }
  444. var DESC = 0;
  445. var ASC = 1;
  446. var thisArr = arr.slice(0);
  447. var _thisAction = null;
  448. // 解析配置
  449. var isSetting = sortSetting && sortSetting.getComparedProperty &&
  450. BaseUtils.isFunction(sortSetting.getComparedProperty);
  451. if (isSetting) {
  452. thisArr = sortSetting.getComparedProperty(arr);
  453. }
  454. switch (order) {
  455. default :
  456. case DESC:
  457. _thisAction = thisArr.push;
  458. break;
  459. case ASC:
  460. _thisAction = thisArr.unshift;
  461. break;
  462. }
  463. var resultArr = [];
  464. for (var j = 0; j < thisArr.length; j++) {
  465. this.getMaxInArray(thisArr, function (max, i) {
  466. delete thisArr[i];
  467. _thisAction.call(resultArr, arr[i]);
  468. });
  469. }
  470. if (sortSetting && sortSetting.createNewData) {
  471. return resultArr.slice(0);
  472. }
  473. return resultArr;
  474. },
  475. /**
  476. * 将类数组转化为数组
  477. * @param arrLike 类数组对象
  478. */
  479. toArray: function (arrLike) {
  480. if (!arrLike || arrLike.length === 0) {
  481. return [];
  482. }
  483. // 非伪类对象,直接返回最好
  484. if (!arrLike.length) {
  485. return arrLike;
  486. }
  487. // 针对IE8以前 DOM的COM实现
  488. try {
  489. return [].slice.call(arrLike);
  490. } catch (e) {
  491. var i = 0,
  492. j = arrLike.length,
  493. res = [];
  494. for (; i < j; i++) {
  495. res.push(arrLike[i]);
  496. }
  497. return res;
  498. }
  499. },
  500. /**
  501. * 判断是否为类数组
  502. * @param o
  503. * @returns {boolean}
  504. */
  505. isArrayLick: function (o) {
  506. if (o && // o is not null, undefined, etc.
  507. typeof o === 'object' && // o is an object
  508. isFinite(o.length) && // o.length is a finite number
  509. o.length >= 0 && // o.length is non-negative
  510. o.length === Math.floor(o.length) && // o.length is an integer
  511. o.length < 4294967296) // o.length < 2^32
  512. return true; // Then o is array-like
  513. else
  514. return false; // Otherwise it is not
  515.  
  516. },
  517. /**
  518. * 判断数组是否包含对象
  519. * @param arr
  520. * @param obj
  521. */
  522. contains: function (arr, obj) {
  523. var contains = false;
  524. this.ergodicArrayObject(this, arr, function (a) {
  525. if (a === obj) {
  526. contains = true;
  527. return -1;
  528. }
  529. });
  530. return contains;
  531. }
  532. }
  533. });
  534.  
  535. // ObjectUtils
  536. factory('ObjectUtils', ['ArrayUtils', 'BaseUtils'], function (ArrayUtils, BaseUtils) {
  537. return {
  538. /**
  539. * 获取对象属性[支持链式表达式,如获取学生所在班级所在的学校(student.class.school):'class.school']
  540. * @param obj
  541. * @param linkProperty {string|Array} 属性表达式,获取多个属性则用数组
  542. * @param cb {function=}
  543. * @return 对象属性
  544. */
  545. readLinkProperty: function (obj, linkProperty, cb) {
  546. var callback = null;
  547. if (BaseUtils.isFunction(cb)) {
  548. callback = cb;
  549. }
  550. if (typeof linkProperty === 'string') {
  551. // 除去所有的空格
  552. linkProperty = linkProperty.replace(/ /g, '');
  553. // 不判断为空的值
  554. if (linkProperty === '') {
  555. return null;
  556. }
  557. // 若是以','隔开的伪数组,则转化为数组再进行操作
  558. if (linkProperty.indexOf(',') !== -1) {
  559. var propertyNameArr = linkProperty.split(',');
  560. return this.readLinkProperty(obj, propertyNameArr, callback);
  561. }
  562. if (linkProperty.indexOf('.') !== -1) {
  563. var names = linkProperty.split('.');
  564. var iterationObj = obj;
  565. var result = null;
  566. ArrayUtils.ergodicArrayObject(this, names, function (name, i) {
  567. iterationObj = this.readLinkProperty(iterationObj, name);
  568. if (names[names.length - 1] === name && names.length - 1 === i) {
  569. result = iterationObj;
  570. if (callback) {
  571. callback.call(window, result, linkProperty);
  572. }
  573. }
  574. // 终止对接下来的属性的遍历
  575. if (typeof iterationObj === 'undefined') {
  576. return -1;
  577. }
  578. });
  579. return result;
  580. }
  581. var normalResult = null;
  582. if (linkProperty.slice(linkProperty.length - 2) === '()') {
  583. var func = linkProperty.slice(0, linkProperty.length - 2);
  584. normalResult = obj[func]();
  585. } else {
  586. normalResult = obj[linkProperty];
  587. }
  588. if (normalResult === null) {
  589. console.warn(obj, '的属性[\'' + linkProperty + '\']值未找到');
  590. }
  591. if (callback) {
  592. callback.call(window, normalResult, linkProperty);
  593. }
  594. return normalResult;
  595. }
  596. if (BaseUtils.isArray(linkProperty)) {
  597. var results = [];
  598. ArrayUtils.ergodicArrayObject(this, linkProperty, function (name) {
  599. var value = this.readLinkProperty(obj, name);
  600. results.push(value);
  601. if (callback && name !== '') {
  602. return callback.call(window, value, name);
  603. }
  604. });
  605. results.isMultipleResults = true;
  606. return results;
  607. }
  608. },
  609. /**
  610. * 为对象属性赋值
  611. * (同一个对象中不能够既有数字开头的属性名和普通属性名)
  612. * @param obj
  613. * @param linkProperty {string|Array} 属性表达式,多个属性则用数组
  614. * @param value
  615. */
  616. createLinkProperty: function (obj, linkProperty, value) {
  617. if (obj === null) {
  618. obj = {};
  619. }
  620. if (typeof linkProperty === 'string') {
  621. // 除去所有的空格
  622. linkProperty = linkProperty.replace(/ /g, '');
  623. // 不判断为空的值
  624. if (linkProperty === '') {
  625. throw new TypeError('对象属性名不能为空')
  626. }
  627. if (linkProperty.indexOf(',') !== -1) {
  628. var propertyNameArr = linkProperty.split(',');
  629. this.createLinkProperty(obj, propertyNameArr, value);
  630. return obj;
  631. }
  632. if (linkProperty.indexOf('.') !== -1) {
  633. var names = linkProperty.split('.');
  634. if (!obj.hasOwnProperty(names[0])) {
  635. obj[names[0]] = {}
  636. }
  637. // 判断属性名是否以数字开头(若是代表是一个数组)
  638. if (!Number.isNaN(parseInt(names[0]))) {
  639. if (!ArrayUtils.isArrayObject(obj)) {
  640. obj = [];
  641. }
  642. }
  643. var propertyObj = obj[names[0]];
  644. var newProperties = names.slice(1, names.length);
  645. var newLinkProperty = '';
  646. ArrayUtils.ergodicArrayObject(this, newProperties, function (property, i) {
  647. if (i < newProperties.length - 1) {
  648. newLinkProperty = newLinkProperty + property + '.'
  649. } else {
  650. newLinkProperty = newLinkProperty + property;
  651. }
  652. });
  653. obj[names[0]] = this.createLinkProperty(propertyObj, newLinkProperty, value);
  654. return obj;
  655. }
  656. // 判断属性名是否以数字开头(若是代表是一个数组)
  657. if (!Number.isNaN(parseInt(linkProperty))) {
  658. if (!ArrayUtils.isArrayObject(obj)) {
  659. obj = [];
  660. }
  661. }
  662. obj[linkProperty] = value;
  663. return obj;
  664. } else if (BaseUtils.isArray(linkProperty)) {
  665. ArrayUtils.ergodicArrayObject(this, linkProperty, function (link) {
  666. obj = this.createLinkProperty(obj, link, value);
  667. });
  668. return obj;
  669. }
  670. },
  671. /**
  672. * 遍历对象属性
  673. * @param context {object} 上下文
  674. * @param obj {object} 遍历对象
  675. * @param cb {function} 回调函数
  676. * @param isReadInnerObject {boolean=} 是否遍历内部对象的属性
  677. */
  678. ergodicObject: function (context, obj, cb, isReadInnerObject) {
  679. var keys = Object.keys(obj);
  680. ArrayUtils.ergodicArrayObject(this, keys, function (propertyName) {
  681. // 若内部对象需要遍历
  682. var _propertyName = propertyName;
  683. if (isReadInnerObject && obj[propertyName] !== null && typeof obj[propertyName] === 'object') {
  684. this.ergodicObject(this, obj[propertyName], function (value, key) {
  685. return cb.call(context, value, _propertyName + '.' + key);
  686. }, true)
  687. } else {
  688. return cb.call(context, obj[propertyName], propertyName);
  689. }
  690. })
  691. },
  692. /**
  693. * 当指定属性为空或不存在时执行回到函数;
  694. * @param context {object} 上下文
  695. * @param obj {object} 检测对象
  696. * @param propertyNames{Array|string} 需要检测的属性名
  697. * 可以检查多级属性如:'a.b.c.e',
  698. * 多个属性使用数组,支持纯字符串多个属性用','隔开
  699. * @param cb {function} 回调函数[参数:为空或不存在的属性名,返回值为'-1'时,跳过之后的回调函数]
  700. */
  701. whileEmptyObjectProperty: function (context, obj, propertyNames, cb) {
  702. // 解析单个属性名
  703. if (typeof propertyNames === 'string') {
  704. // 除去所有的空格
  705. propertyNames = propertyNames.replace(/ /g, '');
  706. // 不判断为空的值
  707. if (propertyNames === '') {
  708. return;
  709. }
  710. // 若是以','隔开的伪数组,则转化为数组再进行操作
  711. if (propertyNames.indexOf(',') !== -1) {
  712. var propertyNameArr = propertyNames.split(',');
  713. return this.whileEmptyObjectProperty(context, obj, propertyNameArr, cb);
  714. }
  715. // 若指定级联属性
  716. if (propertyNames.indexOf('.') !== -1) {
  717. var names = propertyNames.split('.');
  718. var iterationObj = obj;
  719. var result = null;
  720. ArrayUtils.ergodicArrayObject(this, names, function (name) {
  721. if (iterationObj && iterationObj.hasOwnProperty(name)) {
  722. iterationObj = iterationObj[name];
  723. } else {
  724. result = cb.call(window, propertyNames);
  725. // 终止对接下来的属性的遍历
  726. return -1;
  727. }
  728. });
  729. return result;
  730. }
  731. // 正常流程
  732. if (!obj.hasOwnProperty(propertyNames)) {
  733. return cb.call(context, propertyNames);
  734. }
  735. if (obj[propertyNames] === null || obj[propertyNames] === '') {
  736. return cb.call(context, propertyNames);
  737. }
  738. } else if (BaseUtils.isArray(propertyNames)) {
  739. // 解析数组
  740. ArrayUtils.ergodicArrayObject(this, propertyNames, function (propertyName) {
  741. // 递归调用
  742. return this.whileEmptyObjectProperty(context, obj, propertyName, cb);
  743. })
  744. }
  745. },
  746. whileEmptyObjectPropertyV2: function (context, obj, propertyNames, cb) {
  747. this.readLinkProperty(obj, propertyNames, function (value, propertyName) {
  748. if (value === null || value === '' || parseInt(value) < 0) {
  749. return cb.call(context, propertyName);
  750. }
  751. })
  752. },
  753. /**
  754. * 克隆对象[只克隆属性,不克隆原型链]
  755. * @param obj {*}
  756. */
  757. cloneObject: function (obj) {
  758. var newObj = {};
  759. // 判断是否为基本数据类型,若是则直接返回
  760. if (typeof obj === 'string' ||
  761. typeof obj === 'number' ||
  762. typeof obj === 'undefined' ||
  763. typeof obj === 'function' ||
  764. typeof obj === 'boolean') {
  765. return obj;
  766. }
  767. // 判断是否是数组
  768. if (BaseUtils.isArray(obj)) {
  769. newObj = [];
  770. // 遍历数组并递归调用该方法获取数组内部对象的克隆对象并push到新数组
  771. ArrayUtils.ergodicArrayObject(this, obj, function (arrObjValue) {
  772. newObj.push(this.cloneObject(arrObjValue));
  773. })
  774. } else if (typeof obj === 'object') {
  775. // 当目标为一般对象时即 typeof 为 object
  776. if (obj === null) {
  777. // 当克隆对象为空时,返回空
  778. return null;
  779. }
  780. // 遍历对象的属性并调用递归方法获得该属性对应的对象的克隆对象并将其重新赋值到该属性
  781. this.ergodicObject(this, obj, function (value, key) {
  782. newObj[key] = this.cloneObject(value);
  783. });
  784. }
  785. return newObj;
  786. },
  787. // cloneIndeed: function (obj) {
  788. // var hash = new Map();
  789. // var result = this._cloneIndeed(obj, hash);
  790. // for (var item of hash.values()) {
  791. // ArrayUtils.ergodicArrayObject(this, item.settingArr, function (func) {
  792. // func.call(this);
  793. // })
  794. // }
  795. // return result;
  796. // },
  797. // _cloneIndeed: function (obj, hash) {
  798. // hash = hash || new Map();
  799. // var result = {};
  800. // // 获取数据类型
  801. // var dataType = typeof obj;
  802. // switch (dataType.toLowerCase()) {
  803. // case 'string':
  804. // case 'number':
  805. // case 'boolean':
  806. // case 'undefined':
  807. // return obj;
  808. // case 'object':
  809. // default: {
  810. // for (var key in obj) {
  811. // var nextObj = obj[key];
  812. // var hashObj = hash.get(nextObj);
  813. // if (hashObj != null && hashObj.clonedObj != null) {
  814. // obj[key] = null;
  815. // }
  816. // hash.set(nextObj, {
  817. // clonedObj: result,
  818. // settingArr: [],
  819. // active: false
  820. // }
  821. // );
  822. // result[key] = this._cloneIndeed(nextObj, hash);
  823. // }
  824. // if (obj != null) {
  825. // result['__proto__'] = obj['__proto__'];
  826. // }
  827. // }
  828. //
  829. // }
  830. // return result;
  831. // },
  832. /**
  833. * 获取对象的哈希码
  834. * @param obj {Object}
  835. * @returns {number}
  836. */
  837. getObjHashCode: function (obj) {
  838. var str = JSON.stringify(obj);
  839. var hash = 0, i, chr, len;
  840. console.log(str)
  841. console.log(hash)
  842. if (str.length === 0) return hash;
  843. for (i = 0, len = str.length; i < len; i++) {
  844. chr = str.charCodeAt(i);
  845. hash = ((hash << 5) - hash) + chr;
  846. hash |= 0; // Convert to 32bit integer
  847. }
  848. console.log(str)
  849. console.log(hash)
  850. return hash;
  851. },
  852. /**
  853. * 扩展对象属性
  854. * @param obj 原对象
  855. * @param extendedObj 被扩展的对象
  856. * @param isCover {boolean=} 扩展的属性和原来属性冲突时是否覆盖 默认[false]
  857. * @param isClone {boolean=} 是否返回一个新的对象,默认[false]返回扩展后的原对象
  858. */
  859. expandObject: function (obj, extendedObj, isCover, isClone) {
  860. var resultObj = obj;
  861. if (isClone) {
  862. resultObj = this.cloneObject(obj);
  863. }
  864. this.ergodicObject(this, extendedObj, function (value, key) {
  865. if (isCover && this.readLinkProperty(resultObj, key) !== null) {
  866. return;
  867. }
  868. resultObj = this.createLinkProperty(resultObj, key, value);
  869. }, true);
  870. return resultObj;
  871. },
  872. /**
  873. * 为数组排序,当数组中的元素为对象时,根据指定对象的属性名进行排序
  874. * @param arr 数组
  875. * @param propertyName 属性名(当有多个属性名时,为多级排序)
  876. * @param order 升降序
  877. * @returns {*}
  878. */
  879. sortingArrayByProperty: function (arr, propertyName, order) {
  880. var _this = this;
  881. var sortSetting = {
  882. // 是否创建新数据
  883. createNewData: false,
  884. // 通过该方法获取数组中每个对象中用来比较的属性
  885. getComparedProperty: function (arr) {
  886. var compareArr = [];
  887. ArrayUtils.ergodicArrayObject(_this, arr, function (obj, i) {
  888. if (typeof obj !== 'object') {
  889. compareArr.push(obj);
  890. } else {
  891. var compareValue = this.readLinkProperty(obj, propertyName);
  892. if (compareValue !== null) {
  893. compareArr.push(compareValue);
  894. } else {
  895. compareArr.push(obj);
  896. }
  897. }
  898. });
  899. return compareArr.slice(0);
  900. }
  901. };
  902. return ArrayUtils.sortingArrays(arr, order, sortSetting);
  903. },
  904. /**
  905. * 转话为目标的实例
  906. * @param constructor {function} 构造函数
  907. * @param obj {object|Array}判断的对象
  908. * @param defaultProperty {object=}
  909. */
  910. toAimObject: function (obj, constructor, defaultProperty) {
  911. if (BaseUtils.isArray(obj)) {
  912. var originArr = [];
  913. ArrayUtils.ergodicArrayObject(this, obj, function (value) {
  914. originArr.push(this.toAimObject(value, constructor, defaultProperty));
  915. });
  916. return originArr;
  917. } else if (typeof obj === 'object') {
  918. if (defaultProperty) {
  919. this.ergodicObject(this, defaultProperty, function (value, key) {
  920. if (obj[key] === null) {
  921. obj[key] = value;
  922. }
  923. });
  924. }
  925. if (obj instanceof constructor) {
  926. return obj;
  927. }
  928. var originObj = obj;
  929. while (originObj.__proto__ !== null && originObj.__proto__ !== Object.prototype) {
  930. originObj = originObj.__proto__;
  931. }
  932. originObj.__proto__ = constructor.prototype;
  933. return originObj;
  934. }
  935. },
  936. /**
  937. * 将数组中结构类似对象指定属性融合为一个数组
  938. * @param arr {Array}
  939. * @param propertyNames
  940. */
  941. parseTheSameObjectPropertyInArray: function (arr, propertyNames) {
  942. var result = {};
  943. var temp = {};
  944. ArrayUtils.ergodicArrayObject(this, arr, function (obj) {
  945. // 获取想要得到的所有属性,以属性名为键值存储到temp中
  946. this.readLinkProperty(obj, propertyNames, function (value, property) {
  947. if (!temp.hasOwnProperty(property) || !(BaseUtils.isArray(temp[property]))) {
  948. temp[property] = [];
  949. }
  950. temp[property].push(value);
  951. });
  952. });
  953. // 遍历temp获取每个键值中的值,并单独取出
  954. this.ergodicObject(this, temp, function (value, key) {
  955. result = this.createLinkProperty(result, key, value);
  956. });
  957. return this.cloneObject(result);
  958. },
  959. /**
  960. * 将数组中结构类似对象指定属性融合为一个数组
  961. * @param arr {Array}
  962. */
  963. parseTheSameObjectAllPropertyInArray: function (arr) {
  964. if (!ArrayUtils.isArrayObject(arr) || arr.length < 1) {
  965. return;
  966. }
  967. // 获取一个对象的所有属性,包括内部对象的属性
  968. var propertyNames = [];
  969. this.ergodicObject(this, arr[0], function (v, k) {
  970. propertyNames.push(k);
  971. }, true);
  972. return this.parseTheSameObjectPropertyInArray(arr, propertyNames);
  973. },
  974. /**
  975. * 获取对象属性,若为数组则计算其中数字的平均值或其它
  976. * @param obj
  977. * @param propertyNames{Array<string>|string}
  978. * @param type
  979. */
  980. getCalculationInArrayByLinkPropertyNames: function (obj, propertyNames, type) {
  981. var resultObject = {};
  982. var _this = this;
  983. switch (type) {
  984. default:
  985. case 'sum':
  986. this.readLinkProperty(obj, propertyNames, function (value, key) {
  987. if (ArrayUtils.isArrayObject(value)) {
  988. resultObject = _this.createLinkProperty(resultObject, key, ArrayUtils.getSumInArray(value));
  989. }
  990. });
  991. break;
  992. case 'average':
  993. this.readLinkProperty(obj, propertyNames, function (value, key) {
  994. if (ArrayUtils.isArrayObject(value)) {
  995. resultObject = _this.createLinkProperty(resultObject, key, ArrayUtils.getAverageInArray(value));
  996. }
  997. });
  998. break;
  999. }
  1000. return resultObject;
  1001. }
  1002. }
  1003. });
  1004.  
  1005. // ColorUtils
  1006. factory('ColorUtils', [], function () {
  1007. return {
  1008. /**
  1009. * 转换颜色rgb为16进制
  1010. * @param r
  1011. * @param g
  1012. * @param b
  1013. * @return {string}
  1014. */
  1015. rgbToHex: function (r, g, b) {
  1016. var hex = ((r << 16) | (g << 8) | b).toString(16);
  1017. return "#" + new Array(Math.abs(hex.length - 7)).join("0") + hex;
  1018. },
  1019. /**
  1020. * 转换颜色16进制为rgb
  1021. * @param hex
  1022. * @return {Array}
  1023. */
  1024. hexToRgb: function (hex) {
  1025. hex = hex.replace(/ /g, '');
  1026. var length = hex.length;
  1027. var rgb = [];
  1028. switch (length) {
  1029. case 4:
  1030. rgb.push(parseInt(hex[1] + hex[1], 16));
  1031. rgb.push(parseInt(hex[2] + hex[2], 16));
  1032. rgb.push(parseInt(hex[3] + hex[3], 16));
  1033. return rgb;
  1034. case 7:
  1035. for (var i = 1; i < 7; i += 2) {
  1036. rgb.push(parseInt("0x" + hex.slice(i, i + 2)));
  1037. }
  1038. return rgb;
  1039. default:
  1040. break
  1041. }
  1042. },
  1043. /**
  1044. * 根据两个颜色以及之间的百分比获取渐进色
  1045. * @param start
  1046. * @param end
  1047. * @param percentage
  1048. * @return {*}
  1049. */
  1050. gradientColorsPercentage: function (start, end, percentage) {
  1051. var resultRgb = [];
  1052. var startRgb = this.hexToRgb(start);
  1053. if (end == null) {
  1054. return start;
  1055. }
  1056. var endRgb = this.hexToRgb(end);
  1057. var totalR = endRgb[0] - startRgb[0];
  1058. var totalG = endRgb[1] - startRgb[1];
  1059. var totalB = endRgb[2] - startRgb[2];
  1060. resultRgb.push(startRgb[0] + totalR * percentage);
  1061. resultRgb.push(startRgb[1] + totalG * percentage);
  1062. resultRgb.push(startRgb[2] + totalB * percentage);
  1063. return this.rgbToHex(resultRgb[0], resultRgb[1], resultRgb[2])
  1064. }
  1065. }
  1066. });
  1067.  
  1068. factory('FunctionUtils', [], function () {
  1069. return {
  1070. /**
  1071. * 获取方法的名字
  1072. * @param func
  1073. * @returns {*}
  1074. */
  1075. getFunctionName: function (func) {
  1076. if (typeof func === 'function' || typeof func === 'object') {
  1077. var name = ('' + func).match(/function\s*([\w\$]*)\s*\(/);
  1078. }
  1079. return func.name || name[1];
  1080. },
  1081. /**
  1082. * 获取方法的参数名
  1083. * @param func
  1084. * @returns {*}
  1085. */
  1086. getFunctionParams: function (func) {
  1087. if (typeof func === 'function' || typeof func === 'object') {
  1088. var name = ('' + func).match(/function.*\(([^)]*)\)/);
  1089. return name[1].replace(/( )|(\n)/g, '').split(',');
  1090. }
  1091. return;
  1092. },
  1093. /**
  1094. * 通过方法的arguments获取调用该方法的函数
  1095. * @param func_arguments
  1096. * @returns {string}
  1097. */
  1098. getCallerName: function (func_arguments) {
  1099. var caller = func_arguments.callee.caller;
  1100. var callerName = '';
  1101. if (caller) {
  1102. callerName = this.getFunctionName(caller);
  1103. }
  1104. return callerName;
  1105. },
  1106. FunctionBuilder: function (func) {
  1107. var _this = this;
  1108. var fs = [];
  1109. fs.push(func);
  1110. var properties = ['push', 'unshift', 'slice', 'map', 'forEach', 'keys', 'find', 'concat', 'fill', 'shift', 'values'];
  1111. properties.map(function (property) {
  1112. if (typeof Array.prototype[property] === 'function') {
  1113. Object.defineProperty(_this, property, {
  1114. get: function () {
  1115. return function () {
  1116. fs[property].apply(fs, arguments);
  1117. return this;
  1118. }
  1119. }
  1120. });
  1121. }
  1122. });
  1123. this.result = function (context) {
  1124. var rfs = [];
  1125. fs.map(function (f, index) {
  1126. if (typeof f === 'function') {
  1127. rfs.push(f);
  1128. }
  1129. });
  1130. return function () {
  1131. var declareVar = {
  1132. arguments: arguments,
  1133. this: this
  1134. };
  1135. rfs.map(function (f) {
  1136. var dv = f.apply(context || this, [declareVar]);
  1137. if (dv) {
  1138. Object.keys(dv).map(function (key) {
  1139. declareVar[key] = dv[key];
  1140. });
  1141. }
  1142. });
  1143. return declareVar.returnValue;
  1144. }
  1145. }
  1146. },
  1147. invokeMethods: function (context, methods, args) {
  1148. if (!this.isArray(methods)) {
  1149. return;
  1150. }
  1151. var results = [];
  1152. var _this = this;
  1153. this.ergodicArrayObject(context, methods, function (method) {
  1154. if (!_this.isFunction(method)) {
  1155. return;
  1156. }
  1157. results.push(
  1158. method.apply(context, args)
  1159. );
  1160. });
  1161. return results;
  1162. }
  1163. }
  1164. });
  1165.  
  1166. factory('UrlUtils', [], function () {
  1167. return {
  1168. getUrlInfo: function (url) {
  1169. var a = document.createElement('a');
  1170. a.href = url;
  1171. return {
  1172. source: url,
  1173. protocol: a.protocol.replace(':', ''),
  1174. host: a.hostname,
  1175. port: a.port,
  1176. query: a.search,
  1177. file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
  1178. hash: a.hash.replace('#', ''),
  1179. path: a.pathname.replace(/^([^\/])/, '/$1'),
  1180. relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
  1181. segments: a.pathname.replace(/^\//, '').split('/'),
  1182. params: (function () {
  1183. var ret = {};
  1184. var seg = a.search.replace(/^\?/, '').split('&').filter(function (v, i) {
  1185. if (v !== '' && v.indexOf('=')) {
  1186. return true;
  1187. }
  1188. });
  1189. seg.forEach(function (element, index) {
  1190. var idx = element.indexOf('=');
  1191. var key = element.substring(0, idx);
  1192. var val = element.substring(idx + 1);
  1193. ret[key] = val;
  1194. });
  1195. return ret;
  1196. })()
  1197. };
  1198. },
  1199. urlMatching: function (url, matchUrl) {
  1200. var pattern = new RegExp(matchUrl);
  1201. return pattern.test(url);
  1202. },
  1203. getUrlWithoutParam: function (url) {
  1204. return url.split('?')[0];
  1205. },
  1206. getParamFromUrl: function (url) {
  1207. var params = [];
  1208. var paramsObject = this.getUrlInfo(url).params;
  1209. Object.keys(paramsObject).forEach(function (key) {
  1210. params.push({
  1211. key: key,
  1212. value: paramsObject[key]
  1213. })
  1214. });
  1215. // var parts = url.split('?');
  1216. // if (parts.length < 2) {
  1217. // return params;
  1218. // }
  1219. // var paramsStr = parts[1].split('&');
  1220. // for (var i = 0; i < paramsStr.length; i++) {
  1221. // var index = paramsStr[i].indexOf('=');
  1222. // var ps = new Array(2);
  1223. // if (index !== -1) {
  1224. // ps = [
  1225. // paramsStr[i].substring(0, index),
  1226. // paramsStr[i].substring(index + 1),
  1227. // ];
  1228. // } else {
  1229. // ps[0] = paramsStr[i];
  1230. // }
  1231. // params.push({
  1232. // key: ps[0],
  1233. // value: ps[1]
  1234. // });
  1235. // }
  1236. return params;
  1237. },
  1238. margeUrlAndParams: function (url, params) {
  1239. if (url.indexOf('?') !== -1 || !(params instanceof Array)) {
  1240. return url;
  1241. }
  1242. var paramsStr = [];
  1243. for (var i = 0; i < params.length; i++) {
  1244. if (params[i].key !== null && params[i].value !== null) {
  1245. if (!params[i].key) {
  1246. paramsStr.push(params[i].value);
  1247. } else {
  1248. paramsStr.push(params[i].key + '=' + params[i].value);
  1249. }
  1250. }
  1251. }
  1252. return url + '?' + paramsStr.join('&');
  1253. }
  1254. }
  1255. });
  1256.  
  1257. factory('PointUtils', [], function () {
  1258. var Point2D = function (x, y) {
  1259. this.x = x || 0;
  1260. this.y = y || 0;
  1261. };
  1262. Point2D.prototype = {
  1263. constructor: Point2D,
  1264. /**
  1265. * 获取指定距离和角度对应的平面点
  1266. * @param distance
  1267. * @param deg
  1268. */
  1269. getOtherPointFromDistanceAndDeg: function (distance, deg) {
  1270. var radian = Math.PI / 180 * deg;
  1271. var point = new this.constructor();
  1272. point.x = distance * Math.sin(radian) + this.x;
  1273. point.y = this.y - distance * Math.cos(radian);
  1274. return point;
  1275. },
  1276. /**
  1277. * 获取当前平面点与另一个平面点之间的距离
  1278. * @param p
  1279. * @returns {number}
  1280. */
  1281. getDistanceFromAnotherPoint: function (p) {
  1282. return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
  1283. },
  1284. /**
  1285. * 获取当前平面点与另一个平面点之间的角度
  1286. * @param p
  1287. * @returns {number}
  1288. */
  1289. getDegFromAnotherPoint: function (p) {
  1290. var usedPoint = new Point2D(p.x * 1000000 - this.x * 1000000, p.y * 1000000 - this.y * 1000000);
  1291. var radian = Math.atan2(usedPoint.x * 1000000, usedPoint.y * 1000000);
  1292. var deg = radian * 180 / Math.PI;
  1293. return 180 - deg;
  1294. },
  1295. /**
  1296. * 判断该点是否位于一矩形内部
  1297. * @param x 矩形开始坐标x
  1298. * @param y 矩形开始坐标y
  1299. * @param width 矩形宽
  1300. * @param height 矩形长
  1301. * @returns {boolean}
  1302. */
  1303. isInRect: function (x, y, width, height) {
  1304. var px = this.x;
  1305. var py = this.y;
  1306. if (px < x || px > x + width) {
  1307. return false;
  1308. }
  1309. return !(py < y || py > y + height);
  1310. }
  1311. };
  1312. return {
  1313. Point2D: Point2D
  1314. }
  1315. });
  1316.  
  1317. _global.everyUtils = function () {
  1318. if (BaseUtils.isArray(arguments[0])) {
  1319. depend.call(arguments[2] || this, arguments[0], arguments[1]);
  1320. } else if (BaseUtils.isFunction(arguments[0])) {
  1321. var args = arguments;
  1322. depend.call(arguments[1] || this, ['FunctionUtils'], function (FunctionUtils) {
  1323. var depends = FunctionUtils.getFunctionParams(args[0]);
  1324. depend(depends, args[0]);
  1325. })
  1326. }
  1327. };
  1328.  
  1329. _global.eUtils = (function () {
  1330. var utils = {};
  1331. if (window.everyUtils) {
  1332. window.everyUtils(function (
  1333. ArrayUtils,
  1334. ObjectUtils,
  1335. BaseUtils,
  1336. FunctionUtils,
  1337. ColorUtils,
  1338. PointUtils,
  1339. UrlUtils) {
  1340. utils = {
  1341. ArrayUtils: ArrayUtils,
  1342. ObjectUtils: ObjectUtils,
  1343. BaseUtils: BaseUtils,
  1344. ColorUtils: ColorUtils,
  1345. UrlUtils: UrlUtils,
  1346. urlUtils: UrlUtils,
  1347. PointUtils: PointUtils,
  1348. FunctionUtils: FunctionUtils
  1349. };
  1350. });
  1351. }
  1352. var proxy = {};
  1353. Object.keys(utils).forEach(function (utilName) {
  1354. if (!utilName) {
  1355. return;
  1356. }
  1357. Object.defineProperty(proxy, utilName, {
  1358. get: function () {
  1359. return utils[utilName];
  1360. }
  1361. });
  1362. Object.keys(utils[utilName]).forEach(function (key) {
  1363. if (!key) {
  1364. return;
  1365. }
  1366. if (proxy[key]) {
  1367. return;
  1368. }
  1369. Object.defineProperty(proxy, key, {
  1370. get: function () {
  1371. return utils[utilName][key];
  1372. }
  1373. })
  1374. })
  1375. });
  1376. return proxy;
  1377. })();
  1378.  
  1379. return _global.eUtils;
  1380. }));
  1381.  
  1382.  
  1383. ~function (utils) {
  1384. var _global = this;
  1385. var EHook = function () {
  1386. var _autoId = 1;
  1387. var _hookedMap = {};
  1388. this._getHookedMap = function () {
  1389. return _hookedMap;
  1390. };
  1391. this._getAutoStrId = function () {
  1392. return '__auto__' + _autoId++;
  1393. };
  1394. this._getAutoId = function () {
  1395. return _autoId++;
  1396. };
  1397. };
  1398. EHook.prototype = {
  1399. /**
  1400. * 获取一个对象的劫持id,若没有则创建一个
  1401. * @param context
  1402. * @return {*}
  1403. * @private
  1404. */
  1405. _getHookedId: function (context) {
  1406. var hookedId = context.___hookedId;
  1407. if (hookedId == null) {
  1408. hookedId = context.___hookedId = this._getAutoStrId();
  1409. }
  1410. return hookedId;
  1411. },
  1412. /**
  1413. * 获取一个对象的劫持方法映射,若没有则创建一个
  1414. * @param context
  1415. * @return {*}
  1416. * @private
  1417. */
  1418. _getHookedMethodMap: function (context) {
  1419. var hookedId = this._getHookedId(context);
  1420. var hookedMap = this._getHookedMap();
  1421. var thisTask = hookedMap[hookedId];
  1422. if (!utils.isExistObject(thisTask)) {
  1423. thisTask = hookedMap[hookedId] = {};
  1424. }
  1425. return thisTask;
  1426. },
  1427. /**
  1428. * 获取对应方法的hook原型任务对象,若没有则初始化一个。
  1429. * @param context
  1430. * @param methodName
  1431. * @private
  1432. */
  1433. _getHookedMethodTask: function (context, methodName) {
  1434. var thisMethodMap = this._getHookedMethodMap(context);
  1435. var thisMethod = thisMethodMap[methodName];
  1436. if (!utils.isExistObject(thisMethod)) {
  1437. thisMethod = thisMethodMap[methodName] = {
  1438. original: undefined,
  1439. replace: undefined,
  1440. task: {
  1441. before: [],
  1442. current: undefined,
  1443. after: []
  1444. }
  1445. };
  1446. }
  1447. return thisMethod;
  1448. },
  1449. /**
  1450. * 执行多个方法并注入一个方法和参数集合
  1451. * @param context
  1452. * @param methods
  1453. * @param args
  1454. * @return result 最后一次执行方法的有效返回值
  1455. * @private
  1456. */
  1457. _invokeMethods: function (context, methods, args) {
  1458. if (!utils.isArray(methods)) {
  1459. return;
  1460. }
  1461. var result = null;
  1462. utils.ergodicArrayObject(context, methods, function (_method) {
  1463. if (!utils.isFunction(_method.method)) {
  1464. return;
  1465. }
  1466. var r = _method.method.apply(this, args);
  1467. if (r != null) {
  1468. result = r;
  1469. }
  1470. });
  1471. return result;
  1472. },
  1473. /**
  1474. * 生成和替换劫持方法
  1475. * @param parent
  1476. * @param context
  1477. * @param methodName {string}
  1478. * @private
  1479. */
  1480. _hook: function (parent, methodName, context) {
  1481. if (context === undefined) {
  1482. context = parent;
  1483. }
  1484. var method = parent[methodName];
  1485. var methodTask = this._getHookedMethodTask(parent, methodName);
  1486. if (!methodTask.original) {
  1487. methodTask.original = method;
  1488. }
  1489. if (utils.isExistObject(methodTask.replace) && utils.isFunction(methodTask.replace.method)) {
  1490. parent[methodName] = methodTask.replace.method(methodTask.original);
  1491. return;
  1492. }
  1493. var invokeMethods = this._invokeMethods;
  1494. // 组装劫持函数
  1495. var builder = new utils.FunctionBuilder(function (v) {
  1496. return {
  1497. result: undefined
  1498. };
  1499. });
  1500. if (methodTask.task.before.length > 0) {
  1501. builder.push(function (v) {
  1502. invokeMethods(context || v.this, methodTask.task.before, [methodTask.original, v.arguments]);
  1503. });
  1504. }
  1505. if (utils.isExistObject(methodTask.task.current) && utils.isFunction(methodTask.task.current.method)) {
  1506. builder.push(function (v) {
  1507. return {
  1508. result: methodTask.task.current.method.call(context || v.this, parent, methodTask.original, v.arguments)
  1509. }
  1510. });
  1511. } else {
  1512. builder.push(function (v) {
  1513. return {
  1514. result: methodTask.original.apply(context || v.this, v.arguments)
  1515. }
  1516. });
  1517. }
  1518. if (methodTask.task.after.length > 0) {
  1519. builder.push(function (v) {
  1520. var args = [];
  1521. args.push(methodTask.original);
  1522. args.push(v.arguments);
  1523. args.push(v.result);
  1524. var r = invokeMethods(context || v.this, methodTask.task.after, args);
  1525. return {
  1526. result: (r != null ? r : v.result)
  1527. };
  1528. });
  1529. }
  1530. builder.push(function (v) {
  1531. return {
  1532. returnValue: v.result
  1533. };
  1534. });
  1535. // var methodStr = '(function(){\n';
  1536. // methodStr = methodStr + 'var result = undefined;\n';
  1537. // if (methodTask.task.before.length > 0) {
  1538. // methodStr = methodStr + 'invokeMethods(context, methodTask.task.before,[methodTask.original, arguments]);\n';
  1539. // }
  1540. // if (utils.isExistObject(methodTask.task.current) && utils.isFunction(methodTask.task.current.method)) {
  1541. // methodStr = methodStr + 'result = methodTask.task.current.method.call(context, parent, methodTask.original, arguments);\n';
  1542. // } else {
  1543. // methodStr = methodStr + 'result = methodTask.original.apply(context, arguments);\n';
  1544. // }
  1545. // if (methodTask.task.after.length > 0) {
  1546. // methodStr = methodStr + 'var args = [];args.push(methodTask.original);args.push(arguments);args.push(result);\n';
  1547. // methodStr = methodStr + 'var r = invokeMethods(context, methodTask.task.after, args);result = (r!=null?r:result);\n';
  1548. // }
  1549. // methodStr = methodStr + 'return result;\n})';
  1550. // 绑定劫持函数
  1551. var resultFunc = builder.result();
  1552. for (var proxyName in methodTask.original) {
  1553. Object.defineProperty(resultFunc, proxyName, {
  1554. get: function () {
  1555. return methodTask.original[proxyName];
  1556. },
  1557. set: function (v) {
  1558. methodTask.original[proxyName] = v;
  1559. }
  1560. })
  1561. }
  1562. resultFunc.prototype = methodTask.original.prototype;
  1563. parent[methodName] = resultFunc;
  1564. },
  1565. /**
  1566. * 劫持一个方法
  1567. * @param parent
  1568. * @param methodName {string}
  1569. * @param config
  1570. */
  1571. hook: function (parent, methodName, config) {
  1572. var hookedFailure = -1;
  1573. // 调用方法的上下文
  1574. var context = config.context !== undefined ? config.context : parent;
  1575. if (parent[methodName] == null) {
  1576. parent[methodName] = function () {
  1577. }
  1578. }
  1579. if (!utils.isFunction(parent[methodName])) {
  1580. return hookedFailure;
  1581. }
  1582. var methodTask = this._getHookedMethodTask(parent, methodName);
  1583. var id = this._getAutoId();
  1584. if (utils.isFunction(config.replace)) {
  1585. methodTask.replace = {
  1586. id: id,
  1587. method: config.replace
  1588. };
  1589. hookedFailure = 0;
  1590. }
  1591. if (utils.isFunction(config.before)) {
  1592. methodTask.task.before.push({
  1593. id: id,
  1594. method: config.before
  1595. });
  1596. hookedFailure = 0;
  1597. }
  1598. if (utils.isFunction(config.current)) {
  1599. methodTask.task.current = {
  1600. id: id,
  1601. method: config.current
  1602. };
  1603. hookedFailure = 0;
  1604. }
  1605. if (utils.isFunction(config.after)) {
  1606. methodTask.task.after.push({
  1607. id: id,
  1608. method: config.after
  1609. });
  1610. hookedFailure = 0;
  1611. }
  1612. if (hookedFailure === 0) {
  1613. this._hook(parent, methodName, context);
  1614. return id;
  1615. } else {
  1616. return hookedFailure;
  1617. }
  1618.  
  1619. },
  1620. /**
  1621. * 劫持替换一个方法
  1622. * @param parent
  1623. * @param context
  1624. * @param methodName {string}
  1625. * @param replace {function}
  1626. * @return {number} 该次劫持的id
  1627. */
  1628. hookReplace: function (parent, methodName, replace, context) {
  1629. return this.hook(parent, methodName, {
  1630. replace: replace,
  1631. context: context
  1632. });
  1633. },
  1634. hookBefore: function (parent, methodName, before, context) {
  1635. return this.hook(parent, methodName, {
  1636. before: before,
  1637. context: context
  1638. });
  1639. },
  1640. hookCurrent: function (parent, methodName, current, context) {
  1641. return this.hook(parent, methodName, {
  1642. current: current,
  1643. context: context
  1644. });
  1645. },
  1646. hookAfter: function (parent, methodName, after, context) {
  1647. return this.hook(parent, methodName, {
  1648. after: after,
  1649. context: context
  1650. });
  1651. },
  1652. hookClass: function (parent, className, replace, innerName, excludeProperties) {
  1653. var _this = this;
  1654. var originFunc = parent[className];
  1655. if (!excludeProperties) {
  1656. excludeProperties = [];
  1657. }
  1658. excludeProperties.push('prototype');
  1659. excludeProperties.push('caller');
  1660. excludeProperties.push('arguments');
  1661. innerName = innerName || '_innerHook';
  1662. var resFunc = function () {
  1663. this[innerName] = new originFunc();
  1664. replace.apply(this, arguments);
  1665. };
  1666. this.hookedToString(originFunc, resFunc);
  1667. this.hookedToProperties(originFunc, resFunc, true, excludeProperties);
  1668. var prototypeProperties = Object.getOwnPropertyNames(originFunc.prototype);
  1669. var prototype = resFunc.prototype = {
  1670. constructor: resFunc
  1671. };
  1672. prototypeProperties.forEach(function (name) {
  1673. if (name === 'constructor') {
  1674. return;
  1675. }
  1676. var method = function () {
  1677. if (originFunc.prototype[name] && utils.isFunction(originFunc.prototype[name])) {
  1678. return originFunc.prototype[name].apply(this[innerName], arguments);
  1679. }
  1680. return undefined;
  1681. };
  1682. _this.hookedToString(originFunc.prototype[name], method);
  1683. prototype[name] = method;
  1684. });
  1685. this.hookReplace(parent, className, function () {
  1686. return resFunc;
  1687. }, parent)
  1688. },
  1689. hookedToProperties: function (originObject, resultObject, isDefined, excludeProperties) {
  1690. var objectProperties = Object.getOwnPropertyNames(originObject);
  1691. objectProperties.forEach(function (property) {
  1692. if (utils.contains(excludeProperties, property)) {
  1693. return;
  1694. }
  1695. if (!isDefined) {
  1696. resultObject[property] = originObject[property];
  1697. } else {
  1698. Object.defineProperty(resultObject, property, {
  1699. configurable: false,
  1700. enumerable: false,
  1701. value: originObject[property],
  1702. writable: false
  1703. });
  1704. }
  1705. });
  1706. },
  1707. hookedToString: function (originObject, resultObject) {
  1708. var toString = function () {
  1709. return originObject.toString();
  1710. };
  1711. var toLocaleString = function () {
  1712. return originObject.toLocaleString();
  1713. };
  1714. Object.defineProperties(resultObject, {
  1715. toString: {
  1716. configurable: false,
  1717. enumerable: false,
  1718. value: toString,
  1719. writable: false
  1720. },
  1721. toLocaleString: {
  1722. configurable: false,
  1723. enumerable: false,
  1724. value: toLocaleString,
  1725. writable: false
  1726. }
  1727. })
  1728. },
  1729. /**
  1730. * 劫持全局ajax
  1731. * @param methods {object} 劫持的方法
  1732. * @return {*|number} 劫持的id
  1733. */
  1734. hookAjax: function (methods) {
  1735. if (this.isHooked(_global, 'XMLHttpRequest')) {
  1736. return;
  1737. }
  1738. var _this = this;
  1739. var hookMethod = function (methodName) {
  1740. if (utils.isFunction(methods[methodName])) {
  1741. // 在执行方法之前hook原方法
  1742. _this.hookBefore(this.xhr, methodName, methods[methodName]);
  1743. }
  1744. // 返回方法调用内部的xhr
  1745. return this.xhr[methodName].bind(this.xhr);
  1746. };
  1747. var getProperty = function (attr) {
  1748. return function () {
  1749. return this.hasOwnProperty(attr + "_") ? this[attr + "_"] : this.xhr[attr];
  1750. };
  1751. };
  1752. var setProperty = function (attr) {
  1753. return function (f) {
  1754. var xhr = this.xhr;
  1755. var that = this;
  1756. if (attr.indexOf("on") !== 0) {
  1757. this[attr + "_"] = f;
  1758. return;
  1759. }
  1760. if (methods[attr]) {
  1761. xhr[attr] = function () {
  1762. f.apply(xhr, arguments);
  1763. };
  1764. // on方法在set时劫持
  1765. _this.hookBefore(xhr, attr, methods[attr]);
  1766. // console.log(1,attr);
  1767. // xhr[attr] = function () {
  1768. // methods[attr](that) || f.apply(xhr, arguments);
  1769. // }
  1770. } else {
  1771. xhr[attr] = f;
  1772. }
  1773. };
  1774. };
  1775. return this.hookReplace(_global, 'XMLHttpRequest', function (XMLHttpRequest) {
  1776. var resFunc = function () {
  1777. this.xhr = new XMLHttpRequest();
  1778. for (var propertyName in this.xhr) {
  1779. var property = this.xhr[propertyName];
  1780. if (utils.isFunction(property)) {
  1781. // hook 原方法
  1782. this[propertyName] = hookMethod.bind(this)(propertyName);
  1783. } else {
  1784. Object.defineProperty(this, propertyName, {
  1785. get: getProperty(propertyName),
  1786. set: setProperty(propertyName)
  1787. });
  1788. }
  1789. }
  1790. // 定义外部xhr可以在内部访问
  1791. this.xhr.xhr = this;
  1792. };
  1793. _this.hookedToProperties(XMLHttpRequest, resFunc, true);
  1794. _this.hookedToString(XMLHttpRequest, resFunc);
  1795. return resFunc
  1796. });
  1797. },
  1798. /**
  1799. * 劫持全局ajax
  1800. * @param methods {object} 劫持的方法
  1801. * @return {*|number} 劫持的id
  1802. */
  1803. hookAjaxV2: function (methods) {
  1804. this.hookClass(window, 'XMLHttpRequest', function () {
  1805.  
  1806. });
  1807. utils.ergodicObject(this, methods, function (method) {
  1808.  
  1809. });
  1810. },
  1811. /**
  1812. * 解除劫持
  1813. * @param context 上下文
  1814. * @param methodName 方法名
  1815. * @param isDeeply {boolean=} 是否深度解除[默认为false]
  1816. * @param eqId {number=} 解除指定id的劫持[可选]
  1817. */
  1818. unHook: function (context, methodName, isDeeply, eqId) {
  1819. if (!context[methodName] || !utils.isFunction(context[methodName])) {
  1820. return;
  1821. }
  1822. var methodTask = this._getHookedMethodTask(context, methodName);
  1823. if (eqId) {
  1824. if (this.unHookById(eqId)) {
  1825. return;
  1826. }
  1827. }
  1828. if (!methodTask.original) {
  1829. delete this._getHookedMethodMap(context)[methodName];
  1830. return;
  1831. }
  1832. context[methodName] = methodTask.original;
  1833. if (isDeeply) {
  1834. delete this._getHookedMethodMap(context)[methodName];
  1835. }
  1836. },
  1837. /**
  1838. * 通过Id解除劫持
  1839. * @param eqId
  1840. * @returns {boolean}
  1841. */
  1842. unHookById: function (eqId) {
  1843. var hasEq = false;
  1844. if (eqId) {
  1845. var hookedMap = this._getHookedMap();
  1846. utils.ergodicObject(this, hookedMap, function (contextMap) {
  1847. utils.ergodicObject(this, contextMap, function (methodTask) {
  1848. methodTask.task.before = methodTask.task.before.filter(function (before) {
  1849. hasEq = hasEq || before.id === eqId;
  1850. return before.id !== eqId;
  1851. });
  1852. methodTask.task.after = methodTask.task.after.filter(function (after) {
  1853. hasEq = hasEq || after.id === eqId;
  1854. return after.id !== eqId;
  1855. });
  1856. if (methodTask.task.current && methodTask.task.current.id === eqId) {
  1857. methodTask.task.current = undefined;
  1858. hasEq = true;
  1859. }
  1860. if (methodTask.replace && methodTask.replace.id === eqId) {
  1861. methodTask.replace = undefined;
  1862. hasEq = true;
  1863. }
  1864. })
  1865. });
  1866. }
  1867. return hasEq;
  1868. },
  1869. /**
  1870. * 移除所有劫持相关的方法
  1871. * @param context 上下文
  1872. * @param methodName 方法名
  1873. */
  1874. removeHookMethod: function (context, methodName) {
  1875. if (!context[methodName] || !utils.isFunction(context[methodName])) {
  1876. return;
  1877. }
  1878. this._getHookedMethodMap(context)[methodName] = {
  1879. original: undefined,
  1880. replace: undefined,
  1881. task: {
  1882. before: [],
  1883. current: undefined,
  1884. after: []
  1885. }
  1886. };
  1887. },
  1888. /**
  1889. * 判断一个方法是否被劫持过
  1890. * @param context
  1891. * @param methodName
  1892. */
  1893. isHooked: function (context, methodName) {
  1894. var hookMap = this._getHookedMethodMap(context);
  1895. return hookMap[methodName] !== undefined ? (hookMap[methodName].original !== undefined) : false;
  1896. },
  1897. /**
  1898. * 保护一个对象使之不会被篡改
  1899. * @param parent
  1900. * @param methodName
  1901. */
  1902. protect: function (parent, methodName) {
  1903. Object.defineProperty(parent, methodName, {
  1904. configurable: false,
  1905. writable: false
  1906. });
  1907. },
  1908. /**
  1909. * 装载插件
  1910. * @param option
  1911. */
  1912. plugins: function (option) {
  1913. if (utils.isFunction(option.mount)) {
  1914. var result = option.mount.call(this, utils);
  1915. if (typeof option.name === 'string') {
  1916. _global[option.name] = result;
  1917. }
  1918. }
  1919. }
  1920. };
  1921. if (_global.eHook && (_global.eHook instanceof EHook)) {
  1922. return;
  1923. }
  1924. var eHook = new EHook();
  1925. var AHook = function () {
  1926. this.isHooked = false;
  1927. var autoId = 1;
  1928. this._urlDispatcherList = [];
  1929. this._getAutoId = function () {
  1930. return autoId++;
  1931. };
  1932. };
  1933. AHook.prototype = {
  1934. /**
  1935. * 执行配置列表中的指定方法组
  1936. * @param xhr
  1937. * @param methodName
  1938. * @param args
  1939. * @private
  1940. */
  1941. _invokeAimMethods: function (xhr, methodName, args) {
  1942. var configs = utils.parseArrayByProperty(xhr.patcherList, 'config');
  1943. var methods = [];
  1944. utils.ergodicArrayObject(xhr, configs, function (config) {
  1945. if (utils.isFunction(config[methodName])) {
  1946. methods.push(config[methodName]);
  1947. }
  1948. });
  1949. return utils.invokeMethods(xhr, methods, args);
  1950. },
  1951. /**
  1952. * 根据url获取配置列表
  1953. * @param url
  1954. * @return {Array}
  1955. * @private
  1956. */
  1957. _urlPatcher: function (url) {
  1958. var patcherList = [];
  1959. utils.ergodicArrayObject(this, this._urlDispatcherList, function (patcherMap, i) {
  1960. if (utils.UrlUtils.urlMatching(url, patcherMap.patcher)) {
  1961. patcherList.push(patcherMap);
  1962. }
  1963. });
  1964. return patcherList;
  1965. },
  1966. /**
  1967. * 根据xhr对象分发回调请求
  1968. * @param xhr
  1969. * @param fullUrl
  1970. * @private
  1971. */
  1972. _xhrDispatcher: function (xhr, fullUrl) {
  1973. var url = utils.UrlUtils.getUrlWithoutParam(fullUrl);
  1974. xhr.patcherList = this._urlPatcher(url);
  1975. },
  1976. /**
  1977. * 转换响应事件
  1978. * @param e
  1979. * @param xhr
  1980. * @private
  1981. */
  1982. _parseEvent: function (e, xhr) {
  1983. try {
  1984. Object.defineProperties(e, {
  1985. target: {
  1986. get: function () {
  1987. return xhr;
  1988. }
  1989. },
  1990. srcElement: {
  1991. get: function () {
  1992. return xhr;
  1993. }
  1994. }
  1995. });
  1996. } catch (error) {
  1997. console.warn('重定义返回事件失败,劫持响应可能失败');
  1998. }
  1999. return e;
  2000. },
  2001. /**
  2002. * 解析open方法的参数
  2003. * @param args
  2004. * @private
  2005. */
  2006. _parseOpenArgs: function (args) {
  2007. return {
  2008. method: args[0],
  2009. fullUrl: args[1],
  2010. url: utils.UrlUtils.getUrlWithoutParam(args[1]),
  2011. params: utils.UrlUtils.getParamFromUrl(args[1]),
  2012. async: args[2]
  2013. };
  2014. },
  2015. /**
  2016. * 劫持ajax 请求参数
  2017. * @param argsObject
  2018. * @param argsArray
  2019. * @private
  2020. */
  2021. _rebuildOpenArgs: function (argsObject, argsArray) {
  2022. argsArray[0] = argsObject.method;
  2023. argsArray[1] = utils.UrlUtils.margeUrlAndParams(argsObject.url, argsObject.params);
  2024. argsArray[2] = argsObject.async;
  2025. },
  2026. /**
  2027. * 获取劫持方法的参数 [原方法,原方法参数,原方法返回值],剔除原方法参数
  2028. * @param args
  2029. * @return {*|Array.<T>}
  2030. * @private
  2031. */
  2032. _getHookedArgs: function (args) {
  2033. // 将参数中'原方法'剔除
  2034. return Array.prototype.slice.call(args, 0).splice(1);
  2035. },
  2036. /**
  2037. * 响应被触发时调用的方法
  2038. * @param outerXhr
  2039. * @param funcArgs
  2040. * @private
  2041. */
  2042. _onResponse: function (outerXhr, funcArgs) {
  2043. // 因为参数是被劫持的参数为[method(原方法),args(参数)],该方法直接获取参数并转换为数组
  2044. var args = this._getHookedArgs(funcArgs);
  2045. args[0][0] = this._parseEvent(args[0][0], outerXhr.xhr); // 强制事件指向外部xhr
  2046. // 执行所有的名为hookResponse的方法组
  2047. var results = this._invokeAimMethods(outerXhr, 'hookResponse', args);
  2048. // 遍历结果数组并获取最后返回的有效的值作为响应值
  2049. var resultIndex = -1;
  2050. utils.ergodicArrayObject(outerXhr, results, function (res, i) {
  2051. if (res != null) {
  2052. resultIndex = i;
  2053. }
  2054. });
  2055. if (resultIndex !== -1) {
  2056. outerXhr.xhr.responseText_ = outerXhr.xhr.response_ = results[resultIndex];
  2057. }
  2058. },
  2059. /**
  2060. * 手动开始劫持
  2061. */
  2062. startHook: function () {
  2063. var _this = this;
  2064. var normalMethods = {
  2065. // 方法中的this指向内部xhr
  2066. // 拦截响应
  2067. onreadystatechange: function () {
  2068. if (this.readyState == 4 && this.status == 200 || this.status == 304) {
  2069. _this._onResponse(this, arguments);
  2070. }
  2071. },
  2072. onload: function () {
  2073. _this._onResponse(this, arguments);
  2074. },
  2075. // 拦截请求
  2076. open: function () {
  2077. var args = _this._getHookedArgs(arguments);
  2078. var fullUrl = args[0][1];
  2079. _this._xhrDispatcher(this, fullUrl);
  2080. var argsObject = _this._parseOpenArgs(args[0]);
  2081. this.openArgs = argsObject;
  2082. _this._invokeAimMethods(this, 'hookRequest', [argsObject]);
  2083. _this._rebuildOpenArgs(argsObject, args[0]);
  2084. },
  2085. send: function () {
  2086. var args = _this._getHookedArgs(arguments);
  2087. this.sendArgs = args;
  2088. _this._invokeAimMethods(this, 'hookSend', args);
  2089. }
  2090. };
  2091. // 设置总的hookId
  2092. this.___hookedId = _global.eHook.hookAjax(normalMethods);
  2093. this.isHooked = true;
  2094. },
  2095. /**
  2096. * 注册ajaxUrl拦截
  2097. * @param urlPatcher
  2098. * @param configOrRequest
  2099. * @param response
  2100. * @return {number}
  2101. */
  2102. register: function (urlPatcher, configOrRequest, response) {
  2103. if (!urlPatcher) {
  2104. return -1;
  2105. }
  2106. if (!utils.isExistObject(configOrRequest) && !utils.isFunction(response)) {
  2107. return -1;
  2108. }
  2109. var config = {};
  2110. if (utils.isFunction(configOrRequest)) {
  2111. config.hookRequest = configOrRequest;
  2112. }
  2113. if (utils.isFunction(response)) {
  2114. config.hookResponse = response;
  2115. }
  2116. if (utils.isExistObject(configOrRequest)) {
  2117. config = configOrRequest;
  2118. }
  2119. var id = this._getAutoId();
  2120. this._urlDispatcherList.push({
  2121. // 指定id便于后续取消
  2122. id: id,
  2123. patcher: urlPatcher,
  2124. config: config
  2125. });
  2126. // 当注册一个register时,自动开始运行劫持
  2127. if (!this.isHooked) {
  2128. this.startHook();
  2129. }
  2130. return id;
  2131. }
  2132. // todo 注销 cancellation: function (registerId){};
  2133. };
  2134.  
  2135. _global['eHook'] = eHook;
  2136. _global['aHook'] = new AHook();
  2137.  
  2138. }.bind(window)(
  2139. (function () {
  2140. var utils = {};
  2141. if (window.everyUtils) {
  2142. window.everyUtils(function (
  2143. ArrayUtils,
  2144. ObjectUtils,
  2145. BaseUtils,
  2146. FunctionUtils,
  2147. ColorUtils,
  2148. UrlUtils) {
  2149. utils = {
  2150. ArrayUtils: ArrayUtils,
  2151. ObjectUtils: ObjectUtils,
  2152. BaseUtils: BaseUtils,
  2153. ColorUtils: ColorUtils,
  2154. UrlUtils: UrlUtils,
  2155. urlUtils: UrlUtils,
  2156. FunctionUtils: FunctionUtils
  2157. };
  2158. });
  2159. }
  2160. var proxy = {};
  2161. Object.keys(utils).forEach(function (utilName) {
  2162. if (!utilName) {
  2163. return;
  2164. }
  2165. Object.defineProperty(proxy, utilName, {
  2166. get: function () {
  2167. return utils[utilName];
  2168. }
  2169. });
  2170. Object.keys(utils[utilName]).forEach(function (key) {
  2171. if (!key) {
  2172. return;
  2173. }
  2174. if (proxy[key]) {
  2175. return;
  2176. }
  2177. Object.defineProperty(proxy, key, {
  2178. get: function () {
  2179. return utils[utilName][key];
  2180. }
  2181. })
  2182. })
  2183. });
  2184. return proxy;
  2185. })()
  2186. );