-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeco-o.team.toggle-costs.user.js
More file actions
82 lines (70 loc) · 3.02 KB
/
geco-o.team.toggle-costs.user.js
File metadata and controls
82 lines (70 loc) · 3.02 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
// ==UserScript==
// @name GECO-O - Team - Show/Hide Costs
// @namespace http://tampermonkey.net/
// @version 2.0.1
// @description Toggle visibility of cost columns on team page and remember setting in a cookie
// @author Roman Allenstein <r.allenstein@reply.de>
// @match https://geco.reply.com/GeCoO/Project/ManageTeam.aspx?sc=*
// @grant none
// @run-at document-end
// @downloadURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-o.team.toggle-costs.user.js
// @updateURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-o.team.toggle-costs.user.js
// ==/UserScript==
(function () {
"use strict";
const BTN_ID = "tm-btn-toggle-costs";
const COOKIE_NAME = "tm_team_show_costs";
const COOKIE_DAYS = 365;
/* =============================================
REUSABLE HELPERS - copy to other scripts
============================================= */
const el = (tag, attrs = {}, children = []) => {
const e = document.createElement(tag);
Object.entries(attrs).forEach(([k, v]) => k === "text" ? e.textContent = v : k === "html" ? e.innerHTML = v : e.setAttribute(k, v));
children.forEach(c => e.appendChild(typeof c === "string" ? document.createTextNode(c) : c));
return e;
};
const addButtonAfter = (selector, btnAttrs, onClick) => {
const anchor = document.querySelector(selector);
if (!anchor || document.getElementById(btnAttrs.id)) return null;
const btn = el("button", btnAttrs);
if (onClick) btn.addEventListener("click", onClick);
anchor.insertAdjacentElement("afterend", btn);
return btn;
};
/* ============================================= */
const setCookie = (name, val, days) => {
const d = new Date(); d.setTime(d.getTime() + days * 864e5);
document.cookie = `${name}=${encodeURIComponent(val)}; expires=${d.toUTCString()}; path=/`;
};
const getCookie = name => {
const m = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
return m ? decodeURIComponent(m[1]) : null;
};
let showCosts = getCookie(COOKIE_NAME) !== "0";
function updateVisibility() {
document.querySelectorAll(".real-cost-view, .avg-cost-view, #plhAvgCpstHeader, #plhRealCostHeader")
.forEach(el => el.style.display = showCosts ? "" : "none");
}
function toggle() {
showCosts = !showCosts;
updateVisibility();
setCookie(COOKIE_NAME, showCosts ? "1" : "0", COOKIE_DAYS);
const btn = document.getElementById(BTN_ID);
if (btn) btn.textContent = showCosts ? "Hide Costs" : "Show Costs";
}
// Init
const BTN_SELECTOR = "h2";
const init = () => {
const btn = addButtonAfter(BTN_SELECTOR, {
id: BTN_ID,
type: "button",
class: "btn--default btn--light",
style: "margin: 10px 10px 10px 0",
text: showCosts ? "Hide Costs" : "Show Costs"
}, toggle);
if (btn) updateVisibility();
};
window.addEventListener("load", init);
new MutationObserver(init).observe(document.documentElement, {childList: true, subtree: true});
})();