This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
119 lines (111 loc) · 3.13 KB
/
index.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
// Get options
var prefs;
function refresh_options() {
chrome.storage.sync.get({
defaultfontSize: 48,
showAllMessages: false,
showMyMessages: false,
showUsername: false,
pinWeixinTab: false,
showNotifications: false,
danmuDebug: false
}, function(items) { prefs = items; });
}
refresh_options();
chrome.storage.onChanged.addListener(refresh_options);
// Global Variables
var activated = false;
var hb_timer;
// Toolbar Button
chrome.browserAction.onClicked.addListener(function(tab) {
if (prefs.danmuDebug) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var counter = 0;
var timer = setInterval(function() {
if (counter++ < 10) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'danmu',
message: {user:{name: '弹幕测试'}, content:{text: randomString(), image: ''}}
});
}
else {
window.clearInterval(timer);
}
}, 500);
});
} else {
if (activated) {
chrome.tabs.create({
url: 'http://aidistan.github.io/browser-weixin-danmu/helper.html'
});
}
else {
chrome.tabs.create({
url: 'https://wx.qq.com/',
pinned: prefs.pinWeixinTab
});
}
}
});
// Recieve messages
chrome.runtime.onMessage.addListener(function(request) {
if (request.type === 'weixin') {
if (prefs.showAllMessages || request.message.room == 'inside') {
chrome.runtime.sendMessage({
type: 'notification',
message: { title: '已捕获到消息', text: request.message.content.text }
});
if (request.message.user.isSelf && !prefs.showMyMessages) return;
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (tabs[0] !== undefined) {
chrome.tabs.sendMessage(tabs[0].id, { type: 'danmu', message: request.message });
}
});
}
} else if (request.type === 'heartbeat') {
if (activated) {
window.clearTimeout(hb_timer);
}
else {
hookup();
}
hb_timer = setTimeout(breakup, 5000);
} else if (request.type === 'notification' && prefs.showNotifications) {
chrome.notifications.create('notification', {
type: "basic",
iconUrl: 'data/icons/icon.png',
title: request.message.title,
message: request.message.text
});
}
});
function hookup() {
chrome.runtime.sendMessage({
type: 'notification',
message: { title: '已登陆网页微信', text: '开始监听微信消息' }
});
activated = true;
chrome.browserAction.setIcon({
path: 'data/icons/icon-16.png'
});
}
function breakup() {
chrome.runtime.sendMessage({
type: 'notification',
message: { title: '已退出网页微信', text: '不再监听微信消息' }
});
activated = false;
chrome.browserAction.setIcon({
path: 'data/icons/icon-o-16.png'
});
}
function randomString(len) {
len = len || 32;
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0z123456789';
var maxPos = chars.length;
var str = '';
for (_i = 0; _i < len; _i++) {
str += chars.charAt(Math.floor(Math.random() * maxPos));
}
return str;
}