Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
101 lines (91 loc) · 3.61 KB
/
Copy pathpopup.js
File metadata and controls
101 lines (91 loc) · 3.61 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
document.addEventListener('DOMContentLoaded', function () {
const homepageSelect = document.getElementById('homepageSelect');
const saveBtn = document.getElementById('saveBtn');
const deleteBtn = document.getElementById('deleteBtn');
// Load saved homepage options
chrome.storage.local.get(['homepageList', 'selectedHomepage'], function(result) {
// Clear existing options
homepageSelect.innerHTML = '';
if (result.homepageList && result.homepageList.length > 0) {
result.homepageList.forEach(option => {
const item = document.createElement('option');
item.value = option.url;
item.text = option.title;
homepageSelect.add(item);
});
} else {
// Add default option only if the list is empty
const defaultOption = document.createElement('option');
defaultOption.value = "";
defaultOption.text = "Choose a new Tab";
homepageSelect.add(defaultOption);
}
if (result.selectedHomepage) {
homepageSelect.value = result.selectedHomepage;
}
});
// Save current tab URL and title as new homepage option
saveBtn.addEventListener('click', function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
const newUrl = currentTab.url;
const newTitle = currentTab.title;
if (newUrl && !urlAlreadyExists(newUrl)) {
// Remove default option if it exists
if (homepageSelect.options[0].value === "") {
homepageSelect.remove(0);
}
const option = document.createElement('option');
option.value = newUrl;
option.text = newTitle;
homepageSelect.add(option);
homepageSelect.value = newUrl; // Select the new option
saveHomepageList('Settings saved.'); // Update feedback message
} else {
alert("This URL is already in the list!");
}
});
});
// Handle homepage selection change
homepageSelect.addEventListener('change', function() {
saveHomepageList(); // Call saveHomepageList without feedback
});
// Delete selected homepage
deleteBtn.addEventListener('click', function() {
const selectedIndex = homepageSelect.selectedIndex;
if (selectedIndex !== -1 && homepageSelect.options[selectedIndex].value !== "") {
homepageSelect.remove(selectedIndex);
if (homepageSelect.options.length === 0) {
const defaultOption = document.createElement('option');
defaultOption.value = "";
defaultOption.text = "Choose a new Tab";
homepageSelect.add(defaultOption);
}
saveHomepageList('Setting deleted.'); // Update feedback message
}
});
// Save homepage list and selected homepage
function saveHomepageList(feedbackMessage) {
const homepageList = Array.from(homepageSelect.options)
.filter(option => option.value !== "") // Exclude the default option
.map(option => ({
url: option.value,
title: option.text
}));
chrome.storage.local.set({
homepageList: homepageList,
selectedHomepage: homepageSelect.value
}, function() {
// Provide feedback to the user
const status = document.createElement('div');
status.textContent = feedbackMessage || 'Settings saved.'; // Use feedbackMessage if provided
status.style.color = 'green';
document.body.appendChild(status);
setTimeout(() => status.remove(), 2000);
});
}
// Helper function to check if the URL is already in the list
function urlAlreadyExists(newUrl) {
return Array.from(homepageSelect.options).some(option => option.value === newUrl);
}
});