Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ajayyy/SponsorBlock into …
Browse files Browse the repository at this point in the history
…pr/tech234a/2187
  • Loading branch information
ajayyy committed Mar 6, 2025
2 parents e5cc058 + b0984e9 commit 9ab235c
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 61 deletions.
2 changes: 1 addition & 1 deletion manifest/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_fullName__",
"short_name": "SponsorBlock",
"version": "5.11",
"version": "5.11.7",
"default_locale": "en",
"description": "__MSG_Description__",
"homepage_url": "https://sponsor.ajay.app",
Expand Down
1 change: 1 addition & 0 deletions public/options/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ input[type='number'] {
display: inline-block;
width: 40px;
height: 24px;
min-width: 40px;
}

.switch input {
Expand Down
10 changes: 10 additions & 0 deletions public/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,16 @@
<div class="inline"></div>
</div>

<div data-type="keybind-change" data-sync="upvoteKeybind">
<label class="optionLabel">__MSG_setUpvoteKeybind__:</label>
<div class="inline"></div>
</div>

<div data-type="keybind-change" data-sync="downvoteKeybind">
<label class="optionLabel">__MSG_setDownvoteKeybind__:</label>
<div class="inline"></div>
</div>

</div>

<div id="import" class="option-group hidden">
Expand Down
6 changes: 3 additions & 3 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
chrome.tabs.create({url: chrome.runtime.getURL(request.url)});
return false;
case "submitVote":
submitVote(request.type, request.UUID, request.category).then(callback);
submitVote(request.type, request.UUID, request.category, request.videoID).then(callback);

//this allows the callback to be called later
return true;
Expand Down Expand Up @@ -214,7 +214,7 @@ async function unregisterFirefoxContentScript(id: string) {
}
}

