diff --git a/lib/puppet-web-browser.js b/lib/puppet-web-browser.js index 9b31c02ab5bbee42644e6c758b4f7d768f665bd2..c1e81ccb96eb2f733d769f27c1a987a7355ad163 100755 --- a/lib/puppet-web-browser.js +++ b/lib/puppet-web-browser.js @@ -57,7 +57,7 @@ class Browser { }) .then(() => { console.error('injected injectio') - return this.execute('return Wechaty()') + return this.execute('return Wechaty.init()') }) .then(() => { console.error('injected Wechaty()') diff --git a/lib/puppet-web-injectio.js b/lib/puppet-web-injectio.js index 4931608c38ad5374b0b5f02f96d8fe3abdfe60a3..1faf63dbd229233011bcc34b0f7efa55ff308168 100644 --- a/lib/puppet-web-injectio.js +++ b/lib/puppet-web-injectio.js @@ -1,89 +1,68 @@ /** -* -* Wechaty - Robot API/SDK Library for Personal WeChat(微信) Account -* -* Inject this js code to browser, -* in order to interactive with wechat web program. -* -* Licenst: MIT -* https://github.com/zixia/wechaty-lib -* - - MMCgi.isLogin - loginScope.qrcodeUrl - - loginScope.code: - 0: 显示二维码 - 201: 扫描,未确认 - 200: 登录成功 - 408: 未确认 - -*/ + * + * Wechaty - Wechat for Bot, and human who talk to bot. + * + * Inject this js code to browser, + * in order to interactive with wechat web program. + * + * Licenst: MIT + * https://github.com/zixia/wechaty-lib + * + + MMCgi.isLogin + loginScope.qrcodeUrl + + loginScope.code: +0: 显示二维码 +201: 扫描,未确认 +200: 登录成功 +408: 未确认 + + */ ;(function (port) { port = port || 8788 + var injector = angular.element(document).injector() + var zlog = createZlog() var Wechaty = { - // get all we need from browser(angularjs) + // get all we need from wx in browser(angularjs) glue: { - injector: angular.element(document).injector() + injector: injector , rootScope: injector.get("$rootScope") , http: injector.get("$http") , chatFactory: injector.get("chatFactory") , confFactory: injector.get("confFactory") , loginScope: angular.element(".login_box").scope() } - - // variables - , var { - socket: socket - } - - // methods - , func { - , init: init - , send: send - , zlog: zlog - , log: log - - , log: function (msg) { socket && socket.emit('log', msg) } - , ping: function () { return 'pong' } - - , getLoginStatusCode: function () { return loginScope.code } - , getLoginQrImgUrl: function () { return loginScope.qrcodeUrl } - , isLogined: function () { return 200===loginScope.code } - , isReady: function () { return !!(angular && angular.element && angular.element("body")) } - - , initSocket: initSocket - - } - } - - function send (ToUserName, Content) { - var m = chatFactory.createMessage({ - ToUserName: ToUserName - , Content: Content - }) - chatFactory.appendMessage(m) - return chatFactory.sendMessage(m) + // glue funcs + , getLoginStatusCode: function () { return loginScope.code } + , getLoginQrImgUrl: function () { return loginScope.qrcodeUrl } + , isLogined: function () { return 200===loginScope.code } + , isReady: function () { return !!(angular && angular.element && angular.element("body")); } + + // variable + , socket: null + + // funcs + , init: init + , send: send + , zlog: zlog + , log: log + , ping: ping } function init() { - initZlog() zlog('wechaty port ' + port) - initSocket() // save to socket - + + initSocket() hookUnload() hookMessage() zlog('Wechaty injected!. ;-D') } - angular.extend(Wechaty, { - }) - - - function initZlog() { + function createZlog() { var enable = true if (enable) { if (!console.memory && console.time) { // wechat debuger exist @@ -95,47 +74,68 @@ } else { window.zlog = function () {} } + return window.zlog + } + + function log(msg) { Wechaty.socket && Wechaty.socket.emit('log', msg) } + function ping() { return 'pong' } + + function send (ToUserName, Content) { + var c = Wechaty.glue.chatFactory + var m = c.createMessage({ + ToUserName: ToUserName + , Content: Content + }) + c.appendMessage(m) + return c.sendMessage(m) } function hookMessage() { + var rootScope = Wechaty.glue.rootScope + var zlog = Wechaty.zlog rootScope.$on("message:add:success", function (event, data) { - socket.emit('message', data) - .catch(function (e) { zlog('socket.emit(message, data) fail:'); zlog(e) }) + Wechaty.socket.emit('message', data) + .catch(function (e) { + zlog('socket.emit(message, data) fail:') + zlog(e) + }) }) } function hookUnload() { window.addEventListener ('unload', function (e) { - socket.emit('unload') + Wechaty.socket.emit('unload') }) } function initSocket() { // Wechaty global variable: socket - Wechaty.socket = io.connect('https://127.0.0.1:' + port) + var socket = Wechaty.socket = io.connect('https://127.0.0.1:' + port) + var zlog = Wechaty.zlog - zlog('socket: ' + socket) - - Wechaty.socket.on('connect', function() { + socket.on('connect', function() { zlog('on connect entried') - rootScope.$on("message:add:success", function (event, data) { + Wechaty.glue.rootScope.$on("message:add:success", function (event, data) { socket.emit('message', data) }) socket.on('disconnect', function(e) { zlog('event: socket disconnect') - // socket.emit('disconnect', e) + + // for test & live check purpose: ping -> pong + socket.on('ping', function (e) { + Wechaty.zlog('received socket io event: ping. emit pong...') + socket.emit('pong', 'pong') + }) // Reconnect... - setTimeout(initSocket, 1000) + setTimeout(function () { + zlog('starting initSocket after disconnect') + initSocket() + }, 1000) }) - // for test & live check purpose: ping -> pong - socket.on('ping', function (e) { - zlog('received socket io event: ping. emit pong...') - socket.emit('pong', 'pong') - }) }) } diff --git a/tests/puppet-web-browser-tests.js b/tests/puppet-web-browser-tests.js index fb06d519afa84848ad0ef99fa0676e12b1a07c42..d205d93a8ade2aa1e1a5c99eec9b3289d9b870be 100644 --- a/tests/puppet-web-browser-tests.js +++ b/tests/puppet-web-browser-tests.js @@ -3,7 +3,8 @@ const Browser = require('../lib/puppet-web-browser') test('Browser class smoking tests', function (t) { //t.plan(5) - const b = new Browser() + const PORT = 58788 + const b = new Browser('firefox', PORT) t.ok(b, 'Browser instance created') b.open()