Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/content/features/add-friends-fast-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** @jsx h */
import { h } from 'dom-chef'
import select from 'select-dom'

import { deleteUser, getPlayer } from '../helpers/faceit-api'
import { getPlayerProfileNickname } from '../helpers/player-profile'

function handleDeleteUser(playerElement, userGuid, playerGuid) {
deleteUser(userGuid, playerGuid).then(() => {
playerElement.setAttribute('hidden', true)
})
}

export default async parentElement => {
const isUserProfile = select('div.page-title__edit-button', parentElement)

if (isUserProfile === null) {
return
}

const nickname = getPlayerProfileNickname()
const { guid } = await getPlayer(nickname)

const friendList = select.all(
'div.profile-friends > div > section > div > div > div',
parentElement
)

friendList.forEach(friendElement => {
const friendNameElement = select('a', friendElement)

const playerRequest = getPlayer(friendNameElement.innerText)
playerRequest.then(player => {
const isAlreadyDeletable = select('span.fast-delete', friendElement)
if (isAlreadyDeletable) {
return
}

friendElement.children[0].append(
<span
style={{
margin: '1em',
cursor: 'pointer'
}}
className="fast-delete"
onClick={() => handleDeleteUser(friendElement, guid, player.guid)}
aria-label="Delete"
>
x
</span>
)
})
})
}
19 changes: 17 additions & 2 deletions src/content/helpers/faceit-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ import { mapTotalStatsMemoized, mapAverageStatsMemoized } from './stats'
const BASE_URL = 'https://api.faceit.com'
export const CACHE_TIME = 600000

async function fetchApi(path) {
/**
* FetchApi - Used to fetch Faceit API
* @param {string} path - Path of the request. Required
* @param {string} method - Method of the request (eg: POST). Default to GET.
*/
async function fetchApi(path, method) {
if (typeof path !== 'string') {
throw new TypeError(`Expected \`path\` to be a string, got ${typeof path}`)
}

if (typeof method !== 'string') {
method = 'GET'
}

try {
const token = localStorage.getItem('token')
const options = { headers: {} }
const options = {
headers: {},
method
}

if (token) {
options.headers.Authorization = `Bearer ${token}`
Expand Down Expand Up @@ -60,6 +72,9 @@ const fetchApiMemoized = pMemoize(fetchApi, {

export const getUser = userId => fetchApiMemoized(`/core/v1/users/${userId}`)

export const deleteUser = (userId, friendId) =>
fetchApiMemoized(`/core/v1/users/${userId}/friends/${friendId}`, 'DELETE')

export const getPlayer = nickname =>
fetchApiMemoized(`/core/v1/nicknames/${nickname}`)

Expand Down
3 changes: 3 additions & 0 deletions src/content/helpers/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export const isPlayerProfileStats = path =>

export const isPlayerProfile = path =>
/players\/.*$/.test(path || getCurrentPath())

export const isPlayerFriendList = path =>
/players\/.*friends$/.test(path || getCurrentPath())
5 changes: 5 additions & 0 deletions src/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import addMatchRoomEloSelfResult from './features/add-match-room-elo-self-result
import applyMatchRoomFocusMode from './features/apply-match-room-focus-mode'
import addMatchRoomPlayerLinks from './features/add-match-room-player-links'
import addPlayerProfileLinks from './features/add-player-profile-links'
import addFriendFastDelete from './features/add-friends-fast-delete'

let checkedBan = false

Expand Down Expand Up @@ -164,6 +165,10 @@ function observeBody() {
addPlayerProfileBadge(mainContentElement)
addPlayerProfileLinks(mainContentElement)

if (pages.isPlayerFriendList()) {
addFriendFastDelete(mainContentElement)
}

if (pages.isPlayerProfileStats()) {
runFeatureIf(
'playerProfileLevelProgress',
Expand Down