这个是 mac 版 weixin 中的一个叫 wxhook.js
的文件,本人 js 不大行,看的一知半解,貌似对地理位置做监控了,但是这个监控到底达到了什么效果不大清楚。
// 这个脚本会注入到主 frame 和 iframe 中 try { if (typeof window.weixinPostMessageHandlers === 'undefined') { window.weixinPostMessageHandlers = window.webkit.messageHandlers; Object.defineProperty(window, 'weixinPostMessageHandlers', { value: window.weixinPostMessageHandlers,writable:false }); } // hook navigator.geolocation.getCurrentPosition, 增加监控 var oriGetCurrLocation = navigator.geolocation.getCurrentPosition; Object.defineProperty(navigator.geolocation, 'getCurrentPosition', { value: function(successCallback,errorCallback,opt) { var option = (typeof opt !== 'undefined') ? opt : {}; // alert(JSON.stringify(option)); oriGetCurrLocation.call(this, function(position) { if (typeof successCallback !== 'undefined') { // alert(position.coords.longitude); window.weixinPostMessageHandlers.monitorHandler.postMessage(JSON.stringify({event: 'getCurrentLocation', errCode: 0, option: option, funcType:1})); successCallback(position); }; }, function(err) { // alert(err.code + ' ' + err.message); window.weixinPostMessageHandlers.monitorHandler.postMessage(JSON.stringify({event: 'getCurrentLocation', errCode: err.code, option: option, funcType: 1})); if (typeof errorCallback !== 'undefined') { errorCallback(err); }; }, option ); }, writable:true, //用户反馈 angular build 会修改这个属性,如果 false 会 js 运行错误 configurable: false }); // hook navigator.geolocation.watchPosition ,增加监控 var oriWatchPosition = navigator.geolocation.watchPosition; Object.defineProperty(navigator.geolocation, 'watchPosition', { value: function(successCallback,errorCallback,opt) { var option = (typeof opt !== 'undefined') ? opt : {}; var bHaveReport = false; // alert(JSON.stringify(option)); oriWatchPosition.call(this, function(position) { if (typeof successCallback !== 'undefined') { // alert(position.coords.longitude); if (!bHaveReport) { bHaveReport = true; window.weixinPostMessageHandlers.monitorHandler.postMessage(JSON.stringify({event: 'getCurrentLocation', errCode: 0, option: option, funcType: 2})); }; successCallback(position); }; }, function(err) { // alert(err.code + ' ' + err.message); window.weixinPostMessageHandlers.monitorHandler.postMessage(JSON.stringify({event: 'getCurrentLocation', errCode: err.code, option: option, funcType: 2})); if (typeof errorCallback !== 'undefined') { errorCallback(err); }; }, option ); }, writable:true, configurable: false }); } catch (e) {}
![]() | 1 iold 2021-11-29 16:31:08 +08:00 你猜的没错,Geolocation.watchPosition() 用于注册监听器,在设备的地理位置发生改变的时候自动被调用。 下面是文档连接: https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation/watchPosition |
![]() | 2 flyhaozi 2021-11-29 16:32:44 +08:00 就只是给 getCurrentPosition 和 watchPosition 加了调用成功失败的监控日志吧,要监控具体位置也是在 successCallback 里做,这里也看不到,地理位置权限本来就是给了它想干嘛就干嘛,没必要别给权限就是了 |
3 anonydmer OP |