Skip to content

Commit bf28481

Browse files
committed
Add button save
1 parent 398a494 commit bf28481

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

user.js/[SteamHunters] Achievements tweaks.user.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// @license GPL-3.0 License
88
// @tag kevingrillet
99
// @tag steamhunters.com
10-
// @version 1.0.2
10+
// @version 1.0.3
1111

1212
// @homepageURL https://github.com/kevingrillet/Userscripts/
1313
// @supportURL https://github.com/kevingrillet/Userscripts/issues
@@ -16,18 +16,20 @@
1616

1717
// @match https://steamhunters.com/id/*/apps/*/achievements
1818
// @icon https://www.google.com/s2/favicons?sz=64&domain=steamhunters.com
19-
// @grant none
19+
// @grant GM_getValue
20+
// @grant GM_setValue
2021
// @run-at document-end
2122
// ==/UserScript==
2223

2324
(function () {
2425
'use strict';
2526

26-
let isFiltered = false;
27+
let isFiltered = GM_getValue('isFiltered', false);
2728

28-
function toggleVisibility() {
29-
isFiltered = !isFiltered;
29+
function applyFilter() {
3030
const button = document.getElementById('toggle-visibility-btn');
31+
if (!button) return;
32+
3133
const icon = button.querySelector('i');
3234

3335
// Update button appearance
@@ -54,17 +56,44 @@
5456
});
5557
}
5658

57-
const buttonTemplate = `
58-
<button id="toggle-visibility-btn" type="button" class="btn btn-default btn-xs">
59-
<i class="fa fa-filter"></i><span> Hide Completed</span>
60-
</button>
61-
`.trim();
59+
function toggleVisibility() {
60+
isFiltered = !isFiltered;
61+
GM_setValue('isFiltered', isFiltered);
62+
applyFilter();
63+
}
64+
65+
function initButton() {
66+
const buttonTemplate = `
67+
<button id="toggle-visibility-btn" type="button" class="btn btn-default btn-xs">
68+
<i class="fa fa-filter"></i><span> Hide Completed</span>
69+
</button>
70+
`.trim();
71+
72+
const spanContainer = document.querySelector('span.pull-right');
73+
if (spanContainer && !document.getElementById('toggle-visibility-btn')) {
74+
spanContainer.insertAdjacentHTML('beforeend', buttonTemplate);
75+
document.getElementById('toggle-visibility-btn').addEventListener('click', toggleVisibility);
76+
applyFilter();
77+
}
78+
}
79+
80+
// Observer for dynamic content
81+
const observer = new MutationObserver((mutations) => {
82+
for (const mutation of mutations) {
83+
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
84+
initButton();
85+
applyFilter();
86+
}
87+
}
88+
});
6289

63-
const spanContainer = document.querySelector('span.pull-right');
64-
if (spanContainer) {
65-
spanContainer.insertAdjacentHTML('beforeend', buttonTemplate);
66-
document.getElementById('toggle-visibility-btn').addEventListener('click', toggleVisibility);
67-
} else {
68-
console.error('Unable to find <span class="pull-right">');
90+
// Start observing
91+
const targetNode = document.querySelector('.achievements');
92+
if (targetNode) {
93+
observer.observe(targetNode, { childList: true, subtree: true });
6994
}
95+
96+
// Initial setup
97+
initButton();
98+
applyFilter();
7099
})();

0 commit comments

Comments
 (0)