diff --git a/README.md b/README.md index edc635c59dec7f1d98ef4fa032d34a4cf57a7de2..585e92e7615afd87033f190f8297ad995d10f604 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,10 @@ bot.on('message', m => { Notice that you need to wait a moment while bot trying to get the login QRCode from Wechat. As soon as the bot got login QRCode url, he will print url out. You need to scan the qrcode in wechat, and confirm login. -After that, bot will on duty. (roger-bot source can be found at [here](https://github.com/zixia/wechaty/blob/master/examples/roger-bot.js)) +After that, bot will on duty. (roger-bot source can be found at [here](https://github.com/zixia/wechaty/blob/master/example/roger-bot.js)) ## 2. Advanced: 100 lines -There's another basic usage demo bot named (ding-dong-bot)(https://github.com/zixia/wechaty/blob/master/examples/ding-dong-bot.js), who can reply `dong` when receive a message `ding`. +There's another basic usage demo bot named [ding-dong-bot](https://github.com/zixia/wechaty/blob/master/example/ding-dong-bot.js), who can reply `dong` when receive a message `ding`. ## 3. Hardcore: 1000 lines To Be Wroten. @@ -243,7 +243,7 @@ Github Issue - https://github.com/zixia/wechaty/issues ```bash $ npm lint ``` -* Create a issue, then send me a pull request(with unit test please). +* Create a issue, fork, then send a pull request(with unit test please). Author ----------------- diff --git a/examples/ding-dong-bot.js b/example/ding-dong-bot.js similarity index 100% rename from examples/ding-dong-bot.js rename to example/ding-dong-bot.js diff --git a/examples/roger-bot.js b/example/roger-bot.js similarity index 100% rename from examples/roger-bot.js rename to example/roger-bot.js diff --git a/package.json b/package.json index bf35b942ec3a88c7279459f4287dbb6a034b369b..3bb220823ea2a0a06a61322141fa75d393d85e75 100755 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "Wechat for Bot", "main": "index.js", "scripts": { - "start": "node examples/ding-dong-bot.js", - "test": "tape tests/*.js", + "start": "node example/ding-dong-bot.js", + "test": "tape test/*.js", "lint": "eslint src" }, "repository": { diff --git a/src/group.js b/src/group.js index 233e95bb52cbb9a85a667209780c50b10f25504a..0529f74b002e1f64cba192d8c64e3082bcde527c 100644 --- a/src/group.js +++ b/src/group.js @@ -14,7 +14,9 @@ class Group { this.id = id this.obj = {} log.silly('Group', `constructor(${id})`) - if (!Group.puppet) throw new Error('no puppet attached to Group'); + if (!Group.puppet) { + throw new Error('no puppet attached to Group') + } } toString() { return this.obj.name ? this.obj.name : this.id } @@ -32,9 +34,9 @@ class Group { this.rawObj = data this.obj = this.parse(data) return this - }).catch(e => { + }).catch(e => { log.error('Group', `contactGetter(${this.id}) rejected: ` + e) - throw new Error('contactGetter: ' + e) + throw new Error('contactGetter: ' + e) }) } @@ -52,35 +54,32 @@ class Group { } } - dumpRaw() { + dumpRaw() { console.error('======= dump raw group =======') - Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`)) + Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`)) } - dump() { + dump() { console.error('======= dump group =======') - Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) + Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) } getId() { return this.id } - get(prop) { return this.obj[prop] } - - static find() { } static findAll() { } } -Group.init = function () { Group.pool = {} } +Group.init = function() { Group.pool = {} } Group.init() -Group.load = function (id) { +Group.load = function(id) { if (id in Group.pool) { return Group.pool[id] } return Group.pool[id] = new Group(id) } -Group.attach = function (puppet) { Group.puppet = puppet } +Group.attach = function(puppet) { Group.puppet = puppet } module.exports = Group diff --git a/src/puppet-web-injectio.js b/src/puppet-web-injectio.js index ec46b5566f120d7d8bd5497d8ce0de89093b922e..d800724be4536b4a160c21ee8fae4dad72df9ab6 100644 --- a/src/puppet-web-injectio.js +++ b/src/puppet-web-injectio.js @@ -2,7 +2,7 @@ * * Wechaty - Wechat for Bot, and human who talk to bot. * - * Inject this js code to browser, + * Inject this js code to browser, * in order to interactive with wechat web program. * * Licenst: MIT @@ -19,9 +19,11 @@ 408: 未确认 */ -if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; +if (typeof Wechaty !== 'undefined') { + return 'Wechaty already injected?' +} -;return (function (port) { +;return (function(port) { port = port || 8788 /** @@ -36,21 +38,21 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; i.style.display = 'none' document.body.appendChild(i) i.contentWindow.console.log(s) - i.parentNode.removeChild(i) + i.parentNode.removeChild(i) } var Wechaty = { glue: {} // will be initialized by glueAngular() function // glue funcs - , getLoginStatusCode: function () { return Wechaty.glue.loginScope.code } - , getLoginQrImgUrl: function () { return Wechaty.glue.loginScope.qrcodeUrl } - , isLogined: function () { return 200===Wechaty.glue.loginScope.code /* MMCgi.isLogin ??? */} + , getLoginStatusCode: function() { return Wechaty.glue.loginScope.code } + , getLoginQrImgUrl: function() { return Wechaty.glue.loginScope.qrcodeUrl } + , isLogined: function() { return 200 === Wechaty.glue.loginScope.code /* MMCgi.isLogin ??? */} , isReady: isReady - // variable + // variable , socket: null - // funcs + // funcs , init: init , send: send , clog: clog // Console log @@ -61,8 +63,8 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; , getContact: getContact } - function isReady() { - return !!((typeof angular)!=='undefined' && angular.element && angular.element("body")) + function isReady() { + return !!((typeof angular) !== 'undefined' && angular.element && angular.element('body')) } function init() { if (!isReady()) { @@ -74,7 +76,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; clog('init on port:' + port) glueAngular() connectSocket() - hookUnload() + hookUnload() hookMessage() clog('inited!. ;-D') @@ -83,12 +85,12 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; function glueAngular() { var injector = angular.element(document).injector() - var rootScope = injector.get("$rootScope") - var http = injector.get("$http") - var chatFactory = injector.get("chatFactory") - var confFactory = injector.get("confFactory") + var rootScope = injector.get('$rootScope') + var http = injector.get('$http') + var chatFactory = injector.get('chatFactory') + var confFactory = injector.get('confFactory') var contactFactory = injector.get('contactFactory') - var loginScope = angular.element(".login_box").scope() + var loginScope = angular.element('.login_box').scope() // get all we need from wx in browser(angularjs) Wechaty.glue = { @@ -104,8 +106,8 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; function quit() { if (Wechaty.socket) { - Wechaty.socket.close() - Wechaty.socket = undefined + Wechaty.socket.close() + Wechaty.socket = undefined } clog('quit()') } @@ -125,19 +127,22 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; function getContact(id) { return Wechaty.glue.contactFactory.getContact(id) } function hookMessage() { var rootScope = Wechaty.glue.rootScope - rootScope.$on("message:add:success", function (event, data) { - if (Wechaty.socket) Wechaty.socket.emit('message', data); - else clog('Wechaty.socket not ready'); + rootScope.$on('message:add:success', function(event, data) { + if (Wechaty.socket) { + Wechaty.socket.emit('message', data) + } else { + clog('Wechaty.socket not ready') + } }) } function hookUnload() { - window.addEventListener ('unload', function (e) { - Wechaty.socket.emit('unload') + window.addEventListener('unload', function(e) { + Wechaty.socket.emit('unload') }) } function connectSocket() { clog('connectSocket()') - if (typeof io!=='function') { + if (typeof io !== 'function') { clog('connectSocket: io not found. loading lib...') // http://stackoverflow.com/a/3248500/1123955 var script = document.createElement('script') @@ -145,8 +150,8 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; clog('socket io lib loaded.') setTimeout(connectSocket, 50) } - script.src = "https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js" - document.getElementsByTagName('head')[0].appendChild(script); + script.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js' + document.getElementsByTagName('head')[0].appendChild(script) return // wait to be called via script.onload() } @@ -155,7 +160,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; // ding -> dong. for test & live check purpose // ping/pong are reserved by socket.io https://github.com/socketio/socket.io/issues/2414 - socket.on('ding', function (e) { + socket.on('ding', function(e) { clog('received socket io event: ding. emit dong...') socket.emit('dong', 'dong') }) @@ -174,8 +179,9 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?'; window.Wechaty = Wechaty var callback = arguments[arguments.length - 1] - if (typeof callback==='function') + if (typeof callback === 'function') { return callback('Wechaty') + } return 'Wechaty' diff --git a/tests/contact-tests.js b/test/contact-tests.js similarity index 100% rename from tests/contact-tests.js rename to test/contact-tests.js diff --git a/tests/group-tests.js b/test/group-tests.js similarity index 100% rename from tests/group-tests.js rename to test/group-tests.js diff --git a/tests/message-tests.js b/test/message-tests.js similarity index 100% rename from tests/message-tests.js rename to test/message-tests.js diff --git a/tests/puppet-web-browser-tests.js b/test/puppet-web-browser-tests.js similarity index 100% rename from tests/puppet-web-browser-tests.js rename to test/puppet-web-browser-tests.js diff --git a/tests/puppet-web-server-tests.js b/test/puppet-web-server-tests.js similarity index 100% rename from tests/puppet-web-server-tests.js rename to test/puppet-web-server-tests.js diff --git a/tests/puppet-web-tests.js b/test/puppet-web-tests.js similarity index 100% rename from tests/puppet-web-tests.js rename to test/puppet-web-tests.js diff --git a/tests/webdriver-tests.js b/test/webdriver-tests.js similarity index 100% rename from tests/webdriver-tests.js rename to test/webdriver-tests.js