提交 0a2a2b96 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

half work... fighting

上级 632b1f42
......@@ -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()')
......
/**
*
* 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')
})
})
}
......
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册