Skip to content

Commit

Permalink
Add context menu to open sidebar when button is missing due to Rumble…
Browse files Browse the repository at this point in the history
… changes
  • Loading branch information
stevencrader committed Aug 18, 2024
1 parent 99a705e commit 93331eb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Extensions require the following permissions:
- `storage`: save options and stream data to local browser storage
- `unlimitedStorage`: removes storage limit (see current usage in options page)
- `tabs`: used to sync options and other data between pages running extension and to open the Cached Rants page
- `contextMenus`: adds option to context menu to open Rant Stats sidebar (or popup). Useful when Rumble changes layout and breaks button in page.

## Sidebar

Open the sidebar (or popup) by clicking the "View Rants" button in the chat area of a stream that is currently live or
the "Open Cached Rants" link on a stream archive page.
Open the sidebar (or popup) by clicking the "View Rants" button in the chat area of a stream that is currently live or the "Open Cached Rants" link on a stream archive page. Additionally, there is a context menu "Open Rant Stats" that attempts to open the sidebar (or popup) for the current video.

![Chat Button](images/chat-small.png) ![Cached Button](images/cached-button.png)

Expand Down
44 changes: 44 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,47 @@ chrome.runtime.onMessage.addListener((message: Message, _sender, sendResponse) =
chrome.action.onClicked.addListener(() => {
handleOpenOptionsPage()
})

/**
* Handle sending open rants sidebar message to tab
* @param url tab url to send message to
*/
const handleMenuOpenRants = (url: string): void => {
openTabs.forEach((tab) => {
if (tab.url === url) {
chrome.tabs.sendMessage(
tab.id,
{
action: Messages.CONTEXT_MENU_OPEN_RANTS,
},
{},
() => {
if (chrome.runtime.lastError) {
// if unable to send message, assume closed, stop tracking
openTabs.delete(tab.id)
}
},
)
}
})
}

/**
* Handle all menu item clicks
* @param info the clicked menu item
*/
async function genericOnClick(info: chrome.contextMenus.OnClickData): Promise<void> {
if (info.menuItemId === "menu-open-rants") {
handleMenuOpenRants(info.pageUrl)
}
}

chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
title: "Open Rant Stats",
id: "menu-open-rants",
})
})

// A generic onclick callback function.
chrome.contextMenus.onClicked.addListener(genericOnClick)
1 change: 1 addition & 0 deletions src/components/open-chat/open-chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
svg {
height: 25px;
width: 25px;
margin-left: 10px;
margin-right: 10px;
vertical-align: middle;
fill: var(--light-fg);
Expand Down
4 changes: 4 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addCacheButton, addChatButton, openRantsButtonHandler } from "./compone
import { setLastSortOrder } from "./components/rants/rant"
import { getVideoID } from "./components/rumble/rumble"
import { registerTab } from "./components/events/events"
import { openSidebar } from "./context-menu-handler"
import { handleUpdateOptions } from "./message-options"
import { replacePageContent } from "./rantstatspage"
import {
Expand All @@ -23,6 +24,9 @@ chrome.runtime.onMessage.addListener((message: Message, _sender, sendResponse) =
case Messages.RUMBLE_THEME_CHANGED_TAB:
updateThemeStyle(message.data.theme as Theme)
break
case Messages.CONTEXT_MENU_OPEN_RANTS:
openSidebar()
break
default:
break
}
Expand Down
21 changes: 21 additions & 0 deletions src/context-menu-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { openRantsButtonHandler } from "./components/open-chat/open-chat"
import { getVideoID } from "./components/rumble/rumble"

/**
* Open sidebar
*/
export const openSidebar = (): void => {
const videoId = getVideoID()
if (videoId === "") {
return
}

const chatHistory = document.getElementsByClassName("chat-history")
if (chatHistory.length === 0) {
// eslint-disable-next-line no-alert
window.alert("Video is not a live stream or no chat history. No Rants to show.")
return // not a live feed
}

openRantsButtonHandler(videoId).then()
}
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"48": "images/icon-48.png",
"128": "images/icon-128.png"
},
"permissions": ["storage", "unlimitedStorage", "tabs"],
"permissions": ["storage", "unlimitedStorage", "tabs", "contextMenus"],
"background": {
"service_worker": "pages/background/background.js"
},
Expand Down
1 change: 1 addition & 0 deletions src/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum Messages {
OPTIONS_SAVED_TAB,
RUMBLE_THEME_CHANGED,
RUMBLE_THEME_CHANGED_TAB,
CONTEXT_MENU_OPEN_RANTS,
}

/**
Expand Down

0 comments on commit 93331eb

Please sign in to comment.