小黄鸡QQ空间自动回复机器人

60chrome浏览器内核与chrome一致,chrome现在增加了插件验证,不是在chrome插件商店下载的都不许安装,360chrome浏览器无此限制(网上有别的方法安装非商店下载插件),所以这里使用360chrome浏览器。

360chrome浏览器团队翻译了部分谷歌插件开发文档

地址:http://open.chrome.360.cn/extension_dev/overview.html

具体封装插件的方法请参考开发文档。


正文

    插件文件目录

        manifest.json

        jquery.min.js

        content_script.js

        background.js

        icon.png

    

具体代码

manifest.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
   "name" "小贱鸡QQ空间自动回复" ,
   "description" "小贱鸡QQ空间自动回复" ,
   "version" "1.1"
   "background" : {  "scripts" : [ "jquery.min.js" , "background.js" ] },
   "content_scripts" : [
     {
       "matches" : [ "http://*.qq.com/*" ],
       "js" : [ "jquery.min.js" , "content_script.js" ]
     }
   ],
   "permissions" : [
     "tabs" "http://*/*" "https://*/*"
   ],
   "icons"
  
     "16" "icon.png" ,             
     "48" "icon.png" ,            
     "128" "icon.png" 
   }, 
   "browser_action" : {
     "default_icon" "icon.png" 
   },
   "homepage_url" "http://www.cnblogs.com/mahatmasmile/" ,
   "manifest_version" : 2
}


