-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
61 lines (47 loc) · 1.32 KB
/
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
function getNotificationId() {
var id = Math.floor(Math.random() * 9007199254740992) + 1;
return id.toString();
}
function sendNotification(message)
{
chrome.notifications.create(getNotificationId(),
{
title: '排程下載',
iconUrl: 'notify.png',
type: 'basic',
message: message
},
function(notificationId){}
);
}
var port = null;
var hostName = 'twly_receiver';
function onNativeMessage(message) {
console.log(message);
}
function onDisconnected() {
port = null;
console.log("Failed to connect: " + chrome.runtime.lastError.message);
}
function connectNative() {
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
var uri = message.uri
var speaker = message.speaker.trim()
var video_info = "";
var text_lines = speaker.split("\n");
for (var i in text_lines) {
video_info += ("\n"+text_lines[i].trim());
}
sendNotification("影片:" + uri + "\n" + video_info);
if (port != null){
port.postMessage({uri:uri});
}
else{
console.log("no port to post messages");
}
});
connectNative();