async function submitVote(type: number, UUID: string, category: string) {
async function submitVote(type: number, UUID: string, category: string, videoID: string) {
let userID = Config.config.userID;

if (userID == undefined || userID === "undefined") {
Expand All @@ -226,7 +226,7 @@ async function submitVote(type: number, UUID: string, category: string) {
const typeSection = (type !== undefined) ? "&type=" + type : "&category=" + category;

try {
const response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + typeSection);
const response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&videoID=" + videoID + "&userID=" + userID + typeSection);

if (response.ok) {
return {
Expand Down
49 changes: 28 additions & 21 deletions src/components/SkipNoticeComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from "react";
import * as CompileConfig from "../../config.json";
import Config from "../config"
import { Category, ContentContainer, SponsorTime, NoticeVisbilityMode, ActionType, SponsorSourceType, SegmentUUID } from "../types";
import { Category, ContentContainer, SponsorTime, NoticeVisibilityMode, ActionType, SponsorSourceType, SegmentUUID } from "../types";
import NoticeComponent from "./NoticeComponent";
import NoticeTextSelectionComponent from "./NoticeTextSectionComponent";
import Utils from "../utils";
const utils = new Utils();
import { getSkippingText, getUpcomingText } from "../utils/categoryUtils";
import { getSkippingText, getUpcomingText, getVoteText } from "../utils/categoryUtils";

import ThumbsUpSvg from "../svg-icons/thumbs_up_svg";
import ThumbsDownSvg from "../svg-icons/thumbs_down_svg";
Expand All @@ -29,6 +29,7 @@ export interface SkipNoticeProps {
autoSkip: boolean;
startReskip?: boolean;
upcomingNotice?: boolean;
voteNotice?: boolean;
// Contains functions and variables from the content script needed by the skip notice
contentContainer: ContentContainer;

Expand Down Expand Up @@ -102,7 +103,7 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
this.autoSkip = props.autoSkip;
this.contentContainer = props.contentContainer;

const noticeTitle = !this.props.upcomingNotice ? getSkippingText(this.segments, this.props.autoSkip) : getUpcomingText(this.segments);
const noticeTitle = this.props.voteNotice ? getVoteText(this.segments) : !this.props.upcomingNotice ? getSkippingText(this.segments, this.props.autoSkip) : getUpcomingText(this.segments);

const previousSkipNotices = document.querySelectorAll(".sponsorSkipNoticeParent:not(.sponsorSkipUpcomingNotice)");
this.amountOfPreviousNotices = previousSkipNotices.length;
Expand Down Expand Up @@ -186,8 +187,8 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
idSuffix={this.idSuffix}
fadeIn={this.props.fadeIn}
fadeOut={!this.props.upcomingNotice}
startFaded={Config.config.noticeVisibilityMode >= NoticeVisbilityMode.FadedForAll
|| (Config.config.noticeVisibilityMode >= NoticeVisbilityMode.FadedForAutoSkip && this.autoSkip)}
startFaded={Config.config.noticeVisibilityMode >= NoticeVisibilityMode.FadedForAll
|| (Config.config.noticeVisibilityMode >= NoticeVisibilityMode.FadedForAutoSkip && this.autoSkip)}
timed={true}
maxCountdownTime={this.state.maxCountdownTime}
style={noticeStyle}
Expand Down Expand Up @@ -242,15 +243,18 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
</div>

{/* Copy and Downvote Button */}
<div id={"sponsorTimesDownvoteButtonsContainerCopyDownvote" + this.idSuffix}
className="voteButton"
style={{marginLeft: "5px"}}
onClick={() => this.openEditingOptions()}>
<PencilSvg fill={this.state.editing === true
|| this.state.actionState === SkipNoticeAction.CopyDownvote
|| this.state.choosingCategory === true
? this.selectedColor : this.unselectedColor} />
</div>
{
!this.props.voteNotice &&
<div id={"sponsorTimesDownvoteButtonsContainerCopyDownvote" + this.idSuffix}
className="voteButton"
style={{marginLeft: "5px"}}
onClick={() => this.openEditingOptions()}>
<PencilSvg fill={this.state.editing === true
|| this.state.actionState === SkipNoticeAction.CopyDownvote
|| this.state.choosingCategory === true
? this.selectedColor : this.unselectedColor} />
</div>
}
</td>

:
Expand Down Expand Up @@ -278,11 +282,11 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
}

{/* Unskip/Skip Button */}
{!this.props.smaller || this.segments[0].actionType === ActionType.Mute
{!this.props.voteNotice && (!this.props.smaller || this.segments[0].actionType === ActionType.Mute)
? this.getSkipButton(1) : null}

{/* Never show button */}
{!this.autoSkip || this.props.startReskip ? "" :
{!this.autoSkip || this.props.startReskip || this.props.voteNotice ? "" :
<td className="sponsorSkipNoticeRightSection"
key={1}>
<button className="sponsorSkipObject sponsorSkipNoticeButton sponsorSkipNoticeRightButton"
Expand Down Expand Up @@ -374,7 +378,7 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
style.minWidth = "100px";
}

const showSkipButton = (buttonIndex !== 0 || this.props.smaller || this.segments[0].actionType === ActionType.Mute) && !this.props.upcomingNotice;
const showSkipButton = (buttonIndex !== 0 || this.props.smaller || !this.props.voteNotice || this.segments[0].actionType === ActionType.Mute) && !this.props.upcomingNotice;

return (
<span className="sponsorSkipNoticeUnskipSection" style={{ visibility: !showSkipButton ? "hidden" : null }}>
Expand Down Expand Up @@ -624,14 +628,17 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
}

unskip(buttonIndex: number, index: number, forceSeek: boolean): void {
this.contentContainer().unskipSponsorTime(this.segments[index], this.props.unskipTime, forceSeek);
this.contentContainer().unskipSponsorTime(this.segments[index], this.props.unskipTime, forceSeek, this.props.voteNotice);

this.unskippedMode(buttonIndex, index, SkipButtonState.Redo);
this.unskippedMode(buttonIndex, index, this.segments[0].actionType === ActionType.Poi ? SkipButtonState.Undo : SkipButtonState.Redo);
}

reskip(buttonIndex: number, index: number, forceSeek: boolean): void {
this.contentContainer().reskipSponsorTime(this.segments[index], forceSeek);
this.reskippedMode(buttonIndex);
}

reskippedMode(buttonIndex: number): void {
const skipButtonStates = this.state.skipButtonStates;
skipButtonStates[buttonIndex] = SkipButtonState.Undo;

Expand Down Expand Up @@ -661,7 +668,7 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
}

getUnskippedModeInfo(buttonIndex: number, index: number, skipButtonState: SkipButtonState): SkipNoticeState {
const changeCountdown = this.segments[index].actionType !== ActionType.Poi;
const changeCountdown = !this.props.voteNotice && this.segments[index].actionType !== ActionType.Poi;

const maxCountdownTime = changeCountdown ?
this.getFullDurationCountdown(index) : this.state.maxCountdownTime;
Expand Down Expand Up @@ -697,7 +704,7 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
getFullDurationCountdown(index: number): () => number {
return () => {
const sponsorTime = this.segments[index];
const duration = Math.round((sponsorTime.segment[1] - getCurrentTime()) * (1 / getVideo().playbackRate));
const duration = Math.round((sponsorTime.segment[1] - getCurrentTime()) * (1 / (getVideo()?.playbackRate ?? 1)));

return Math.max(duration, Config.config.skipNoticeDuration);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/options/KeybindDialogComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ class KeybindDialogComponent extends React.Component<KeybindDialogProps, Keybind
this.props.option !== "actuallySubmitKeybind" && this.equals(Config.config['actuallySubmitKeybind']) ||
this.props.option !== "previewKeybind" && this.equals(Config.config['previewKeybind']) ||
this.props.option !== "closeSkipNoticeKeybind" && this.equals(Config.config['closeSkipNoticeKeybind']) ||
this.props.option !== "startSponsorKeybind" && this.equals(Config.config['startSponsorKeybind']))
this.props.option !== "startSponsorKeybind" && this.equals(Config.config['startSponsorKeybind']) ||
this.props.option !== "downvoteKeybind" && this.equals(Config.config['downvoteKeybind']) ||
this.props.option !== "upvoteKeybind" && this.equals(Config.config['upvoteKeybind']))
return {message: chrome.i18n.getMessage("keyAlreadyUsed"), blocking: true};

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CompileConfig from "../config.json";
import * as invidiousList from "../ci/invidiouslist.json";
import { Category, CategorySelection, CategorySkipOption, NoticeVisbilityMode, PreviewBarOption, SponsorTime, VideoID, SponsorHideType } from "./types";
import { Category, CategorySelection, CategorySkipOption, NoticeVisibilityMode, PreviewBarOption, SponsorTime, VideoID, SponsorHideType } from "./types";
import { Keybind, ProtoConfig, keybindEquals } from "../maze-utils/src/config";
import { HashedValue } from "../maze-utils/src/hash";

Expand Down Expand Up @@ -32,7 +32,7 @@ interface SBConfig {
trackDownvotesInPrivate: boolean;
dontShowNotice: boolean;
showUpcomingNotice: boolean;
noticeVisibilityMode: NoticeVisbilityMode;
noticeVisibilityMode: NoticeVisibilityMode;
hideVideoPlayerControls: boolean;
hideInfoButtonPlayerControls: boolean;
hideDeleteButtonPlayerControls: boolean;
Expand Down Expand Up @@ -97,6 +97,8 @@ interface SBConfig {
nextChapterKeybind: Keybind;
previousChapterKeybind: Keybind;
closeSkipNoticeKeybind: Keybind;
upvoteKeybind: Keybind;
downvoteKeybind: Keybind;

// What categories should be skipped
categorySelections: CategorySelection[];
Expand Down Expand Up @@ -295,7 +297,7 @@ const syncDefaults = {
trackDownvotesInPrivate: false,
dontShowNotice: false,
showUpcomingNotice: false,
noticeVisibilityMode: NoticeVisbilityMode.FadedForAutoSkip,
noticeVisibilityMode: NoticeVisibilityMode.FadedForAutoSkip,
hideVideoPlayerControls: false,
hideInfoButtonPlayerControls: false,
hideDeleteButtonPlayerControls: false,
Expand Down Expand Up @@ -356,6 +358,8 @@ const syncDefaults = {
nextChapterKeybind: { key: "ArrowRight", ctrl: true },
previousChapterKeybind: { key: "ArrowLeft", ctrl: true },
closeSkipNoticeKeybind: { key: "Backspace" },
downvoteKeybind: { key: "h", shift: true },
upvoteKeybind: { key: "g", shift: true },

categorySelections: [{
name: "sponsor" as Category,
Expand Down Expand Up @@ -466,7 +470,11 @@ const syncDefaults = {
"preview-filler": {
color: "#2E0066",
opacity: "0.7"
}
},
"chapter": {
color: "#fff",
opacity: "0"
},
}
};

Expand Down
43 changes: 35 additions & 8 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
for (const segment of importedSegments) {
if (!sponsorTimesSubmitting.some(
(s) => Math.abs(s.segment[0] - segment.segment[0]) < 1
&& Math.abs(s.segment[1] - segment.segment[1]) < 1)) {
&& Math.abs(s.segment[1] - segment.segment[1]) < 1
&& s.description === segment.description)) {
const hasChaptersPermission = (Config.config.showCategoryWithoutPermission
|| Config.config.permissions["chapter"]);
if (segment.category === "chapter" && (!utils.getCategorySelection("chapter") || !hasChaptersPermission)) {
Expand Down Expand Up @@ -1406,7 +1407,7 @@ function updatePreviewBar(): void {
showLarger: segment.actionType === ActionType.Poi,
description: segment.description,
source: segment.source,
requiredSegment: requiredSegment && (segment.UUID === requiredSegment || segment.UUID?.startsWith(requiredSegment)),
requiredSegment: requiredSegment && (segment.UUID === requiredSegment || segment.UUID?.startsWith(requiredSegment) || requiredSegment.startsWith(segment.UUID)),
selectedSegment: selectedSegment && segment.UUID === selectedSegment
});
});
Expand Down Expand Up @@ -1684,7 +1685,7 @@ function sendTelemetryAndCount(skippingSegments: SponsorTime[], secondsSkipped:
counted = true;
}

if (fullSkip) asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID);
if (fullSkip) asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID + "&videoID=" + getVideoID());
}
}
}
Expand Down Expand Up @@ -1784,7 +1785,7 @@ function skipToTime({v, skipTime, skippingSegments, openNotice, forceAutoSkip, u
if (autoSkip || isSubmittingSegment) sendTelemetryAndCount(skippingSegments, skipTime[1] - skipTime[0], true);
}

function createSkipNotice(skippingSegments: SponsorTime[], autoSkip: boolean, unskipTime: number, startReskip: boolean) {
function createSkipNotice(skippingSegments: SponsorTime[], autoSkip: boolean, unskipTime: number, startReskip: boolean, voteNotice = false) {
for (const skipNotice of skipNotices) {
if (skippingSegments.length === skipNotice.segments.length
&& skippingSegments.every((segment) => skipNotice.segments.some((s) => s.UUID === segment.UUID))) {
Expand All @@ -1798,7 +1799,7 @@ function createSkipNotice(skippingSegments: SponsorTime[], autoSkip: boolean, un
const newSkipNotice = new SkipNotice(skippingSegments, autoSkip, skipNoticeContentContainer, () => {
upcomingNotice?.close();
upcomingNotice = null;
}, unskipTime, startReskip, upcomingNoticeShown);
}, unskipTime, startReskip, upcomingNoticeShown, voteNotice);
if (isOnMobileYouTube() || Config.config.skipKeybind == null) newSkipNotice.setShowKeybindHint(false);
skipNotices.push(newSkipNotice);

Expand All @@ -1817,13 +1818,13 @@ function createUpcomingNotice(skippingSegments: SponsorTime[], timeLeft: number,
upcomingNotice = new UpcomingNotice(skippingSegments, skipNoticeContentContainer, timeLeft / 1000, autoSkip);
}

function unskipSponsorTime(segment: SponsorTime, unskipTime: number = null, forceSeek = false) {
function unskipSponsorTime(segment: SponsorTime, unskipTime: number = null, forceSeek = false, voteNotice = false) {
if (segment.actionType === ActionType.Mute) {
getVideo().muted = false;
videoMuted = false;
}

if (forceSeek || segment.actionType === ActionType.Skip) {
if (forceSeek || segment.actionType === ActionType.Skip || voteNotice) {
//add a tiny bit of time to make sure it is not skipped again
setCurrentTime(unskipTime ?? segment.segment[0] + 0.001);
}
Expand Down Expand Up @@ -2282,7 +2283,8 @@ async function voteAsync(type: number, UUID: SegmentUUID, category?: Category):
message: "submitVote",
type: type,
UUID: UUID,
category: category
category: category,
videoID: getVideoID()
}, (response) => {
if (response.successType === 1) {
// Change the sponsor locally
Expand Down Expand Up @@ -2545,6 +2547,23 @@ function previousChapter(): void {
}
}

async function handleKeybindVote(type: number): Promise<void>{
let lastSkipNotice = skipNotices[0]?.skipNoticeRef.current;
lastSkipNotice?.onMouseEnter();

if (!lastSkipNotice) {
const lastSegment = [...sponsorTimes].reverse()?.find((s) => s.source == SponsorSourceType.Server && (s.segment[0] <= getCurrentTime() && getCurrentTime() - (s.segment[1] || s.segment[0]) <= Config.config.skipNoticeDuration));
if (!lastSegment) return;

createSkipNotice([lastSegment], shouldAutoSkip(lastSegment), lastSegment?.segment[0] + 0.001,false, true);
lastSkipNotice = await skipNotices[0].waitForSkipNoticeRef();
lastSkipNotice?.reskippedMode(0);
}

vote(type,lastSkipNotice?.segments[0]?.UUID, undefined, lastSkipNotice);
return;
}

function addHotkeyListener(): void {
document.addEventListener("keydown", hotkeyListener);

Expand Down Expand Up @@ -2588,6 +2607,8 @@ function hotkeyListener(e: KeyboardEvent): void {
const openSubmissionMenuKey = Config.config.submitKeybind;
const nextChapterKey = Config.config.nextChapterKeybind;
const previousChapterKey = Config.config.previousChapterKeybind;
const upvoteKey = Config.config.upvoteKeybind;
const downvoteKey = Config.config.downvoteKeybind;

if (keybindEquals(key, skipKey)) {
if (activeSkipKeybindElement) {
Expand Down Expand Up @@ -2631,6 +2652,12 @@ function hotkeyListener(e: KeyboardEvent): void {
if (sponsorTimes.length > 0) e.stopPropagation();
previousChapter();
return;
} else if (keybindEquals(key, upvoteKey)) {
handleKeybindVote(1);
return;
} else if (keybindEquals(key, downvoteKey)) {
handleKeybindVote(0);
return;
}
}

Expand Down
Loading

0 comments on commit 9ab235c

Please sign in to comment.