-
Notifications
You must be signed in to change notification settings - Fork 0
/
espn-cbb-247-recruits.user.js
150 lines (121 loc) · 4.89 KB
/
espn-cbb-247-recruits.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
// ==UserScript==
// @name ESPN CBB 247 Recruits
// @description Adds high school ranking to ESPN College Basketball player pages
// @namespace https://github.com/asecco/espn-cbb-247-recruits
// @version 1.2.1
// @author Andrew https://github.com/asecco
// @license MIT
// @grant none
// @match https://www.espn.com/mens-college-basketball/player/*
// @icon https://github.com/asecco/espn-cbb-247-recruits/raw/main/icon-64.png
// ==/UserScript==
const getPlayerName = () => {
let name = window.location.pathname.split("/").pop()
name = name.replace(/-/g, ' ');
return name
}
const getPlayerYear = () => {
let playerYearElement = document.querySelectorAll('.Table__TBODY')[1];
if (playerYearElement && playerYearElement.lastElementChild.textContent.trim().length >= 9) {
playerYearElement = document.querySelectorAll('.Table__TBODY')[0];
}
const lastChild = playerYearElement ? playerYearElement.lastElementChild : null;
if (lastChild) {
let playerYear = lastChild.textContent.trim();
return playerYear ? playerYear.split('-')[0] : null;
}
return null;
}
const normalizeName = (name) => {
return name.replace(/ jr/g, '').toUpperCase();
};
const removeExistingRecruitingInfo = () => {
const existingPlayerInfo = document.querySelector('.recruiting-info');
if (existingPlayerInfo) {
existingPlayerInfo.remove();
}
}
const updateRecruitingInfo = (ranking) => {
removeExistingRecruitingInfo();
const playerInfo = document.createElement('li');
playerInfo.classList.add('recruiting-info');
const div1 = document.createElement('div');
const div2 = document.createElement('div');
div1.classList.add('ttu');
div1.textContent = 'HS RANKING';
div2.classList.add('fw-medium', 'clr-black');
div2.textContent = ranking;
playerInfo.appendChild(div1);
playerInfo.appendChild(div2);
const playerBioList = document.querySelector('.PlayerHeader__Bio_List');
if (playerBioList) {
playerBioList.appendChild(playerInfo);
}
};
const recruitingSearch = async (redshirt = undefined) => {
const espnName = getPlayerName();
const espnYear = redshirt || getPlayerYear() || new Date().getFullYear();
const baseUrl = `https://247sports.com/Season/${espnYear}-Basketball/CompositeRecruitRankings/`;
const maxPage = 5;
let playerFound = false;
if (!espnName || !espnYear) {
return;
}
updateRecruitingInfo('Searching 247Sports...', false);
for (let page = 1; page <= maxPage; page++) {
const url = `${baseUrl}?page=${page}`;
try {
const response = await fetch(url);
const html = await response.text();
const doc = new DOMParser().parseFromString(html, "text/html");
const listItems = doc.querySelectorAll('li.rankings-page__list-item');
listItems.forEach(item => {
const recruit = item.querySelector('.recruit .rankings-page__name-link');
const rankColumn = item.querySelector('.rank-column .primary').childNodes[0];
const rating = item.querySelector('.rating .rankings-page__star-and-score');
const playerName = recruit ? recruit.textContent.trim() : 'N/A';
const rank = rankColumn ? rankColumn.textContent.trim() : 'N/A';
let starCount = 0;
if (rating) {
const yellowStars = rating.querySelectorAll('.icon-starsolid.yellow');
starCount = yellowStars.length;
}
const playerNameNormalized = normalizeName(playerName);
const espnNameNormalized = normalizeName(espnName);
if (playerNameNormalized === espnNameNormalized) {
const ranking = `${starCount}⭐ #${rank}(${espnYear})`;
updateRecruitingInfo(ranking);
playerFound = true;
}
});
} catch (error) {
console.log("Error fetching recruiting info:", error);
}
if (playerFound) break;
}
if (!playerFound) {
if (redshirt === undefined) {
recruitingSearch(espnYear - 1);
} else {
const ranking = `0⭐ NR(${espnYear})`;
updateRecruitingInfo(ranking, true);
}
}
}
const urlObserver = () => {
let currentPlayerId = getPlayerIdFromURL();
const observer = new MutationObserver(() => {
const newPlayerId = getPlayerIdFromURL();
if (newPlayerId !== currentPlayerId) {
currentPlayerId = newPlayerId;
setTimeout(recruitingSearch, 500);
}
});
observer.observe(document.body, { childList: true, subtree: true });
};
const getPlayerIdFromURL = () => {
const id = window.location.href.match(/\/id\/(\d+)\//);
return id ? id[1] : null;
};
recruitingSearch();
urlObserver();