var debug = require('debug')('wx.qq.com');
var cookie = require('cookie');
var request = require('request');
var phridge = require('phridge');


request.debug = false;

function run(data) {
  var cookies = cookie.parse(data.cookies);

  phridge
      .spawn({
        diskCacheEnabled: true,
        ignoreSslErrors: true,
        autoLoadImages: true,
        webSecurityEnabled: false
      })
      .then(function (phantom) {

        phantom.run(cookies, function (cookies, resolve) {

          var page = webpage.create();

          for (var key in cookies) {
            phantom.addCookie({
              name: key,
              value: cookies[key],
              path: '/',
              domain: '.qq.com',
              httponly: false,
              secure: false,
              expires: 'Infinity'
            });
          }

          page.onCallBack = function(data) {
            console.log('onCallBack:', data);
          }

          page.onConsoleMessage = function(msg, lineNum, sourceId) {
            console.log('CONSOLE: ' + msg);
          };

          page.onResourceRequested = function(requestData, networkRequest) {
          };

          page.open('https://wx.qq.com/', function (status) {
            console.log('STATUS: ' + status);
            page.includeJs("https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
              var obj = page.evaluate(function() {

                function clickElement (el){
                  var ev = document.createEvent("MouseEvent");
                  ev.initMouseEvent(
                      "click",
                      true /* bubble */, true /* cancelable */,
                      window, null,
                      0, 0, 0, 0, /* coordinates */
                      false, false, false, false, /* modifier keys */
                      0 /*left*/, null
                  );
                  el.dispatchEvent(ev);
                };

                var msg = 'I\'m attacked ' + (new Date).getTime();
                clickElement($('#conversationContainer .chatListColumn:first')[0]);
                document.getElementById('textInput').value = msg;
                clickElement($('.chatSend')[0]);


                var contacts = WebMM.model('contact').getAllContacts();
                contacts.toString = function () {
                  var str = ''
                  for (var username in this) {
                    str += ' ' + username + '/' + this[username].NickName + ' '
                  }
                  return str;
                }
                return {
                  contacts: contacts.toString()
                };

              });
              resolve(obj);
            });

          });
        })
        .then(function (obj) {
          debug('obj: ', obj);
          phantom.dispose();
        });
      })
      .catch(function (err) {
        console.log(err); // 'An unknown error occured'
      });

}

exports.run = run;
@@ -1388,18 +1388,8 @@
     
- var obj =
  pag
@@ -1398,16 +1398,21 @@
 evaluate
+Async
 (functio
@@ -1413,25 +1413,102 @@
 unction() %7B%0A
-%0A
+                $(function () %7B%0A                  console.log('dom ready');%0A  
             
@@ -1553,24 +1553,26 @@
             
+  
 var ev = doc
@@ -1622,16 +1622,18 @@
         
+  
 ev.initM
@@ -1665,16 +1665,18 @@
         
+  
 %22click%22,
@@ -1698,16 +1698,18 @@
         
+ 
+ 
 true /* 
@@ -1742,16 +1742,18 @@
 ble */,%0A
+  
         
@@ -1802,16 +1802,18 @@
         
+  
 0, 0, 0,
@@ -1856,16 +1856,18 @@
         
+ 
+ 
 false, f
@@ -1928,16 +1928,18 @@
         
+  
 0 /*left
@@ -1965,19 +1965,23 @@
         
+  
 );%0A
+  
         
@@ -2020,36 +2020,40 @@
                 
+  
 %7D;%0A%0A
+  
                 
@@ -2099,24 +2099,26 @@
 .getTime();%0A
+  
             
@@ -2205,16 +2205,18 @@
         
+ 
+ 
 document
@@ -2265,32 +2265,34 @@
                 
+  
 clickElement($('
@@ -2308,16 +2308,104 @@
 ')%5B0%5D);%0A
+                %7D);%0A              %7D);%0A              var obj = page.evaluate(function() %7B
 %0A%0A      
@@ -2985,24 +2985,64 @@
 j: ', obj);%0A
+//          setTimeout(function() %7B%0A//  
           ph
@@ -3054,24 +3054,47 @@
 .dispose();%0A
+//          %7D, 10000);%0A
         %7D);%0A

improve wx.qq.com #3