-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcheck-favorites.js
More file actions
40 lines (33 loc) · 1.13 KB
/
check-favorites.js
File metadata and controls
40 lines (33 loc) · 1.13 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
document.addEventListener('DOMContentLoaded', function() {
var STORAGE_KEY = 'qumulo-favorites';
var path = window.location.pathname;
var star = document.getElementById('page-star');
if (!star) return; // not on a page with a star
function getFavorites() {
try { return JSON.parse(localStorage.getItem(STORAGE_KEY)) || []; }
catch(e) { return []; }
}
function saveFavorites(favs) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(favs));
}
function isFavorited(favs) {
return favs.some(function(f) { return f.path === path; });
}
function renderStar(favs) {
star.innerHTML = isFavorited(favs) ? '★' : '☆';
star.style.color = '#f5c518';
}
var favs = getFavorites();
renderStar(favs);
star.addEventListener('click', function() {
var favs = getFavorites();
if (isFavorited(favs)) {
favs = favs.filter(function(f) { return f.path !== path; });
} else {
var title = document.querySelector('h1.post-title-main');
favs.push({ path: path, title: title ? title.textContent.trim() : document.title });
}
saveFavorites(favs);
renderStar(favs);
});
});