content_script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
window.confirm =  function  () {}
var  GSid, GRefreshSid, refreshReplySid, qz =  new  QZone();
//消息传递监听 onRequest
chrome.extension.onRequest.addListener(
     function  (request, sender, sendResponse) {
     var  status = request.status;
     console.log(sender.tab ?
         "from a content script:"  + sender.tab.url :
         "from the extension" );
     console.log( "收到消息:"  + status);
     if  (status == 1) {
         //运行
         GSid = setInterval( function  () {
                 //刷新回复页面
                 qz.refreshReply( function  () {
                     //执行回复操作,默认只扫描前三个
                     var  i = 3;  //从零开始,只查找前四个聊天状态
                     //判断是否回复过了
                     while  (qz.isReplyed(i) && i >= 0) {
                         console.log( "判断是否回复过了::"  + i);
                         console.log(qz.isReplyed(i));
                         i--;  //如果回复过了就往前查找
                     }
                     if  (i >= 0) {
                         //获取回复内容
                         var  content = qz.getLastReply(i);
                         console.log( "获取到回复内容:"  + content);
                         sendToExtension(content,  function  (reply) {
                             //提交回复
                             console.log( "小贱鸡回复:"  + reply);
                             //qz.putReply(i, reply+"[老总不在,我是助手]");
                             qz.putReply(i, reply);
                         });
                     }
                 });
             }, 30000);
 
         // GRefreshSid = setInterval(function () {
         // //刷新回复页面
         // qz.refreshReply();
         // console.log("刷新回复页面");
         // }, 15000);
         alert( "自动回复启动" );
         console.log( "自动回复启动" );
     else  {
         //停止
         clearInterval(GSid);
         //clearInterval(GRefreshSid);
         clearInterval(refreshReplySid);
         alert( "自动回复停止" );
         console.log( "自动回复停止" );
     }
     sendResponse({
         farewell :  "contentScript 收到了消息"
     });
});
 
//对插件发送消息
function  sendToExtension(text, callbackFun) {
     console.log( "send request" );
     chrome.extension.sendRequest({
         text : text
     },  function  (response) {
         var  reply = response.reply;
         console.log(reply);
         if  (callbackFun)
             callbackFun(reply);
     });
}
 
//QQ空间操作对象 var qz = new QZone();
function  QZone() {
     //空间所有者
     this .qzOwner =  function  () {
         return  $( '.user-info' ).find( 'span.user-name.textoverflow' ).text();
     };
     //回复对象集合
     this .objArray =  function  () {
         return  $( '.mod-comments' ).find( '.comments-item-bd:eq(0)' );
     };
     //获得根节点对象
     this .getRoot =  function  (i) {
         return  this .objArray().eq(i);
     };
     //获得根节点回复
     this .getRootReply =  function  (i) {
         var  obj =  this .objArray().eq(i);
         var  html = obj.find( '.comments-content:eq(0)' ).html();
         var  regex = /&nbsp;\s:\s(.*)<div class= "comments-op" >/.exec(html);
         var  replyContent = regex.length > 0 ? regex[1] :  "" //回复内容
         return  $.trim(replyContent);
     };
     //获得根节点回复人
     this .getRootReplyer =  function  (i) {
         var  obj =  this .objArray().eq(i);
         var  nick = obj.find( '.c_tx' ).text();  //昵称
         return  nick;
     };
     //获得唯一的数据
     this .getDataParam =  function  (i) {
         var  obj =  this .objArray().eq(i);
         var  param = obj.find( '.act-reply' ).attr( 'data-param' );  //唯一数据参数
         return  param ? param :  "" ;
     };
     //获得根节点回复信息列表
     this .getCommentsList =  function  (i) {
         var  obj =  this .objArray().eq(i);
         return  obj.find( '.comments-list .comments-item-bd' );
     };
     //获得最新的回复内容
     this .getLastReply =  function  (i) {
         var  obj =  null ;
         if  ( this .hasChildNode(i))
             obj =  this .getCommentsList(i).last();
         else
             obj =  this .getRoot(i);
         //console.log(obj);
         var  html = obj.find( '.comments-content:eq(0)' ).html();
         //console.log(obj.find('.comments-content:eq(0)').html());
         var  regContent = /[&nbsp;|\s]+?:\s(.*)<div class= "comments-op" >/.exec(html);
         var  replyContent = regContent.length > 0 ? regContent[1] :  "" //回复内容
         return  $.trim(replyContent);
     };
     //获得最新的聊天关系
     this .getLastRelationship =  function  (i) {
         var  array = [];
         try  {
             var  obj =  null ;
             if  ( this .hasChildNode(i))
                 obj =  this .getCommentsList(i).last();
             else
                 obj =  this .getRoot(i);
             if  (obj.length == 0)
                 return  [];
             var  temp = obj.find( '.c_tx.q_namecard' );
             //console.log(temp);
             if  (temp !=  null  && temp.length == 1) {
                 array.push(temp.eq(0).text());  //回复人
                 console.log( "回复人:"  + temp.eq(0).text());
             else  if  (temp !=  null  && temp.length == 2) {
                 array.push(temp.eq(0).text());  //回复人
                 array.push(temp.eq(1).text());  //被回复人
                 console.log( "回复人:"  + temp.eq(0).text());
                 console.log( "被回复人:"  + temp.eq(1).text());
             }
 
             // var html = obj.find('.comments-content:eq(0)').html();
             // var reg = new RegExp("<a.+?q_namecard.+?>(.+?)<\/a>", "g");
             // var tmp = reg.exec(html);
             // console.log(tmp);
             // if (tmp != null && tmp.length > 0)
             // array.push(tmp[1]); //回复人
             // tmp = reg.exec(html);
             // console.log(tmp);
             // if (tmp != null && tmp.length > 0)
             // array.push(tmp[1]); //被回复人
         catch  (ex) {
             console.log( "getLastRelationship:"  + ex);
         }
         //console.log(array);
         return  array;  //0回复人 1被回复人
     };
     //是否有子节点
     this .hasChildNode =  function  (i) {
         return  this .getCommentsList(i).length > 0;
     };
     //是否已经回复
     this .isReplyed =  function  (i) {
         if  ( this .getLastRelationship(i).length > 0)
             return  this .getLastRelationship(i)[0] ==  this .qzOwner();
         else
             return  true //回复过
     };
     //提交回复
     this .putReply =  function  (i, replyContent) {
         var  rootObj =  this .getRoot(i);
         //回复按钮:点击打开文本框(最后一个)
         rootObj.find( '.act-reply:last' ).get(0).click();
         //var replyBtn = this.getRoot(i).find('.act-reply:last');
         //var replyBtnIndex = $('.act-reply').index(replyBtn);
         //$$('.act-reply')[replyBtnIndex].click();
         //文本框内容
         var  text = $( ".mention" ).parent().html();
         //修改文本内容
         var  oldText = /<\/button>.*/.exec(text)[0];
         text = text.replace(oldText,  "</button>"  + replyContent);
         $( 'div.textarea.input_focus' ).html(text);
         //提交
         rootObj.find( 'a.btn-post.gb_bt.evt_click:last' ).get(0).click();
         //var submitBtn = this.getRoot(i).find('a.btn-post.gb_bt.evt_click:last');
         //var submitBtnIndex = $('a.btn-post.gb_bt.evt_click').index(submitBtn);
         //$$('a.btn-post.gb_bt.evt_click')[submitBtnIndex].click();
     };
     //进入个人中心页面
     this .goToUserCenter =  function  () {
         top.location = $( '#aIcenter' ).attr( 'href' );
     };
     //进入与我相关页面
     this .goToAboutMe =  function  () {
         $( '.sn-title:contains("与我相关")' ).click();
     };
     //刷新回复页面
     this .refreshReply =  function  (callbackFun) {
         $( '#feed_me_refresh' )[0].click();
         refreshReplySid = setInterval( function  () {
                 if  (qz.isLoadCompleted()) {
                     if  (callbackFun)
                         callbackFun();
                     clearInterval(refreshReplySid);
                     console.log( "load completed!" );
                 else
                     console.log( "loading..." );
             }, 500);
     };
     //回复页面是否加载完毕
     this .isLoadCompleted =  function  () {
         var  style = $( '#feed_me_list' ).attr( 'style' );
         return  style ==  "display: block;"  || style ==  "" ;
     }
}


background.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var  status = 0;  //0停止 1运行
//消息传递监听 onRequest
chrome.extension.onRequest.addListener(
     function  (request, sender, sendResponse) {
     var  text = request.text;
     console.log(sender.tab ?
         "from a content script:"  + sender.tab.url :
         "from the extension" );
     console.log( "收到消息:"  + text);
     if  (text !=  null  && text.length > 0) {
         console.log( "请求小贱鸡获取回复" );
         //请求小贱鸡获取回复
         simsimiQuery(text,  function  (data) {
             sendResponse({
                 reply : data
             });
         });
     else  {
         sendResponse({
             reply :  "未识别出留言内容"
         });
     }
 
});
 
chrome.browserAction.onClicked.addListener( function  (tab) {
     if  (tab.url.indexOf( "http://user.qzone.qq.com/" ) >= 0) {
         //对 contentScript 发送消息
         if  (status == 0)
             status = 1;
         else
             status = 0;
         console.log( "send request" );
         chrome.tabs.sendRequest(tab.id, {
             status : status
         },  function  (response) {
             console.log(response.farewell);
         });
         //run();
     else  {
         alert( "这个只能在QQ空间运行!" );
         console.log( "只能在qq空间运行" );
     }
});
 
function  run() {
 
     simsimiQuery( "呵呵" function  (data) {
         alert(data)
     });
 
}
 
function  simsimiQuery(data, callbackFun) {
     $.ajax({
         cache :  false ,
         async :  false ,
         url :  "http://www.simsimi.com/func/reqN" ,
         data : {
             "lc"  "ch" ,
             "ft"  "0.0" ,
             "req"  : data,
             "fl"  "http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm%3Flc%3Dch"
         },
         type :  "get" ,
         success :  function  (d) {
             callbackFun(d.sentence_resp);
         }
     });
}


懒得看代码直接用,点击下载crx文件,解压双击安装(首先安装360chrome浏览器

下载地址 http://www.woshishenjingbing.com/blog/zb_users/upload/2014/09/20140902140831_19783.zip

==========

说明: 这个插件目前效果只会回复一句,说主人不在,而且要等个3,4分钟才开始小黄鸡自动回复。 有待完善

===========

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值