forked from akshatnitd/Tabs-Share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
116 lines (99 loc) · 3.83 KB
/
popup.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
var save_btns = [];
var remove_btns = [];
var load_btns = [];
const initWindowsData = () => {
let allWindowsData = [];
chrome.windows.getAll((windowData) => {
windowData.map((eachWindow) => {
chrome.tabs.getAllInWindow(eachWindow.id, (tabs) => {
allWindowsData.push(tabs);
})
});
});
return allWindowsData;
}
const createTabItemHTML = (title, linkUrl, favicon) => {
return (`
<a class="tab-item" href = "${linkUrl}">
<div class="circle"><img src="${favicon}" /></div>
<span id="tab_name_text">${title}</span>
</a>
`);
}
const createActionButtonsHTML = (sectionType, key) => {
if (sectionType == 'current') {
return `<button type="button" id="${key}" class="btn btn-primary btn-xs save_btn">Save</button>`;
} else if (sectionType === 'saved') {
return `<button type="button" id="${key}" class="btn btn-primary btn-xs load_btn">Load</button> <button type="button" id="${key}" class="btn btn-danger btn-xs remove_btn">Remove</button>`;
}
}
const createWindowItemHTML = (key, sectionType, tabItemsHTML) => {
return (`
<div class="card" >
<div class="card-header" id="window_${sectionType}_Heading_${key}">
<div class="tab-header">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#window_${sectionType}_${key}" aria-expanded="false" aria-controls="window_${sectionType}_${key}">
${sectionType === 'current' ? 'CURRENT' : 'SAVED'} WINDOW ${key}
</button>
</h5>
<div>
${createActionButtonsHTML(sectionType, key)}
</div>
</div>
</div>
<div id="window_${sectionType}_${key}" class="collapse" aria-labelledby="window_${sectionType}_Heading_${key}" data-parent="#accordion">
<div class="card-body">
${tabItemsHTML}
</div>
</div>
</div>
`);
}
const createSectionHTML = (allWindowsData, sectionType) => {
const noOfWindows = allWindowsData.length;
let sectionHTML = '';
for (let winIdx = 0; winIdx < noOfWindows; winIdx++) {
const windowData = allWindowsData[winIdx];
const noOfTabs = windowData.length;
let tabItemsHTML = '';
for (let tabIdx = 0; tabIdx < noOfTabs; tabIdx++) {
const tabData = windowData[tabIdx];
const favicon = tabData.favIconUrl;
let title = tabData.title, linkUrl = tabData.url;
if (!linkUrl) linkUrl = '';
if (!title) title = linkUrl;
tabItemsHTML += createTabItemHTML(title, linkUrl, favicon);
}
sectionHTML += createWindowItemHTML(winIdx + 1, sectionType, tabItemsHTML);
}
return sectionHTML;
}
const currentActiveSectionData = () => {
const sectionHTML = createSectionHTML(currentWindowsData, 'current');
$('#current #accordion').html(sectionHTML);
};
const savedSectionData = () => {
const totalSavedWindows = localStorage.length;
let savedWindows = [];
for (let idx = 0; idx < totalSavedWindows; idx++) {
savedWindows.push(JSON.parse(localStorage.getItem("saved_window" + (idx + 1))));
}
const sectionHTML = createSectionHTML(savedWindows, 'saved');
$('#saved #accordion').html(sectionHTML);
};
//Function calls
let currentWindowsData = initWindowsData();
setTimeout(() => {
currentActiveSectionData();
savedSectionData();
$('.save_btn').on("click", (event) => {
saveWindowData(event);
});
$('.remove_btn').on("click", (event) => {
removeWindowData(event);
});
$('.load_btn').on("click", (event) => {
loadWindow(event);
});
}, 500);