-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
125 lines (102 loc) · 3.1 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
117
118
119
120
121
122
123
124
125
const dmp_all_btn = document.getElementById("dmp_all_btn")
const dmp_btn = document.getElementById("dmp_btn")
const all_tabs = document.getElementById("all_tabs")
const fileInput = document.getElementById("fileInput")
var textFile = null
const style = document.createElement('style');
style.textContent = `
.tab-item {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.tab-label {
font-family: Arial, sans-serif;
font-size: 14px;
cursor: pointer;
}
`;
document.head.appendChild(style);
chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, (tabs) => {
tabs.forEach((tab, index) => {
const tabItem = document.createElement('div');
tabItem.className = 'tab-item';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `tab${index}`;
checkbox.name = 'tabs';
checkbox.value = JSON.stringify(tab);
checkbox.className = 'tab-checkbox';
const label = document.createElement('label');
label.htmlFor = `tab${index}`;
label.className = 'tab-label';
label.appendChild(document.createTextNode(tab.title));
tabItem.appendChild(checkbox);
tabItem.appendChild(label);
all_tabs.appendChild(tabItem);
all_tabs.appendChild(document.createElement('hr'));
});
});
function downloadTabs(tabs, file_name){
let text = tabs.map(tab => tab.url).join('\n')
let data = new Blob([text], {type: 'text/plain'});
if (data !== null) {
window.URL.revokeObjectURL(data);
}
textFile = window.URL.createObjectURL(data);
const options = {
url: textFile,
filename: `${file_name}.txt`,
};
chrome.downloads.download(options, (downloadId) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log('Download initiated with ID:', downloadId);
}
});
}
function readFileToArray(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
const contents = event.target.result;
const linesArray = contents.split('\n');
resolve(linesArray);
};
reader.onerror = (error) => {
reject(error);
};
reader.readAsText(file);
});
}
function dump(){
const file_name = document.getElementById("file_name")
const checkedItems = Array.from(document.querySelectorAll('input[name="tabs"]:checked'))
.map(checkbox => JSON.parse(checkbox.value));
downloadTabs(checkedItems, file_name.value)
}
function load() {
fileInput.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (file) {
try {
const tabsArray = await readFileToArray(file);
tabsArray.forEach((tab) => {
chrome.tabs.create({ url: tab });
});
} catch (error) {
console.error('Error reading file:', error);
}
}
});
}
function dumpAll(){
const file_name = document.getElementById("file_name")
chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, (tabs) => {
downloadTabs(tabs, file_name.value)
});
}
dmp_all_btn.addEventListener('click',dumpAll)
dmp_btn.addEventListener('click',dump)
load()