-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.user.js
176 lines (139 loc) · 4.14 KB
/
script.user.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ==UserScript==
// @name Footyroom - Custom Enhancements
// @namespace Violentmonkey Scripts
// @match https://footyroom.co/*
// @grant none
// @version 0.2.0
// @author ushruff
// @description Changes all links to open in current tab, Add button onto page to scroll to top, Change some team badges with more accurate images.
// @require https://raw.githubusercontent.com/ush-ruff/Footyroom-Custom-Enhancements/main/teamBadges.json
// ==/UserScript==
// --------------------
// REFERENCE VARIABLES
// --------------------
const TOP_BTN_ID = "top-btn"
const BADGE_ELEMENTS = {
load: ".mph-teamlogo, .guide-badge, .guide-final-away-badge, .guide-final-home-badge, .scoreline-badge, .team-badge img",
delayed: ".prediction-badge, .table-badge",
click: ".table-badge"
}
const TEAM_BADGES = teamBadges.data
// ----------------
// EVENT LISTENERS
// ----------------
document.addEventListener("click", openLinksInCurrentTab)
document.onload = addTopBtn()
document.onload = setupChangeBadges()
// ----------
// FUNCTIONS
// ----------
// --------------------------
// Open Links in Current Tab
// --------------------------
function openLinksInCurrentTab(e) {
let link = null
if (e.target.target === "_blank") {
link = e.target
}
else if(e.target.closest("a").target === "_blank") {
link = e.target.closest("a")
}
if (link == null) return
e.preventDefault()
link.target = "_self"
const newEvent = new MouseEvent("click", {bubbles: true})
link.dispatchEvent(newEvent)
}
// --------------
// Scroll to Top
// --------------
function addTopBtn() {
const topBtn = document.createElement("button")
topBtn.id = TOP_BTN_ID
document.body.appendChild(topBtn)
addBtnStyle()
topBtn.onclick = (e) => {
e.preventDefault()
scrollToTop()
}
window.addEventListener("scroll", screenPos)
}
function screenPos() {
const y = window.scrollY
const topBtn = document.getElementById(`${TOP_BTN_ID}`)
if (topBtn == null) return
if (y > 250) {
topBtn.classList.remove("hidden")
}
else {
topBtn.classList.add("hidden")
}
}
function scrollToTop() {
const c = document.documentElement.scrollTop || document.body.scrollTop
if (c > 0) {
window.requestAnimationFrame(scrollToTop)
window.scrollTo(0, c - (c / 10))
}
}
function addBtnStyle() {
const styleSheet = document.createElement("style")
styleSheet.textContent = `
#${TOP_BTN_ID} {
appearance: none;
display: block;
position: fixed;
height: 30px;
width: 30px;
bottom: 20px;
left: calc(50% + 1030px/2);
background: #434343 url('https://i.imgur.com/oO391VB.png') no-repeat 50% 9px;
border: 1px solid #232323;
border-radius: 4px;
cursor: pointer;
z-index: 99;
}
#${TOP_BTN_ID}.hidden {
display: none;
}
#${TOP_BTN_ID}:is(:hover, :focus) {
background-color: #5e5e5e;
}
`
styleSheet.id = `${TOP_BTN_ID}-styles`
document.head.append(styleSheet)
}
// -------------------
// Club Badge Changer
// -------------------
function setupChangeBadges() {
if (document.readyState !== "loading") {
changeBadges("load")
predictorChange()
}
const pageTabs = document.querySelectorAll(".simple-tab")
pageTabs.forEach(pageTab => pageTab.addEventListener("click", () => changeBadges("click")))
}
function changeBadges(eventType) {
const allBadgeElements = document.querySelectorAll(BADGE_ELEMENTS[eventType])
allBadgeElements.forEach(badge => {
const badgeNum = parseInt(badge.src.split("/").pop())
const index = TEAM_BADGES.findIndex(element => element.code === badgeNum)
if (index !== -1) {
badge.src = TEAM_BADGES[index].url
}
})
}
function predictorChange() {
const predictorCheck = document.querySelectorAll(".expandable__icn, .extra-content")
if (predictorCheck.length !== 0) {
let reRun = setInterval(change, 250)
function change() {
const lastBadgeElement = document.querySelectorAll(".predictor-match:last-of-type .prediction-badge, .table-row:last-of-type .table-badge")
if (lastBadgeElement.length >= 1) {
clearInterval(reRun)
changeBadges("delayed")
}
}
}
}