Skip to content

Commit

Permalink
Add Disable Kicking Banned and Audio on Inbox Message
Browse files Browse the repository at this point in the history
  • Loading branch information
smashedr committed Apr 12, 2024
1 parent 193d16f commit bb5aa7f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 33 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ All **Chromium** Based Browsers can install the extension from the
* Play Audio on Your Turn to Make a Move
* Play Audio When Users Join or Leave a Room
* Play Audio When Users Changes Teams
* Play Audio on New Inbox Message
* Play Audio on New Chat Message
* Play Chat Message as Audio (TTS)
* Auto Update Options on Room Creation
Expand All @@ -73,8 +74,9 @@ For any issues, bugs or concerns; please [Open an Issue](https://github.com/smas
* Custom Audio Sounds and Volume
* Custom Chat Commands and Options
* Auto Select Team # and Keep it Set
* Remember Recent Game and Room URLs
* Remember Recent Players who Kicked You
* Player Stats (Total Points, Time Taken)
* Option to Disable Kicking Banned Players

# Configuration

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "PlayDrift Web Extension.",
"homepage_url": "https://github.com/smashedr/playdrift-extension",
"author": "Shane",
"version": "0.2.6",
"version": "0.2.7",
"manifest_version": 3,
"commands": {
"_execute_action": {
Expand Down
Binary file added src/audio/inbox.mp3
Binary file not shown.
20 changes: 19 additions & 1 deletion src/html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,22 @@ <h6 class="mt-3 ms-2 text-body-secondary"><i class="fa-solid fa-comments me-2"><
<h6 class="mt-3 ms-2 text-body-secondary"><i class="fa-solid fa-house me-2"></i> Room Options</h6>
<div class="form-check form-switch">
<input class="form-check-input" id="autoUpdateOptions" type="checkbox" role="switch">
<label class="form-check-label" for="autoUpdateOptions" aria-describedby="autoUpdateOptionsHelp">
<label class="form-check-label text-success-emphasis" for="autoUpdateOptions" aria-describedby="autoUpdateOptionsHelp">
Auto Update Options on Room
<span data-bs-toggle="tooltip" data-bs-title="Auto Update Options on Room.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" id="disableBanKick" type="checkbox" role="switch">
<label class="form-check-label text-success-emphasis" for="disableBanKick" aria-describedby="disableBanKickHelp">
Disable Auto Kicking Banned Players
<span data-bs-toggle="tooltip" data-bs-title="Disable Auto Kicking Banned Players.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>

<h6 class="mt-3 ms-2 text-body-secondary"><i class="fa-solid fa-volume-high me-2"></i> Audio Options</h6>
<div class="form-check form-switch">
Expand Down Expand Up @@ -152,6 +161,15 @@ <h6 class="mt-3 ms-2 text-body-secondary"><i class="fa-solid fa-volume-high me-2
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" id="playInboxAudio" type="checkbox" role="switch">
<label class="form-check-label text-success-emphasis" for="playInboxAudio" aria-describedby="playInboxAudioHelp">
Play Audio on Inbox Message
<span data-bs-toggle="tooltip" data-bs-title="Play Audio on Inbox Message.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" id="playMessageAudio" type="checkbox" role="switch">
<label class="form-check-label" for="playMessageAudio" aria-describedby="playMessageAudioHelp">
Expand Down
36 changes: 27 additions & 9 deletions src/html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,24 @@ <h2 class="text-center mb-0">
<!-- </span>-->
<!-- </label>-->
<!-- </div>-->
<div class="form-check form-switch">
<input class="form-check-input" id="autoUpdateOptions" type="checkbox" role="switch">
<label class="form-check-label" for="autoUpdateOptions" aria-describedby="autoUpdateOptionsHelp">
Auto Update Options on Room
<span data-bs-toggle="tooltip" data-bs-title="Auto Update Options on Room.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>
<!-- <div class="form-check form-switch">-->
<!-- <input class="form-check-input" id="autoUpdateOptions" type="checkbox" role="switch">-->
<!-- <label class="form-check-label" for="autoUpdateOptions" aria-describedby="autoUpdateOptionsHelp">-->
<!-- Auto Update Options on Room-->
<!-- <span data-bs-toggle="tooltip" data-bs-title="Auto Update Options on Room.">-->
<!-- <i class="fa-solid fa-circle-info ms-1"></i>-->
<!-- </span>-->
<!-- </label>-->
<!-- </div>-->
<!-- <div class="form-check form-switch">-->
<!-- <input class="form-check-input" id="disableBanKick" type="checkbox" role="switch">-->
<!-- <label class="form-check-label" for="disableBanKick" aria-describedby="disableBanKickHelp">-->
<!-- Disable Auto Kicking Banned Players-->
<!-- <span data-bs-toggle="tooltip" data-bs-title="Disable Auto Kicking Banned Players.">-->
<!-- <i class="fa-solid fa-circle-info ms-1"></i>-->
<!-- </span>-->
<!-- </label>-->
<!-- </div>-->
<div class="form-check form-switch">
<input class="form-check-input" id="playTurnAudio" type="checkbox" role="switch">
<label class="form-check-label" for="playTurnAudio" aria-describedby="playTurnAudioHelp">
Expand All @@ -135,6 +144,15 @@ <h2 class="text-center mb-0">
<!-- <i class="fa-solid fa-circle-info ms-1"></i>-->
<!-- </span>-->
<!-- </label>-->
<!-- </div>-->
<!-- <div class="form-check form-switch">-->
<!-- <input class="form-check-input" id="playInboxAudio" type="checkbox" role="switch">-->
<!-- <label class="form-check-label" for="playInboxAudio" aria-describedby="playInboxAudioHelp">-->
<!-- Play Audio on Inbox Message-->
<!-- <span data-bs-toggle="tooltip" data-bs-title="Play Audio on Inbox Message.">-->
<!-- <i class="fa-solid fa-circle-info ms-1"></i>-->
<!-- </span>-->
<!-- </label>-->
<!-- </div>-->
<div class="form-check form-switch">
<input class="form-check-input" id="playMessageAudio" type="checkbox" role="switch">
Expand Down
83 changes: 62 additions & 21 deletions src/js/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ let userIntervalID = setInterval(setUserProfile, 1000)
let source1
let source2
let source3
let source4

// State
const profiles = {}
Expand All @@ -94,6 +95,7 @@ let currentRoom = ''
// Audio
const speech = new SpeechSynthesisUtterance()
const audio = {
inbox: new Audio(chrome.runtime.getURL('/audio/inbox.mp3')),
join: new Audio(chrome.runtime.getURL('/audio/join.mp3')),
leave: new Audio(chrome.runtime.getURL('/audio/leave.mp3')),
message: new Audio(chrome.runtime.getURL('/audio/message.mp3')),
Expand Down Expand Up @@ -163,6 +165,8 @@ async function processLoad() {
// setTimeout(startMutation, 3000)
startMutation()

sse4()

const url = new URL(window.location.href)
if (url.searchParams.has('profile')) {
// TODO: Direct Loads do not trigger the MutationObserver
Expand Down Expand Up @@ -494,6 +498,42 @@ async function sse3(game) {
})
}

/**
* Server-Sent Event Inbox Handler
* @function sse4
*/
async function sse4() {
const url = 'https://api-v2.playdrift.com/api/v1/chat/inbox/sse'
console.debug('connecting to sse4 url:', url)
source4 = new EventSource(url, {
withCredentials: true,
})
// console.debug('source2:', source2)
const { options } = await chrome.storage.sync.get(['options'])
const now = Date.now()
source4.addEventListener('msg', function (event) {
const msg = JSON.parse(event.data)
console.debug('sse4:', msg)
console.debug(`${msg.json?.ts} > ${now}`, msg.json?.ts > now)
if (msg.t === 'm' && msg.json?.ts > now) {
if (options.playInboxAudio) {
// newInboxMessage(msg.json)
console.log('play audio')
audio.inbox.play().then()
}
}
})
}

// /**
// * New Inbox Message Handler
// * @function newInboxMessage
// * @param {Object} msg
// */
// function newInboxMessage(msg) {
// console.log('newInboxMessage:', msg)
// }

/**
* Room Player Update Handler
* @function roomPlayerChange
Expand Down Expand Up @@ -602,26 +642,27 @@ async function roomKickedChange(before, after) {
*/
async function roomTeamsChanged(before, after) {
console.debug('roomTeamsChanged:', before, after)
if (after.game) {
return console.debug('not sending team change because game active')
}
const { options } = await chrome.storage.sync.get(['options'])
if (options.sendTeamsChanged || options.playTeamsAudio) {
for (const pid of after.players) {
if (before.teams[pid] !== after.teams[pid]) {
const player = await getProfile(pid, true)
const from = before.teams[pid]
? `Team ${before.teams[pid]}`
: 'No Team'
const to = after.teams[pid]
? `Team ${after.teams[pid]}`
: 'No Team'
if (options.playTeamsAudio) {
await audio.team.play()
}
if (options.sendTeamsChanged) {
await sendChatMessage(
`${player.username} Changed from ${from} to ${to}`
)
}
}
if (!options.sendTeamsChanged && !options.playTeamsAudio) {
return
}
for (const pid of after.players) {
if (before.teams[pid] === after.teams[pid]) {
continue
}
const player = await getProfile(pid, true)
const from = before.teams[pid] ? `Team ${before.teams[pid]}` : 'No Team'
const to = after.teams[pid] ? `Team ${after.teams[pid]}` : 'No Team'
if (options.playTeamsAudio) {
await audio.team.play()
}
if (options.sendTeamsChanged) {
await sendChatMessage(
`${player.username} Changed from ${from} to ${to}`
)
}
}
}
Expand Down Expand Up @@ -814,7 +855,7 @@ async function userJoinRoom(pid, rid = currentRoom) {
}
// const stats = await calStats(player)
if (owner) {
if (banned.includes(pid)) {
if (!options.disableBanKick && banned.includes(pid)) {
await kickPlayer(pid)
await sendChatMessage(`Auto Kicked Banned User: ${player.username}`)
return
Expand Down Expand Up @@ -854,7 +895,7 @@ async function sendKickedPlayers() {
}
const players = []
for (const pid of room.kicked) {
const player = getProfile(pid, true)
const player = await getProfile(pid, true)
players.push(player.username)
}
let msg = 'Kicked Players: '
Expand Down
2 changes: 2 additions & 0 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ async function onInstalled(details) {
sendPlayerLeft: false,
sendTeamsChanged: false,
autoUpdateOptions: false,
disableBanKick: false,
playTurnAudio: false,
playPlayersAudio: false,
playTeamsAudio: false,
playInboxAudio: false,
playMessageAudio: false,
playChatSpeech: false,
autoKickLowRate: false,
Expand Down

0 comments on commit bb5aa7f

Please sign in to comment.