-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
107 lines (94 loc) · 3.44 KB
/
Copy pathscripts.js
File metadata and controls
107 lines (94 loc) · 3.44 KB
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
document.addEventListener('DOMContentLoaded', function() {
var save = document.getElementById('save');
var load = document.getElementById('load');
var file = document.getElementById('file');
var closeDupes = document.getElementById('closeDupes');
// Load event listeners for buttons
save.addEventListener('click', function() {
getAllURLsFromWindow();
});
load.addEventListener('click', function() {
document.form.file.click(event);
});
closeDupes.addEventListener('click', function() {
closeDuplicateTabs();
});
file.addEventListener('change', function() {
loadArrayFromFile();
});
});
function closeDuplicateTabs() {
// console.log('closeDuplicateTabs running');
var query = {
currentWindow: true,
};
// Get a list of tab URLs opened in the current window
chrome.tabs.query(query, function(tabs) {
var arrURLs = [];
for (var i = 0; i < tabs.length; i++) {
// tabs[i].id shows the id of the tab
// tabs[i].url shows the url of the tab
let normalizedUrl = tabs[i].url.replace('data:text/html,<title>', '');
if (arrURLs.indexOf(normalizedUrl) < 0) {
// new unique tab, add it to the list of tabs
arrURLs.push(normalizedUrl);
} else {
// another tab with the same url already exists
// close the current tab
console.log('Removing tab with url ' + tabs[i].url);
chrome.tabs.remove(tabs[i].id, function() {
// pass
});
};
};
});
};
function getAllURLsFromWindow() {
var query = {
currentWindow: true,
};
// Gets a list of tab URLS opened in the current window
chrome.tabs.query(query, function(tabs) {
var arrURLs = [];
for (var i = 0; i < tabs.length; i++) {
const splitText = 'data:text/html,<title>';
if (tabs[i].url.startsWith(splitText)) {
const realTabUrl = tab.url.replace(splitText, '');
arrURLs.push(realTabUrl);
} else {
arrURLs.push(tabs[i].url);
}
};
// debugShowOutput(JSON.stringify(arrURLs));
writeArrayToFile(arrURLs);
});
};
function loadArrayFromFile() {
console.log("loadArrayFromFile running");
var file = document.getElementById('file').files[0];
if (file) {
var reader = new FileReader();
// Async function to call loadAllURLsFromArray after the reader is done reading
reader.onload = function(e) {
console.log("FileReader load complete");
console.log(e.target.result);
loadAllURLsFromArray(JSON.parse(e.target.result))
};
// Init the FileReader
reader.readAsText(file);
}
};
function loadAllURLsFromArray(arr) {
// Javascript runtime ends early if run in menu.html; delegation to the eventPage (background script) required
console.log("loadAllURLsFromArray running");
chrome.runtime.sendMessage({task: 'openTabs', arr: arr});
};
function writeArrayToFile(arr) {
window.URL = window.URL || window.webkitURL;
chrome.runtime.sendMessage({task: 'saveFile', arr: arr});
};
function debugShowOutput(info) {
// Unused function for debugging purposes
var output = document.getElementById('output');
output.textContent = info;
};