-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
65 lines (55 loc) · 1.74 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
61
62
63
64
65
let connection;
let sourceID;
chrome.identity.getProfileUserInfo(function(browserUser){
sourceID = browserUser.id;
});
connection = new WebSocket("wss://lync-api.glitch.me");
connection.onopen = (event) => {
console.log("Connected to server.");
const initialSetupData = {
"initial" : true,
"sourceID" : sourceID
}
try{
connection.send(JSON.stringify(initialSetupData));
}
catch(error){
console.log("Error sending initial setup data\n", error);
}
}
connection.onmessage = (event) => {
const data = JSON.parse(event.data);
const senderID = data["sourceID"];
chrome.storage.local.get({lyncSavedSources:[]},(result) => {
var lyncSavedSources = result.lyncSavedSources;
var alreadySaved = false;
for(let i in lyncSavedSources){
if(lyncSavedSources[i].sourceID == senderID){
alreadySaved = true;
break;
}
}
if(alreadySaved){
chrome.tabs.create({url:data["link"]});
}
else{
const canPair = confirm("Receive links from browser " + senderID + "?");
if(canPair){
const nickName = prompt("Enter nickname for browser " + senderID);
lyncSavedSources.push({sourceID:senderID,nickName:nickName});
chrome.storage.local.set({lyncSavedSources:lyncSavedSources}, () => {
chrome.tabs.create({url:data["link"]});
});
}
}
});
}
chrome.runtime.onMessage.addListener(function(tabData,sender,res){
try{
connection.send(JSON.stringify(tabData));
res({status:"SUCCESS"});
}
catch(error){
res({status:"ERROR"});
}
});