-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (106 loc) · 4.01 KB
/
script.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
126
127
// Get references to DOM elements
const iconUrlInput = document.getElementById('icon-url');
const iconLabelInput = document.getElementById('icon-label');
const addIconButton = document.getElementById('add-icon-button');
const iconsContainer = document.getElementById('icons-container');
const backgroundInput = document.getElementById('background-url');
const setBackgroundButton = document.getElementById('set-background-button');
const toggleContainerButton = document.getElementById('toggle-container-button');
const container = document.querySelector('.container');
// Load icons from localStorage
function loadIcons() {
const icons = JSON.parse(localStorage.getItem('icons')) || [];
console.log('Loaded icons:', icons); // Debugging line
icons.forEach(icon => {
const iconElement = createIconItem(icon.url, icon.label, icon.favicon);
iconsContainer.appendChild(iconElement);
});
}
// Save icons to localStorage
function saveIcons(icons) {
localStorage.setItem('icons', JSON.stringify(icons));
}
// Get current icons from localStorage
function getStoredIcons() {
return JSON.parse(localStorage.getItem('icons')) || [];
}
// Function to create a new icon item
function createIconItem(url, label, favicon) {
const iconItem = document.createElement('div');
iconItem.classList.add('icon-item');
const img = document.createElement('img');
img.src = favicon || `https://www.google.com/s2/favicons?sz=64&domain_url=${url}`;
img.alt = label;
const caption = document.createElement('p');
caption.textContent = label;
const removeButton = document.createElement('button');
removeButton.classList.add('remove-button');
removeButton.innerHTML = '×';
removeButton.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent event from bubbling up to the iconItem
iconsContainer.removeChild(iconItem);
// Update localStorage after removal
let icons = getStoredIcons();
icons = icons.filter(icon => icon.url !== url);
saveIcons(icons);
});
// Add click event to open URL in a new tab
iconItem.addEventListener('click', () => {
// Ensure the URL is absolute
const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `http://${url}`;
window.open(fullUrl, '_blank');
});
iconItem.appendChild(img);
iconItem.appendChild(caption);
iconItem.appendChild(removeButton);
return iconItem;
}
// Event listener for adding new icons
addIconButton.addEventListener('click', () => {
const url = iconUrlInput.value.trim();
const label = iconLabelInput.value.trim();
if (url && label) {
const favicon = `https://www.google.com/s2/favicons?sz=64&domain_url=${url}`;
const newIcon = createIconItem(url, label, favicon);
iconsContainer.appendChild(newIcon);
// Save to localStorage
const icons = getStoredIcons();
icons.push({ url, label, favicon });
saveIcons(icons);
// Clear input fields
iconUrlInput.value = '';
iconLabelInput.value = '';
} else {
alert('Please enter both a URL and a label.');
}
});
// Load icons from localStorage on page load
loadIcons();
// Load background from localStorage on page load
function loadBackground() {
const backgroundUrl = localStorage.getItem('backgroundUrl');
if (backgroundUrl) {
document.body.style.backgroundImage = `url(${backgroundUrl})`;
}
}
// Save background to localStorage
function saveBackground(url) {
localStorage.setItem('backgroundUrl', url);
}
// Event listener for setting the background image
setBackgroundButton.addEventListener('click', () => {
const url = backgroundInput.value.trim();
if (url) {
document.body.style.backgroundImage = `url(${url})`;
saveBackground(url);
backgroundInput.value = ''; // Clear input field
} else {
alert('Please enter a valid image URL.');
}
});
// Load background on page load
loadBackground();
// Toggle container visibility
toggleContainerButton.addEventListener('click', () => {
container.style.display = container.style.display === 'none' || container.style.display === '' ? 'block' : 'none';
});