-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (81 loc) · 2.84 KB
/
app.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
let sourceID;
chrome.identity.getProfileUserInfo(function(browserUser){
sourceID = browserUser.id;
document.getElementsByClassName("browserID")[0].textContent = "ID: " + sourceID;
});
document.getElementsByTagName("i")[0].addEventListener("click",(event)=>{
const sinkBrowserID = prompt("Enter the browser ID");
if(sinkBrowserID != null){
const nickName = prompt("Enter nickname for browser " + sinkBrowserID);
if(nickName != null){
chrome.storage.local.get({lyncSavedSources:[]},(result) => {
var lyncSavedSources = result.lyncSavedSources;
lyncSavedSources.push({sourceID:sinkBrowserID,nickName:nickName});
chrome.storage.local.set({lyncSavedSources:lyncSavedSources},()=>{
window.close();
});
});
}
}
});
function removeBrowser(browserID){
chrome.storage.local.get({lyncSavedSources:[]},(result) => {
var lyncSavedSources = result.lyncSavedSources;
for(var b in lyncSavedSources){
if(lyncSavedSources[b].sourceID == browserID){
lyncSavedSources.splice(b,1);
break;
}
}
chrome.storage.local.set({lyncSavedSources:lyncSavedSources},()=>{
window.close();
})
});
}
function loadSinkNames(){
var currentDiv = document.getElementsByClassName("paired")[0];
chrome.storage.local.get((result) => {
const lyncSavedSources = result.lyncSavedSources;
for(var i in lyncSavedSources){
const sinkDiv = document.createElement("div");
sinkDiv.setAttribute("class","sinks");
const sinkNameSpan = document.createElement("span");
sinkNameSpan.setAttribute("class","sinkName");
sinkNameSpan.appendChild(document.createElement("p").appendChild(document.createTextNode(lyncSavedSources[i].nickName)));
sinkDiv.appendChild(sinkNameSpan);
const trashIconSpan = document.createElement("span");
const trashIcon = document.createElement("i");
trashIcon.setAttribute("class","trashIcon fa-solid fa-trash");
trashIconSpan.appendChild(trashIcon);
sinkDiv.appendChild(trashIconSpan);
sinkNameSpan.addEventListener("click",(event) => {
sendLink(sourceID,lyncSavedSources[i].sourceID);
});
trashIcon.addEventListener("click",(event) => {
removeBrowser(lyncSavedSources[i].sourceID);
});
currentDiv.after(sinkDiv);
currentDiv = sinkDiv;
}
});
}
function sendLink(sourceID,sinkID) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, async (tabs) => {
let url = tabs[0].url;
const data = {
"initial" : false,
"sourceID" : sourceID,
"sinkID" : sinkID,
"url" : url
};
chrome.runtime.sendMessage(data,(res)=>{
if(res.status == "SUCCESS"){
window.close();
}
else{
alert("Error sending the link!");
}
});
});
}
document.body.onload = loadSinkNames;